-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSWEA_4012.java
More file actions
64 lines (46 loc) · 1.18 KB
/
Copy pathSWEA_4012.java
File metadata and controls
64 lines (46 loc) · 1.18 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
import java.util.Scanner;
// SWEXPERT 4012. ¿ä¸®»ç (2017³â ÇϹݱâ SDS ¹®Á¦)
class Solution02 {
static int n;
static int[][] s;
static int min;
public static void main(String args[]) throws Exception {
Scanner sc = new Scanner(System.in);
int T;
T = sc.nextInt();
for (int test_case = 1; test_case <= T; test_case++) {
n = sc.nextInt();
s = new int[n][n];
int[] check = new int[n];
min = Integer.MAX_VALUE;
for (int i = 0; i < n; i++)
for (int j = 0; j < n; j++)
s[i][j] = sc.nextInt();
cook(check, 0, 0);
System.out.println("#" + test_case + " " + min);
} // for
} // main
public static void cook(int[] check, int a_cnt, int cnt) {
if (a_cnt == n / 2 || cnt == n) {
int a_sum = 0;
int b_sum = 0;
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
if (i == j)
continue;
if (check[i] == 0 && check[j] == 0)
b_sum += s[i][j];
else if (check[i] == 1 && check[j] == 1)
a_sum += s[i][j];
}
}
if (min > Math.abs(a_sum - b_sum))
min = Math.abs(a_sum - b_sum);
return;
}
check[cnt] = 1;
cook(check, a_cnt + 1, cnt + 1);
check[cnt] = 0;
cook(check, a_cnt, cnt + 1);
}
}