문제 출저
https://www.acmicpc.net/problem/3980
3980번: 선발 명단
각각의 테스트 케이스에 대해서, 모든 포지션의 선수를 채웠을 때, 능력치의 합의 최댓값을 한 줄에 하나씩 출력한다. 항상 하나 이상의 올바른 라인업을 만들 수 있다.
www.acmicpc.net
문제 풀이
11명의 축구 선수들은 각각의 포지션의 점수가 있습니다. 11명의 선수들을 포지션을 배치했을 때 가장 큰 점수를 구해야 합니다.
백트래킹으로 풀었습니다.
모든 경우를 계산하여 가장 큰 값을 구했습니다.
소스코드
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.StringTokenizer;
/*
선발 명단
https://www.acmicpc.net/problem/3980
12% 오답
-> result 초기화
*/
public class Main {
static int t;
static StringBuilder sb;
static int n = 11;
static int[][] board;
static boolean[] visited;
static int result;
public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
t = Integer.parseInt(br.readLine());
sb = new StringBuilder();
while(t-->0) {
board = new int[n][n];
for (int i = 0; i < n; i++) {
StringTokenizer st = new StringTokenizer(br.readLine());
for (int j = 0; j < n; j++) {
board[i][j] = Integer.parseInt(st.nextToken());
}
}
solution();
}
System.out.println(sb);
}
public static void solution() {
result = 0;
visited = new boolean[n];
dfs(0, 0);
sb.append(result).append("\n");
}
public static void dfs(int stage, int score) {
if (stage == n) {
result = Math.max(result, score);
return;
}
for (int i = 0; i < n; i++) {
if (visited[i] == false && board[stage][i] > 0) {
visited[i] = true;
dfs(stage+1, score + board[stage][i]);
visited[i] = false;
}
}
}
}