Skip to content

Commit 8bcc55f

Browse files
authored
[20251124] BOJ / G2 / 마피아 / 이종환
1 parent a77f199 commit 8bcc55f

File tree

1 file changed

+109
-0
lines changed

1 file changed

+109
-0
lines changed

0224LJH/202511/24 BOJ 마피아.md

Lines changed: 109 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,109 @@
1+
```java
2+
import java.io.BufferedReader;
3+
import java.io.IOException;
4+
import java.io.InputStreamReader;
5+
import java.util.Arrays;
6+
import java.util.StringTokenizer;
7+
8+
public class Main {
9+
static BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
10+
static int total,target,ans;
11+
static int[] points;
12+
static int[][] arr;
13+
static boolean[] alive;
14+
15+
public static void main(String[] args) throws IOException {
16+
17+
init();
18+
process();
19+
print();
20+
21+
}
22+
23+
private static void init() throws IOException{
24+
total = Integer.parseInt(br.readLine());
25+
points = new int[total];
26+
alive = new boolean[total];
27+
arr = new int[total][total];
28+
ans = 0;
29+
30+
Arrays.fill(alive, true);
31+
32+
StringTokenizer st = new StringTokenizer(br.readLine());
33+
for (int i = 0; i < total; i++) {
34+
points[i] = Integer.parseInt(st.nextToken());
35+
}
36+
37+
for (int i = 0; i < total; i++) {
38+
st = new StringTokenizer(br.readLine());
39+
for (int j = 0; j < total; j++) {
40+
arr[i][j] = Integer.parseInt(st.nextToken());
41+
}
42+
}
43+
44+
target = Integer.parseInt(br.readLine());
45+
}
46+
47+
private static void process() throws IOException {
48+
49+
processDay(0,total);
50+
}
51+
52+
private static void processDay(int nightCnt, int cnt) {
53+
if (cnt%2 == 0) {
54+
for (int i = 0; i < total; i++) {
55+
if ( i != target && alive[i]) {
56+
// 임의로 한명 죽임
57+
alive[i] = false;
58+
for (int j = 0; j < total; j++) {
59+
points[j] += arr[i][j];
60+
}
61+
processDay(nightCnt+1,cnt-1);
62+
63+
//원상복구
64+
alive[i] = true;
65+
for (int j = 0; j < total;j++) {
66+
points[j] -= arr[i][j];
67+
}
68+
69+
}
70+
}
71+
} else {
72+
if (cnt == 1) {
73+
ans = Math.max(ans, nightCnt);
74+
return;
75+
}
76+
77+
int highest = Integer.MIN_VALUE;
78+
int highestIdx = -1;
79+
for (int i = 0; i < total; i++) {
80+
if (!alive[i]) continue;
81+
if (points[i] > highest) {
82+
highest = points[i];
83+
highestIdx = i;
84+
}
85+
}
86+
87+
if (highestIdx == target) {
88+
ans = Math.max(ans, nightCnt);
89+
return;
90+
}
91+
92+
alive[highestIdx] = false;
93+
processDay(nightCnt,cnt-1);
94+
alive[highestIdx] = true;
95+
96+
}
97+
98+
}
99+
100+
101+
102+
103+
104+
private static void print() {
105+
System.out.println(ans);
106+
}
107+
108+
}
109+
```

0 commit comments

Comments
 (0)