|
| 1 | +```java |
| 2 | +import java.io.*; |
| 3 | +import java.util.*; |
| 4 | + |
| 5 | +public class Main { |
| 6 | + |
| 7 | + static class Point { |
| 8 | + int l, r, c, time; |
| 9 | + |
| 10 | + public Point(int l, int r, int c, int time) { |
| 11 | + this.l = l; |
| 12 | + this.r = r; |
| 13 | + this.c = c; |
| 14 | + this.time = time; |
| 15 | + } |
| 16 | + } |
| 17 | + |
| 18 | + static int l, r, c; |
| 19 | + static char[][][] building; |
| 20 | + static boolean[][][] visited; |
| 21 | + static int[] dl = {0, 0, 0, 0, -1, 1}; |
| 22 | + static int[] dr = {0, 0, -1, 1, 0, 0}; |
| 23 | + static int[] dc = {-1, 1, 0, 0, 0, 0}; |
| 24 | + |
| 25 | + public static void main(String[] args) throws IOException { |
| 26 | + BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); |
| 27 | + StringBuilder sb = new StringBuilder(); |
| 28 | + |
| 29 | + while (true) { |
| 30 | + StringTokenizer st = new StringTokenizer(br.readLine()); |
| 31 | + l = Integer.parseInt(st.nextToken()); |
| 32 | + r = Integer.parseInt(st.nextToken()); |
| 33 | + c = Integer.parseInt(st.nextToken()); |
| 34 | + |
| 35 | + if (l == 0 && r == 0 && c == 0) { |
| 36 | + break; |
| 37 | + } |
| 38 | + |
| 39 | + building = new char[l][r][c]; |
| 40 | + visited = new boolean[l][r][c]; |
| 41 | + Point start = null; |
| 42 | + |
| 43 | + for (int l = 0; l < Main.l; l++) { |
| 44 | + for (int r = 0; r < Main.r; r++) { |
| 45 | + String line = br.readLine(); |
| 46 | + for (int c = 0; c < Main.c; c++) { |
| 47 | + building[l][r][c] = line.charAt(c); |
| 48 | + if (building[l][r][c] == 'S') { |
| 49 | + start = new Point(l, r, c, 0); |
| 50 | + } |
| 51 | + } |
| 52 | + } |
| 53 | + br.readLine(); |
| 54 | + } |
| 55 | + |
| 56 | + int result = bfs(start); |
| 57 | + |
| 58 | + if (result == -1) { |
| 59 | + sb.append("Trapped!\n"); |
| 60 | + } else { |
| 61 | + sb.append("Escaped in ").append(result).append(" minute(s).\n"); |
| 62 | + } |
| 63 | + } |
| 64 | + System.out.print(sb); |
| 65 | + } |
| 66 | + |
| 67 | + static int bfs(Point start) { |
| 68 | + Queue<Point> queue = new LinkedList<>(); |
| 69 | + queue.offer(start); |
| 70 | + visited[start.l][start.r][start.c] = true; |
| 71 | + |
| 72 | + while (!queue.isEmpty()) { |
| 73 | + Point cur = queue.poll(); |
| 74 | + |
| 75 | + if (building[cur.l][cur.r][cur.c] == 'E') { |
| 76 | + return cur.time; |
| 77 | + } |
| 78 | + |
| 79 | + for (int i = 0; i < 6; i++) { |
| 80 | + int nl = cur.l + dl[i]; |
| 81 | + int nr = cur.r + dr[i]; |
| 82 | + int nc = cur.c + dc[i]; |
| 83 | + |
| 84 | + if (nl >= 0 && nl < l && nr >= 0 && nr < r && nc >= 0 && nc < c) { |
| 85 | + if (!visited[nl][nr][nc] && building[nl][nr][nc] != '#') { |
| 86 | + visited[nl][nr][nc] = true; |
| 87 | + queue.offer(new Point(nl, nr, nc, cur.time + 1)); |
| 88 | + } |
| 89 | + } |
| 90 | + } |
| 91 | + } |
| 92 | + |
| 93 | + return -1; |
| 94 | + } |
| 95 | +} |
| 96 | +``` |
0 commit comments