반응형

www.acmicpc.net/problem/14889

 

14889번: 스타트와 링크

예제 2의 경우에 (1, 3, 6), (2, 4, 5)로 팀을 나누면 되고, 예제 3의 경우에는 (1, 2, 4, 5), (3, 6, 7, 8)로 팀을 나누면 된다.

www.acmicpc.net

 
1
2
3
4
5
6
7
8
9
10
11
12
13
void dfs(int now, int cnt) {
    if (cnt == n / 2) {
        // 로직 처리
        return;
    }
 
    // 조합 찾기(n개 중 r개)
    for (int i = now; i < n; i++) {
        visited[i] = true;
        dfs(i + 1, cnt + 1);
        visited[i] = false;
    }
}
cs

 

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
#include <iostream>
#include <algorithm>
using namespace std;
int n;
int arr[21][21];
int visited[20];
int min_gap = 987654321;
void dfs(int now, int cnt) {
    if (cnt == n / 2) {
        int score1 = 0;
        int score2 = 0;
        for (int i = 0; i < n; i++) {
            for (int j = i + 1; j < n; j++) {
                // 선택됨 -> 팀1
                if (visited[i] && visited[j])
                    score1 += (arr[i][j] + arr[j][i]);
                // 선택안됨 -> 팀2
                else if (!visited[i] && !visited[j])
                    score2 += (arr[i][j] + arr[j][i]);
            }
        }
        min_gap = min(min_gap, abs(score1 - score2));
        return;
    }
 
    // 조합 찾기(n개 중 r개)
    for (int i = now; i < n; i++) {
        visited[i] = true;
        dfs(i + 1, cnt + 1);
        visited[i] = false;
    }
}
int main() {
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    cin >> n;
    
    for (int i = 0; i < n; i++) {
        for (int j = 0; j < n; j++
            cin >> arr[i][j];
    }
    
    dfs(00);
    cout << min_gap;
}
cs
반응형

'백준 > 삼성기출' 카테고리의 다른 글

백준 14500  (0) 2021.02.13
백준 15686  (0) 2021.02.05
백준 14502  (0) 2021.02.03
백준 15684  (0) 2021.02.03
백준 14888  (0) 2021.01.28

+ Recent posts