|
| 1 | +```java |
| 2 | +import java.io.*; |
| 3 | +import java.util.ArrayDeque; |
| 4 | +import java.util.Queue; |
| 5 | +import java.util.StringTokenizer; |
| 6 | + |
| 7 | +public class BJ_G5_상범_빌딩 { |
| 8 | + |
| 9 | + private static final int[] dx = { 0, 1, 0, -1, 0, 0 }; |
| 10 | + private static final int[] dy = { 1, 0, -1, 0, 0, 0 }; |
| 11 | + private static final int[] dz = { 0, 0, 0, 0, 1, -1 }; |
| 12 | + |
| 13 | + private static final BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); |
| 14 | + private static final BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(System.out)); |
| 15 | + private static final StringBuilder sb = new StringBuilder(); |
| 16 | + private static StringTokenizer st; |
| 17 | + |
| 18 | + private static int L, R, C; // 층 행 열 |
| 19 | + private static char[][][] building; |
| 20 | + private static boolean[][][] visited; |
| 21 | + private static Queue<Node> q; |
| 22 | + |
| 23 | + private static class Node { |
| 24 | + int floor; |
| 25 | + int x; |
| 26 | + int y; |
| 27 | + int cnt; |
| 28 | + |
| 29 | + Node(int floor, int x, int y, int cnt) { |
| 30 | + this.floor = floor; |
| 31 | + this.x = x; |
| 32 | + this.y = y; |
| 33 | + this.cnt = cnt; |
| 34 | + } |
| 35 | + |
| 36 | + } |
| 37 | + |
| 38 | + public static void main(String[] args) throws IOException { |
| 39 | + while (init()) { |
| 40 | + sol(); |
| 41 | + } |
| 42 | + bw.write(sb.toString()); |
| 43 | + bw.flush(); |
| 44 | + bw.close(); |
| 45 | + br.close(); |
| 46 | + } |
| 47 | + |
| 48 | + private static boolean init() throws IOException { |
| 49 | + st = new StringTokenizer(br.readLine()); |
| 50 | + L = Integer.parseInt(st.nextToken()); |
| 51 | + R = Integer.parseInt(st.nextToken()); |
| 52 | + C = Integer.parseInt(st.nextToken()); |
| 53 | + |
| 54 | + if (L == 0) return false; |
| 55 | + |
| 56 | + building = new char[L][R][C]; |
| 57 | + visited = new boolean[L][R][C]; |
| 58 | + q = new ArrayDeque<>(); |
| 59 | + for (int i = 0; i < L; i++) { |
| 60 | + for (int j = 0; j < R; j++) { |
| 61 | + String line = br.readLine(); |
| 62 | + for (int k = 0; k < C; k++) { |
| 63 | + building[i][j][k] = line.charAt(k); |
| 64 | + |
| 65 | + if (building[i][j][k] == 'S') { |
| 66 | + q.offer(new Node(i, j, k, 0)); |
| 67 | + visited[i][j][k] = true; |
| 68 | + } |
| 69 | + } |
| 70 | + } |
| 71 | + br.readLine(); |
| 72 | + } |
| 73 | + return true; |
| 74 | + } |
| 75 | + |
| 76 | + private static void sol() { |
| 77 | + while (!q.isEmpty()) { |
| 78 | + Node cur = q.poll(); |
| 79 | + |
| 80 | + if (building[cur.floor][cur.x][cur.y] == 'E') { |
| 81 | + sb.append("Escaped in ").append(cur.cnt).append(" minute(s).\n"); |
| 82 | + return; |
| 83 | + } |
| 84 | + |
| 85 | + for (int d = 0; d < 6; d++) { |
| 86 | + int nz = cur.floor + dz[d]; |
| 87 | + int nx = cur.x + dx[d]; |
| 88 | + int ny = cur.y + dy[d]; |
| 89 | + |
| 90 | + if (OOB(nz, nx, ny) || visited[nz][nx][ny] || building[nz][nx][ny] == '#') continue; |
| 91 | + |
| 92 | + q.offer(new Node(nz, nx, ny, cur.cnt + 1)); |
| 93 | + visited[nz][nx][ny] = true; |
| 94 | + } |
| 95 | + } |
| 96 | + sb.append("Trapped!\n"); |
| 97 | + } |
| 98 | + |
| 99 | + private static boolean OOB(int floor, int x, int y) { |
| 100 | + return floor < 0 || L <= floor || x < 0 || R <= x || y < 0 || C <= y; |
| 101 | + } |
| 102 | + |
| 103 | +} |
| 104 | +``` |
0 commit comments