Skip to content

Commit bd092f8

Browse files
authored
[20250923] BOJ / G5 / 토마토 / 이인희
1 parent 4605376 commit bd092f8

File tree

1 file changed

+64
-0
lines changed

1 file changed

+64
-0
lines changed
Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
```java
2+
import java.io.*;
3+
import java.util.*;
4+
5+
public class Main {
6+
public static void main(String[] args) throws IOException {
7+
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
8+
StringTokenizer st = new StringTokenizer(br.readLine());
9+
int m = Integer.parseInt(st.nextToken());
10+
int n = Integer.parseInt(st.nextToken());
11+
int h = Integer.parseInt(st.nextToken());
12+
int[][][] box = new int[h][n][m];
13+
Queue<int[]> q = new ArrayDeque<>();
14+
15+
16+
for (int k = 0; k < h; k++) {
17+
for (int i = 0; i < n; i++) {
18+
st = new StringTokenizer(br.readLine());
19+
for (int j = 0; j < m; j++) {
20+
box[k][i][j] = Integer.parseInt(st.nextToken());
21+
if (box[k][i][j] == 1) {
22+
q.add(new int[]{k, i, j});
23+
}
24+
}
25+
}
26+
}
27+
28+
int[] dz = {0, 0, 0, 0, 1, -1};
29+
int[] dx = {1, -1, 0, 0, 0, 0};
30+
int[] dy = {0, 0, 1, -1, 0, 0};
31+
int days = -1;
32+
while (!q.isEmpty()) {
33+
int size = q.size();
34+
for (int s = 0; s < size; s++) {
35+
int[] cur = q.poll();
36+
int z = cur[0], x = cur[1], y = cur[2];
37+
for (int d = 0; d < 6; d++) {
38+
int nz = z + dz[d], nx = x + dx[d], ny = y + dy[d];
39+
if (nz < 0 ||nz >= h|| nx < 0 || nx >= n || ny < 0 || ny >= m)
40+
continue;
41+
if (box[nz][nx][ny] == 0) {
42+
box[nz][nx][ny] = 1;
43+
q.add(new int[]{nz, nx, ny});
44+
}
45+
}
46+
}
47+
days++;
48+
}
49+
50+
for (int z = 0; z < h; z++) {
51+
for (int x = 0; x < n; x++) {
52+
for (int y = 0; y < m; y++) {
53+
if (box[z][x][y] == 0) {
54+
System.out.println(-1);
55+
return;
56+
}
57+
}
58+
}
59+
}
60+
System.out.println(days);
61+
}
62+
}
63+
64+
```

0 commit comments

Comments
 (0)