Skip to content

Commit 07df24d

Browse files
authored
[20250214] BOJ / G5 / 링크와 스타트 / 신동윤
1 parent bdcaacd commit 07df24d

File tree

1 file changed

+59
-0
lines changed

1 file changed

+59
-0
lines changed
Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
```java
2+
import java.io.*;
3+
import java.util.*;
4+
5+
public class Main {
6+
7+
private static int N;
8+
private static int[][] S;
9+
10+
public static void main(String[] args) throws IOException {
11+
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
12+
BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(System.out));
13+
14+
N = Integer.parseInt(br.readLine());
15+
S = new int[N][N];
16+
for (int i =0; i < N; i++) {
17+
StringTokenizer st = new StringTokenizer(br.readLine());
18+
for (int j = 0; j < N; j++) {
19+
S[i][j] = Integer.parseInt(st.nextToken());
20+
}
21+
}
22+
23+
boolean[] teamOne = new boolean[N];
24+
int answer = solve(0, teamOne);
25+
bw.write(answer + "\n");
26+
27+
br.close();
28+
bw.flush();
29+
bw.close();
30+
}
31+
32+
private static int solve(int idx, boolean[] teamOne) {
33+
if (idx == N) {
34+
int scoreOne = 0;
35+
int scoreTwo = 0;
36+
for (int i = 0; i < N; i++) {
37+
for (int j = 0; j < N; j++) {
38+
if (teamOne[i] != teamOne[j]) { continue; }
39+
if (teamOne[i]) {
40+
scoreOne += S[i][j];
41+
} else {
42+
scoreTwo += S[i][j];
43+
}
44+
}
45+
}
46+
return Math.abs(scoreOne - scoreTwo);
47+
}
48+
49+
// idx가 팀1에 속하는 경우
50+
teamOne[idx] = true;
51+
int result1 = solve(idx + 1, teamOne);
52+
// idx가 팀2에 속하는 경우
53+
teamOne[idx] = false;
54+
int result2 = solve(idx + 1, teamOne);
55+
return Integer.min(result1, result2);
56+
}
57+
}
58+
59+
```

0 commit comments

Comments
 (0)