14889 Samsung sw test
코드
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
#include <iostream>
#include <cstring>
#define MAX_WIDTH 20
using namespace std;
int numOfPeople;
int map[MAX_WIDTH][MAX_WIDTH];
int minDifferenceBetweenTwoTeam(int arr[10], int len, int deep);
int getSum(int arr[10]);
int min(int a, int b);
int main() {
cin >> numOfPeople;
for (int i = 0; i < numOfPeople; i++)
for (int j = 0; j < numOfPeople; j++)
cin >> map[i][j];
int arr[10];
memset(arr, -1, sizeof(arr));
cout << minDifferenceBetweenTwoTeam(arr, 0, 0);
}
int minDifferenceBetweenTwoTeam(int arr[10], int len, int deep) {
if (len == numOfPeople / 2) {
//계산
int temp[10];
int index = 0;
int indext = 0;
for (int i = 0; i < numOfPeople; i++) {
if (arr[index] == i)
index++;
else
temp[indext++] = i;
}
int d = getSum(temp) - getSum(arr);
return d < 0 ? (-1) * d : d;
}
else if (numOfPeople - deep < numOfPeople / 2 - len)
return 10000000;
arr[len] = deep;
int a = minDifferenceBetweenTwoTeam(arr, len + 1, deep + 1);
arr[len] = 0;
return min(a, minDifferenceBetweenTwoTeam(arr, len, deep + 1));
}
int min(int a, int b) {
return a < b ? a : b;
}
int getSum(int arr[10]) {
int sum = 0;
for (int i = 0; i < numOfPeople/2; i++)
for (int j = 0; j < numOfPeople/2; j++)
sum += map[arr[i]][arr[j]];
return sum;
}