|
| 1 | +```java |
| 2 | +import java.util.*; |
| 3 | +import java.io.*; |
| 4 | + |
| 5 | +public class boj6593 { |
| 6 | + static BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); |
| 7 | + static StringTokenizer st; |
| 8 | + static void nextLine() throws Exception {st = new StringTokenizer(br.readLine());} |
| 9 | + static int nextInt() {return Integer.parseInt(st.nextToken());} |
| 10 | + |
| 11 | + static Pos start, end; |
| 12 | + static int L, R, C, answer; |
| 13 | + static char[][][] map; |
| 14 | + static int[][] dir = {{1,0,0}, {-1,0,0}, {0,1,0}, {0,-1,0}, {0,0,1}, {0,0,-1}}; |
| 15 | + public static void main(String[] args) throws Exception { |
| 16 | + while (true) { |
| 17 | + nextLine(); |
| 18 | + answer = Integer.MAX_VALUE; |
| 19 | + L = nextInt(); |
| 20 | + R = nextInt(); |
| 21 | + C = nextInt(); |
| 22 | + if (L == 0 && R == 0 && C == 0) return; |
| 23 | + map = new char[L][R][C]; |
| 24 | + for (int l = 0; l < L; l++) { |
| 25 | + for (int r = 0; r < R; r++) { |
| 26 | + String str = br.readLine(); |
| 27 | + for (int c = 0; c < C; c++) { |
| 28 | + map[l][r][c] = str.charAt(c); |
| 29 | + if (map[l][r][c] == 'S') start = new Pos(l, r, c, 0); |
| 30 | + else if (map[l][r][c] == 'E') end = new Pos(l, r, c, 0); |
| 31 | + } |
| 32 | + } |
| 33 | + br.readLine(); |
| 34 | + } |
| 35 | + bfs(); |
| 36 | + if (answer != Integer.MAX_VALUE) System.out.println("Escaped in "+answer+" minute(s)."); |
| 37 | + else System.out.println("Trapped!"); |
| 38 | + } |
| 39 | + } |
| 40 | + |
| 41 | + static void bfs() { |
| 42 | + boolean[][][] visited = new boolean[L][R][C]; |
| 43 | + Queue<Pos> q = new LinkedList<>(); |
| 44 | + q.add(start); |
| 45 | + while (!q.isEmpty()) { |
| 46 | + Pos cur = q.poll(); |
| 47 | + if ((cur.h == end.h) && (cur.r == end.r) && (cur.c == end.c)) { |
| 48 | + answer = Math.min(answer, cur.cnt); |
| 49 | + continue; |
| 50 | + } |
| 51 | + for (int[] d : dir) { |
| 52 | + int nh = cur.h + d[0]; |
| 53 | + int nr = cur.r + d[1]; |
| 54 | + int nc = cur.c + d[2]; |
| 55 | + if (nh < 0 || nh >= L || nr < 0 || nr >= R || nc < 0 || nc >= C) continue; |
| 56 | + if (visited[nh][nr][nc] || map[nh][nr][nc] == '#') continue; |
| 57 | + q.add(new Pos(nh, nr, nc, cur.cnt+1)); |
| 58 | + visited[nh][nr][nc] = true; |
| 59 | + } |
| 60 | + } |
| 61 | + } |
| 62 | + |
| 63 | + static class Pos { |
| 64 | + int h, r, c, cnt; |
| 65 | + public Pos(int h, int r, int c, int cnt) { |
| 66 | + this.h = h; |
| 67 | + this.r = r; |
| 68 | + this.c = c; |
| 69 | + this.cnt = cnt; |
| 70 | + } |
| 71 | + } |
| 72 | +} |
| 73 | +``` |
0 commit comments