1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68
| import java.util.*;
class PIIs { public int x; public int y; public int z; public int dist; public PIIs(int x,int y,int z, int dist) { this.x = x; this.y = y; this.z = z; this.dist = dist; } }
public class Main { static PIIs start; static PIIs end; static int L, R, C; static int N = 100; static char[][][] g = new char[N][N][N]; static final int[] dx = {-1, 1, 0, 0, 0, 0}; static final int[] dy = {0, 0, -1, 1, 0, 0}; static final int[] dz = {0, 0, 0, 0, -1, 1}; public static void main(String[] args) { Scanner scan = new Scanner(System.in); while(scan.hasNext()) { L = scan.nextInt(); R = scan.nextInt(); C = scan.nextInt(); if(L == 0 && R == 0 && C == 0) break; for(int i = 0;i < L; i ++) { for(int j = 0;j < R; j++) { char[] charArray = scan.next().toCharArray(); for(int k = 0; k < C; k++) { g[i][j][k] = charArray[k]; if(g[i][j][k] == 'S') start = new PIIs(i, j, k, 0); if(g[i][j][k] == 'E') end = new PIIs(i, j, k, 0); } } } int distance = bfs(); if(distance == -1) System.out.println("Trapped!"); else System.out.println("Escaped in " + distance + " minute(s)."); } }
public static int bfs() { Queue<PIIs> quene = new LinkedList<PIIs>(); quene.add(start); while (!quene.isEmpty()) { PIIs piIs = quene.poll(); if (piIs.x == end.x && piIs.y == end.y && piIs.z == end.z) return piIs.dist; for (int i = 0; i < 6; i++) { int x = piIs.x + dx[i]; int y = piIs.y + dy[i]; int z = piIs.z + dz[i]; if (x < 0 || x >= L || y < 0 || y >= R || z < 0 || z >= C || g[x][y][z] == '#') { continue; } g[x][y][z] = '#'; quene.offer(new PIIs(x, y, z, piIs.dist + 1)); } } return -1; } }
|