Skip to content

Commit 1278bd8

Browse files
authored
Merge pull request #615 from AlgorithmWithGod/JHLEE325
[20250806] BOJ / G4 / 연구소 / 이준희
2 parents a82c477 + 000dd1e commit 1278bd8

File tree

1 file changed

+99
-0
lines changed

1 file changed

+99
-0
lines changed
Lines changed: 99 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,99 @@
1+
```java
2+
import java.io.*;
3+
import java.util.*;
4+
5+
public class Main {
6+
7+
static int n, m;
8+
static int[][] board;
9+
static int maxSafe = 0;
10+
static int[][] dir = {{-1, 0}, {1, 0}, {0, -1}, {0, 1}};
11+
12+
public static void main(String[] args) throws Exception {
13+
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
14+
StringTokenizer st;
15+
16+
st = new StringTokenizer(br.readLine());
17+
n = Integer.parseInt(st.nextToken());
18+
m = Integer.parseInt(st.nextToken());
19+
20+
board = new int[n][m];
21+
List<Integer> empties = new ArrayList<>();
22+
List<Integer> viruses = new ArrayList<>();
23+
24+
for (int r = 0; r < n; r++) {
25+
st = new StringTokenizer(br.readLine());
26+
for (int c = 0; c < m; c++) {
27+
board[r][c] = Integer.parseInt(st.nextToken());
28+
if (board[r][c] == 0) empties.add(r * m + c);
29+
else if (board[r][c] == 2) viruses.add(r * m + c);
30+
}
31+
}
32+
33+
int E = empties.size();
34+
35+
for (int i = 0; i < E; i++) {
36+
for (int j = i + 1; j < E; j++) {
37+
for (int k = j + 1; k < E; k++) {
38+
int[][] tmp = copyBoard(board);
39+
40+
placeWall(tmp, empties.get(i));
41+
placeWall(tmp, empties.get(j));
42+
placeWall(tmp, empties.get(k));
43+
44+
spreadVirus(tmp, viruses);
45+
46+
int safe = countSafe(tmp);
47+
if (safe > maxSafe) maxSafe = safe;
48+
}
49+
}
50+
}
51+
52+
System.out.println(maxSafe);
53+
}
54+
55+
static int[][] copyBoard(int[][] src) {
56+
int[][] dst = new int[n][m];
57+
for (int r = 0; r < n; r++) {
58+
System.arraycopy(src[r], 0, dst[r], 0, m);
59+
}
60+
return dst;
61+
}
62+
63+
static void placeWall(int[][] g, int pos) {
64+
int r = pos / m, c = pos % m;
65+
g[r][c] = 1;
66+
}
67+
68+
static void spreadVirus(int[][] g, List<Integer> viruses) {
69+
ArrayDeque<int[]> q = new ArrayDeque<>();
70+
for (int v : viruses) {
71+
int vr = v / m, vc = v % m;
72+
q.add(new int[]{vr, vc});
73+
}
74+
while (!q.isEmpty()) {
75+
int[] cur = q.poll();
76+
int y = cur[0], x = cur[1];
77+
for (int d = 0; d < 4; d++) {
78+
int dy = y + dir[d][0], dx = x + dir[d][1];
79+
if (dy < 0 || dy >= n || dx < 0 || dx >= m) continue;
80+
if (g[dy][dx] == 0) {
81+
g[dy][dx] = 2;
82+
q.add(new int[]{dy, dx});
83+
}
84+
}
85+
}
86+
}
87+
88+
static int countSafe(int[][] g) {
89+
int cnt = 0;
90+
for (int r = 0; r < n; r++) {
91+
for (int c = 0; c < m; c++) {
92+
if (g[r][c] == 0) cnt++;
93+
}
94+
}
95+
return cnt;
96+
}
97+
}
98+
99+
```

0 commit comments

Comments
 (0)