输入样例:
3 4 5
S....
.###.
.##..
###.#
#####
#####
##.##
##...
#####
#####
#.###
####E
1 3 3
S##
#E#
###
0 0 0
输出样例:
Escaped in 11 minute(s).
Trapped!
#include<bits/stdc++.h>
using namespace std;
const int N=100+5;
int l,n,m,sx,sy,sz,ex,ey,ez;
char ch[N][N][N];
int vis[N][N][N];
struct node{
int x,y,z,w;
};
int dir[6][3]={
1,0,0,
-1,0,0,
0,1,0,
0,-1,0,
0,0,1,
0,0,-1
};
int check(int x,int y,int z){
return x>0&&y>0&&z>0&&x<=l&&y<=n&&z<=m;
}
void bfs(){
memset(vis,0,sizeof vis);
queue<node>q;
node start={sx,sy,sz,0};
node next;
vis[start.x][start.y][start.z]=1;
q.push(start);
while(q.size()){
start=q.front();
q.pop();
if(start.x==ex&&start.y==ey&&start.z==ez){
printf("Escaped in %d minute(s).\n",start.w);
return;
}
for(int i=0;i<6;i++){
next.x=start.x+dir[i][0];
next.y=start.y+dir[i][1];
next.z=start.z+dir[i][2];
if(check(next.x,next.y,next.z)&&ch[next.x][next.y][next.z]!='#'&&!vis[next.x][next.y][next.z]){
next.w=start.w+1;
vis[next.x][next.y][next.z]=1;
q.push(next);
}
}
}
cout<<"Trapped!"<<endl;
}
int main(){
while(cin>>l>>n>>m){
if(l==0&&n==0&&m==0) break;
for(int i=1;i<=l;i++){
for(int j=1;j<=n;j++){
for(int k=1;k<=m;k++){
cin>>ch[i][j][k];
if(ch[i][j][k]=='S') sx=i,sy=j,sz=k;
else if(ch[i][j][k]=='E') ex=i,ey=j,ez=k;
}
}
}
bfs();
}
return 0;
}