java 代码
import java.util.*;
import java.io.*;
public class Main{
static int N = 110;
static int L = -1,R = -1,C = -1;
static char[][][] g = new char[N][N][N];
static int[] dx = {1,-1,0,0,0,0};
static int[] dy = {0,0,1,-1,0,0};
static int[] dz = {0,0,0,0,1,-1};
static PII start = new PII();
static PII end = new PII();
static int time = -1;
public static void main(String[] args) throws IOException {
BufferedReader rd = new BufferedReader(new InputStreamReader(System.in));
String line = rd.readLine();
String[] str = line.split(" ");
L = Integer.parseInt(str[0]);
R = Integer.parseInt(str[1]);
C = Integer.parseInt(str[2]);
while(L != 0 && R != 0 && C != 0){
for(int i = 0;i < L;i++){
for(int j = 0;j < R;j++){
line = rd.readLine();
for(int k = 0;k < C;k++){
g[j][k][i] = line.charAt(k);
if(line.charAt(k) == 'S'){
start.x = j;
start.y = k;
start.z = i;
}
if(line.charAt(k) == 'E'){
end.x = j;
end.y = k;
end.z = i;
}
}
}
line = rd.readLine();
}
bfs();
if(time == -1){
System.out.println("Trapped!");
}else{
System.out.printf("Escaped in %d minute(s).\n",time);
}
line = rd.readLine();
str = line.split(" ");
time = -1;
L = Integer.parseInt(str[0]);
R = Integer.parseInt(str[1]);
C = Integer.parseInt(str[2]);
}
rd.close();
}
public static void bfs(){
int[][][] dist = new int[N][N][N];
for(int i = 0;i < R;i++){
for(int j = 0;j < C;j++){
Arrays.fill(dist[i][j],0);
}
}
Queue<PII> queue = new LinkedList<>();
queue.add(start);
while(!queue.isEmpty()){
PII temp = queue.poll();
for(int i = 0;i < 6;i++){
int x = temp.x + dx[i];
int y = temp.y + dy[i];
int z = temp.z + dz[i];
if(x >= 0 && x < R && y >= 0 && y < C && z >= 0 && z < L &&
g[x][y][z] == '.' && dist[x][y][z] == 0){
PII newTemp = new PII(x,y,z);
dist[newTemp.x][newTemp.y][newTemp.z]
= dist[temp.x][temp.y][temp.z] + 1;
queue.add(newTemp);
}
if(x >= 0 && x < R && y >= 0 && y < C && z >= 0 && z < L &&
g[x][y][z] == 'E' ){
time = dist[temp.x][temp.y][temp.z] + 1;
return;
}
}
}
}
}
class PII{
int x;
int y;
int z;
public PII(){
}
public PII(int x,int y,int z){
this.x = x;
this.y = y;
this.z = z;
}
}