|
| 1 | +'''java |
| 2 | +import java.io.BufferedReader; |
| 3 | +import java.io.IOException; |
| 4 | +import java.io.InputStreamReader; |
| 5 | +import java.util.ArrayDeque; |
| 6 | +import java.util.Queue; |
| 7 | +import java.util.StringTokenizer; |
| 8 | + |
| 9 | +public class Main { |
| 10 | + static Queue<int[]> list = new ArrayDeque<>(); |
| 11 | + static int count, m, n, h; |
| 12 | + static int[][] dir = {{-1, 0, 0}, {1, 0, 0}, {0, -1, 0}, {0, 1, 0}, {0, 0, -1}, {0, 0, 1}}; |
| 13 | + static int[][][] box; |
| 14 | + static int day = -1; |
| 15 | + |
| 16 | + public static void main(String[] args) throws IOException { |
| 17 | + BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); |
| 18 | + StringTokenizer st = new StringTokenizer(br.readLine()); |
| 19 | + |
| 20 | + m = Integer.parseInt(st.nextToken()); |
| 21 | + n = Integer.parseInt(st.nextToken()); |
| 22 | + h = Integer.parseInt(st.nextToken()); |
| 23 | + |
| 24 | + box = new int[n][m][h]; |
| 25 | + |
| 26 | + for (int height = 0; height < h; height++) { |
| 27 | + for (int i = 0; i < n; i++) { |
| 28 | + st = new StringTokenizer(br.readLine()); |
| 29 | + for (int j = 0; j < m; j++) { |
| 30 | + box[i][j][height] = Integer.parseInt(st.nextToken()); |
| 31 | + if (box[i][j][height] == 0) count++; |
| 32 | + if (box[i][j][height] == 1) list.add(new int[]{i, j, height}); |
| 33 | + } |
| 34 | + } |
| 35 | + } |
| 36 | + |
| 37 | + tomato(); |
| 38 | + |
| 39 | + if (count == 0) |
| 40 | + System.out.println(day); |
| 41 | + else System.out.println("-1"); |
| 42 | + } |
| 43 | + |
| 44 | + static void tomato() { |
| 45 | + while (!list.isEmpty()) { |
| 46 | + int s = list.size(); |
| 47 | + for (int i = 0; i < s; i++) { |
| 48 | + int[] cur = list.poll(); |
| 49 | + int y = cur[0]; |
| 50 | + int x = cur[1]; |
| 51 | + int he = cur[2]; |
| 52 | + |
| 53 | + for (int d = 0; d < 6; d++) { |
| 54 | + int dy = y + dir[d][0]; |
| 55 | + int dx = x + dir[d][1]; |
| 56 | + int dh = he + dir[d][2]; |
| 57 | + |
| 58 | + if (dy < 0 || dy >= n || dx < 0 || dx >= m || dh < 0 || dh >= h) |
| 59 | + continue; |
| 60 | + |
| 61 | + if (box[dy][dx][dh] == 0) { |
| 62 | + count--; |
| 63 | + box[dy][dx][dh] = 1; |
| 64 | + list.add(new int[]{dy, dx, dh}); |
| 65 | + } |
| 66 | + } |
| 67 | + } |
| 68 | + day++; |
| 69 | + } |
| 70 | + } |
| 71 | +} |
| 72 | +''' |
0 commit comments