|
| 1 | +```java |
| 2 | +import java.io.*; |
| 3 | +import java.util.*; |
| 4 | + |
| 5 | +public class Main { |
| 6 | + static int n, m, h; |
| 7 | + static int[][][] box; |
| 8 | + static Queue<int[]> q = new LinkedList<>(); |
| 9 | + |
| 10 | + static int[] dx = {-1, 1, 0, 0, 0, 0}; |
| 11 | + static int[] dy = {0, 0, -1, 1, 0, 0}; |
| 12 | + static int[] dz = {0, 0, 0, 0, -1, 1}; |
| 13 | + |
| 14 | + public static void main(String[] args) throws Exception { |
| 15 | + BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); |
| 16 | + StringTokenizer st = new StringTokenizer(br.readLine()); |
| 17 | + |
| 18 | + m = Integer.parseInt(st.nextToken()); |
| 19 | + n = Integer.parseInt(st.nextToken()); |
| 20 | + h = Integer.parseInt(st.nextToken()); |
| 21 | + |
| 22 | + box = new int[h][n][m]; |
| 23 | + |
| 24 | + int remain = 0; |
| 25 | + |
| 26 | + for (int z = 0; z < h; z++) { |
| 27 | + for (int x = 0; x < n; x++) { |
| 28 | + st = new StringTokenizer(br.readLine()); |
| 29 | + for (int y = 0; y < m; y++) { |
| 30 | + int v = Integer.parseInt(st.nextToken()); |
| 31 | + box[z][x][y] = v; |
| 32 | + |
| 33 | + if (v == 1) q.offer(new int[]{z, x, y}); |
| 34 | + else if (v == 0) remain++; |
| 35 | + } |
| 36 | + } |
| 37 | + } |
| 38 | + |
| 39 | + if (remain == 0) { |
| 40 | + System.out.println(0); |
| 41 | + return; |
| 42 | + } |
| 43 | + |
| 44 | + int day = 0; |
| 45 | + |
| 46 | + while (!q.isEmpty()) { |
| 47 | + int size = q.size(); |
| 48 | + for (int i = 0; i < size; i++) { |
| 49 | + int[] cur = q.poll(); |
| 50 | + int cz = cur[0], cx = cur[1], cy = cur[2]; |
| 51 | + |
| 52 | + for (int dir = 0; dir < 6; dir++) { |
| 53 | + int nz = cz + dz[dir]; |
| 54 | + int nx = cx + dx[dir]; |
| 55 | + int ny = cy + dy[dir]; |
| 56 | + |
| 57 | + if (!isOk(nz, nx, ny)) continue; |
| 58 | + if (box[nz][nx][ny] != 0) continue; |
| 59 | + |
| 60 | + box[nz][nx][ny] = 1; |
| 61 | + remain--; |
| 62 | + q.offer(new int[]{nz, nx, ny}); |
| 63 | + } |
| 64 | + } |
| 65 | + |
| 66 | + day++; |
| 67 | + |
| 68 | + if (remain == 0) { |
| 69 | + System.out.println(day); |
| 70 | + return; |
| 71 | + } |
| 72 | + } |
| 73 | + |
| 74 | + System.out.println(-1); |
| 75 | + } |
| 76 | + |
| 77 | + static boolean isOk(int z, int x, int y) { |
| 78 | + return !(z < 0 || z >= h || x < 0 || x >= n || y < 0 || y >= m); |
| 79 | + } |
| 80 | +} |
| 81 | + |
| 82 | +``` |
0 commit comments