1、数据量比较大,要用快读的方式,否则会tle
2、开一个map数组就可以了访问过了就置为#,防止重复走,如果是开一个st数组会超内存
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.LinkedList;
import java.util.Queue;
import java.util.Scanner;
public class Main
{
private static int l,r,c;
private static char[][][] map;
private static boolean[][][] st;//去掉
private static int sl,sx,sy,el,ex,ey;
public static void main(String[] args) throws IOException
{
BufferedReader read=new BufferedReader(new InputStreamReader(System.in));
while(true)
{
String s=read.readLine();
String[] split = s.split("\\s+");
l=Integer.valueOf(split[0]);
r=Integer.valueOf(split[1]);
c=Integer.valueOf(split[2]);
if(l==r&&r==c&&c==0)
break;
map=new char[l][r][c];
for(int d=0;d<l;d++)
{
for(int i=0;i<r;i++)
{
map[d][i]=read.readLine().toCharArray();
for(int j=0;j<map[d][i].length;j++)
{
if(map[d][i][j]=='S')
{
sl=d;
sx=i;
sy=j;
}
else if(map[d][i][j]=='E')
{
el=d;
ex=i;
ey=j;
}
}
}
//scan.nextLine();
read.readLine();
}
int t=bfs();
if(t==-1)
System.out.println("Trapped!");
else
System.out.println("Escaped in "+t+" minute(s).");
}
}
private static int bfs()
{
Queue<Node> queue=new LinkedList<>();
queue.offer(new Node(sl,sx,sy,0));
map[sl][sx][sy]='#';
int[] dx= {-1,0,1,0};
int[] dy= {0,1,0,-1};
while(!queue.isEmpty())
{
Node t=queue.poll();
int f=t.f,x=t.x,y=t.y,s=t.s;
if(f==el&&x==ex&&y==ey)
return s;
//在本层走 向北,向南,向东,向西
for(int i=0;i<4;i++)
{
int tx=x+dx[i];
int ty=y+dy[i];
if(tx>=0&&tx<r&&ty>=0&&ty<c&&map[f][tx][ty]!='#')
{
queue.offer(new Node(f,tx,ty,s+1));
map[f][tx][ty]='#';
}
}
//向下走
int tf=f-1;
if(tf>=0&&tf<l&&map[tf][x][y]!='#')
{
queue.offer(new Node(tf,x,y,s+1));
map[tf][x][y]='#';
}
//向上走
tf=f+1;
if(tf>=0&&tf<l&&map[tf][x][y]!='#')
{
queue.offer(new Node(tf,x,y,s+1));
map[tf][x][y]='#';
}
}
return -1;
}
private static class Node
{
private int f;
private int x;
private int y;
private int s;
public Node(int f,int x,int y,int s)
{
this.f=f;
this.x=x;
this.y=y;
this.s=s;
}
}
}