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
#include <iostream>
#include <vector>
#include <limits.h>
#include <math.h>
 
using namespace std;
 
vector<vector<int>> power;
vector<bool> isContain;  //start팀에 포함됐는지 여부
 
int minVal = INT_MAX;
 
void findAnswer(int cnt, int start, const int n) {
    //start팀 고르기. 링크팀은 스타트팀 고르면 알아서 골라짐
    if (cnt == n/2) {  //다 골랐으면
        int startSum = 0;
        int linkSum = 0;
 
        for (int i = 0; i < n; i++) {
            for (int j = 0; j < n; j++) {
                if (i == j)
                    continue;
 
                if (isContain[i] && isContain[j])  //i,j가 모두 start 팀이면
                    startSum += power[i][j];
 
                else if (!isContain[i] && !isContain[j])  //i,j가 모두 link 팀이면
                    linkSum += power[i][j];
            }
        }
 
        int diff = abs(startSum - linkSum);
        if (minVal > diff)
            minVal = diff;
 
        return;
    }
 
    
    for (int i = start; i < n; i++) {
        isContain[i] = true;
        findAnswer(cnt + 1, i + 1, n);
        isContain[i] = false;
    }
}
 
int main() {
    int n;
    cin >> n;
 
    power = vector<vector<int>>(n, vector<int>(n));
    isContain = vector<bool>(n, false);
 
    for (int i = 0; i < n; i++) {
        for (int j = 0; j < n; j++) {
            cin >> power[i][j];
        }
    }
 
    findAnswer(00, n);
 
    cout << minVal;
    return 0;
}
http://colorscripter.com/info#e" target="_blank" style="color:#e5e5e5text-decoration:none">Colored by Color Scripter
http://colorscripter.com/info#e" target="_blank" style="text-decoration:none;color:white">cs

 

start팀을 고르면 link팀은 알아서 골라지므로 start만 고르면 된다

nCn/2 조합 문제 풀듯이

 

'Baekjoon' 카테고리의 다른 글

[#1003] 피보나치 함수  (0) 2020.03.10
[#2748] 피보나치 수 2  (0) 2020.03.10
[#14888] 연산자 끼워넣기  (0) 2020.03.10
[#2580] 스도쿠  (0) 2020.03.10
[#9663] N-Queen  (0) 2020.03.09

+ Recent posts