|
| 1 | +```java |
| 2 | +import java.io.*; |
| 3 | +import java.util.*; |
| 4 | + |
| 5 | +public class boj1079 { |
| 6 | + static BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); |
| 7 | + static StringTokenizer st; |
| 8 | + static void nextLine() throws Exception { st = new StringTokenizer(br.readLine()); } |
| 9 | + static int nextInt() { return Integer.parseInt(st.nextToken()); } |
| 10 | + |
| 11 | + static boolean[] isDead; |
| 12 | + static int N, mafia, answer = 0; |
| 13 | + static int[][] R; |
| 14 | + static int[] guilty; |
| 15 | + public static void main(String[] args) throws Exception { |
| 16 | + nextLine(); |
| 17 | + N = nextInt(); |
| 18 | + isDead = new boolean[N]; |
| 19 | + guilty = new int[N]; |
| 20 | + mafia = -1; |
| 21 | + R = new int[N][N]; |
| 22 | + nextLine(); |
| 23 | + for (int i = 0; i < N; i++) guilty[i] = nextInt(); |
| 24 | + for (int i = 0; i < N; i++) { |
| 25 | + nextLine(); |
| 26 | + for (int j = 0; j < N; j++) { |
| 27 | + R[i][j] = nextInt(); |
| 28 | + } |
| 29 | + } |
| 30 | + nextLine(); |
| 31 | + mafia = nextInt(); |
| 32 | + dfs(N, 0); |
| 33 | + |
| 34 | + System.out.println(answer); |
| 35 | + } |
| 36 | + |
| 37 | + static void dfs(int alive, int nights) { |
| 38 | + if (isDead[mafia] || alive == 1) { |
| 39 | + answer = Math.max(answer, nights); |
| 40 | + return; |
| 41 | + } |
| 42 | + if (alive % 2 == 0) { // 밤 |
| 43 | + for (int i = 0; i < N; i++) { |
| 44 | + if (isDead[i] || i == mafia) continue; |
| 45 | + isDead[i] = true; |
| 46 | + changeGuilty(1, i); |
| 47 | + dfs(alive-1, nights+1); |
| 48 | + changeGuilty(-1, i); |
| 49 | + isDead[i] = false; |
| 50 | + } |
| 51 | + } else { // 낮 |
| 52 | + int maxGuilty = Integer.MIN_VALUE; |
| 53 | + int idx = -1; |
| 54 | + for (int i = 0; i < N; i++) { |
| 55 | + if (isDead[i] || guilty[i] <= maxGuilty) continue; |
| 56 | + maxGuilty = guilty[i]; |
| 57 | + idx = i; |
| 58 | + } |
| 59 | + isDead[idx] = true; |
| 60 | + dfs(alive-1, nights); |
| 61 | + isDead[idx] = false; |
| 62 | + } |
| 63 | + } |
| 64 | + |
| 65 | + static void changeGuilty(int type, int idx) { |
| 66 | + for (int i = 0; i < N; i++) { |
| 67 | + if (isDead[i]) continue; |
| 68 | + guilty[i] += type * R[idx][i]; |
| 69 | + } |
| 70 | + } |
| 71 | + } |
| 72 | +``` |
0 commit comments