#include<stdio.h>
#include<string.h>
#include<algorithm>
#include<queue>
using namespace std;
int k,n,m;
char s[40][40][40];
int use[40][40][40];
int dx[]= {0,0,1,-1,0,0};
int dy[]= {1,-1,0,0,0,0};
int dz[]= {0,0,0,0,1,-1};
struct data
{
int x,y,z,t;
} t1,t0;
queue<data>q;
int bfs(int sx,int sy,int sz)
{
int i,j,flag=0;
while(!q.empty()) q.pop();
t0.x=sx;
t0.y=sy;
t0.z=sz;
t0.t=0;
q.push(t0);
while(!q.empty())
{
t1=q.front();
q.pop();
if(s[t1.z][t1.x][t1.y]==‘E‘)
{
flag=t1.t;
break;
}
for(i=0; i<6; i++)
{
t0.x=t1.x+dx[i];
t0.y=t1.y+dy[i];
t0.z=t1.z+dz[i];
t0.t=t1.t+1;
if(t0.x<0||t0.x>=n||t0.y<0||t0.y>=m||t0.z<0||t0.z>=k) continue;
if(s[t0.z][t0.x][t0.y]==‘#‘) continue;
if(t0.t<use[t0.z][t0.x][t0.y]||use[t0.z][t0.x][t0.y]==0)
{
q.push(t0);
use[t0.z][t0.x][t0.y]=t0.t;
}
}
}
return flag;
}
int main()
{
int i,j,p,sx,sy,sz;
while(~scanf("%d%d%d",&k,&n,&m))
{
if(k==0&&n==0&&m==0) break;
//getchar();
for(p=0; p<k; p++)
{
for(i=0; i<n; i++)
{
scanf("%s",s[p][i]);
for(j=0; j<n; j++)
{
if(s[p][i][j]==‘S‘)
{
sx=i;sy=j;sz=p;
}
}
//getchar();
}
//getchar();
}
memset(use,0,sizeof(use));
int f=bfs(sx,sy,sz);
if(f>0) printf("Escaped in %d minute(s).\n",f);
else printf("Trapped!\n");
}
return 0;
}
版权声明:本文为博主原创文章,未经博主允许不得转载。
原文:http://blog.csdn.net/xinag578/article/details/46944523