티스토리 뷰

PS/boj

boj)15661 - 링크와 스타트

kingsubin 2020. 12. 6. 16:08
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
import java.io.*;
import java.util.*;
 
public class boj_15661 {
    static BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
    static StringTokenizer st;
    static int N;
    static int min = Integer.MAX_VALUE;
    static int[][] map;
    static ArrayList<Integer> team1 = new ArrayList<>();
    static ArrayList<Integer> team2 = new ArrayList<>();
 
    public static void main(String[] args) throws IOException {
        N = Integer.parseInt(br.readLine());
        map = new int[N][N];
        for (int i = 0; i < N; i++) {
            st = new StringTokenizer(br.readLine());
            for (int j = 0; j < N; j++) {
                map[i][j] = Integer.parseInt(st.nextToken());
            }
        }
 
        func(0);
        System.out.println(min);
    }
 
    static void func(int k) {
        if (k == N) {
            if (team1.size() > 0 && team2.size() > 0) {
                int t1 = 0;
                int t2 = 0;
 
                t1 = getT(t1, team1);
                t2 = getT(t2, team2);
 
                int diff = Math.abs(t1 - t2);
                min = Math.min(min, diff);
            }
            return;
        }
 
        team1.add(k);
        func(k+1);
        team1.remove(team1.size()-1);
 
        team2.add(k);
        func(k+1);
        team2.remove(team2.size()-1);
    }
 
    static int getT(int t, ArrayList<Integer> team) {
        for (int i = 0; i < team.size(); i++) {
            for (int j = 0; j < team.size(); j++) {
                if (i == j) continue;
                t += map[team.get(i)][team.get(j)];
            }
        }
        return t;
    }
}
 
cs

 

- 브루트포스, 재귀 

- www.acmicpc.net/problem/14889 문제와 유사함

 

- N번 까지 team1 에 추가할지 team2 에 추가할지 선택

- 모든 경우의 수에 대해 차이 구하고 min 갱신

 

 

 

 


www.acmicpc.net/problem/15661

'PS > boj' 카테고리의 다른 글

boj)14391 - 종이 조각  (0) 2020.12.09
boj)2529 - 부등호  (0) 2020.12.08
boj)14889 - 스타트와 링크  (0) 2020.12.03
boj)14501 - 퇴사  (0) 2020.12.02
boj)1759 - 암호 만들기  (0) 2020.12.01