본문 바로가기

브루트 포스/브루트포스(비트마스크)

스타트와 링크 with 비트마스크

https://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
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
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
int s[20][20];
int n;
 
int main() {
    cin >> n;
    for (int i = 0; i < n; i++) {
        for (int j = 0; j < n; j++) {
            cin >> s[i][j];
        }
    }
    int ans = -1;
    for (int i = 0; i < (1 << n); i++)
    {
        vector<int> first, second;
 
        //1번팀 0번팀
        for (int j = 0; j < n; j++)
        {
            if (i & (1 << j))
                first.push_back(j);
            else
                second.push_back(j);
        }
 
        if (first.size() != n / 2continue;
        int t1 = 0;
        int t2 = 0;
        for (int l1 = 0; l1 < n / 2; l1++
        {
            for (int l2 = 0; l2 < n / 2; l2++
            {
                if (l1 == l2) continue;
                t1 += s[first[l1]][first[l2]];
                t2 += s[second[l1]][second[l2]];
            }
        }
 
        int diff = t1 - t2;
        if (diff < 0) diff = -diff;
        if (ans == -1 || ans > diff)
            ans = diff;
    }
    
    cout << ans << '\n';
}
cs

'브루트 포스 > 브루트포스(비트마스크)' 카테고리의 다른 글

구슬 탈출 2 (중요!!)  (0) 2023.09.21
가르침 (어렵다..)  (0) 2023.09.16
종이 조각 (중요!!!)  (0) 2023.03.03
부분 집합의 합  (0) 2023.03.01
비트마스크  (0) 2023.02.25