#include<iostream>
#include<cstring>
#include<stdlib.h>
#include<algorithm>
#include<cmath>
#include<queue>
using namespace std;
int l,r,c;
char m[31][31][31];
bool book[31][31][31];
typedef struct node
{
int x,y,z,dis;
}N;
int sx,sy,sz;
int ex,ey,ez;
int dx[6]={-1,0,1,0,0,0};
int dy[6]={0,-1,0,1,0,0};
int dz[6]={0,0,0,0,-1,1};
int bfs()
{
queue<N> q;
N firstn;
firstn.x=sx;
firstn.y=sy;
firstn.z=sz;
firstn.dis=0;
q.push(firstn);
while(!q.empty())
{
N temp_node=q.front();
int x=temp_node.x;
int y=temp_node.y;
int z=temp_node.z;
int d=temp_node.dis;
q.pop();
for(int i=0;i<6;i++)
{
int xx=x+dx[i];
int yy=y+dy[i];
int zz=z+dz[i];
int dd=d+1;
if(xx>=0&&xx<r&&yy>=0&&yy<c&&zz>=0&&zz<l&&m[zz][xx][yy]!='#'&&book[zz][xx][yy]==false)
{
if((xx==ex)&&(yy==ey)&&(zz==ez))
{
return dd;
}
N tep;
tep.x=xx;
tep.y=yy;
tep.z=zz;
tep.dis=dd;
book[zz][xx][yy]=true;
q.push(tep);
}
}
}
return 0;
}
int main()
{
while(cin>>l>>r>>c&&l&&r&&c)
{
memset(book,0,sizeof(book));
for(int k=0;k<l;k++)
{
for(int i=0;i<r;i++)
cin>>m[k][i];
}
for(int k=0;k<l;k++)
for(int i=0;i<r;i++)
for(int j=0;j<c;j++)
{
if(m[k][i][j]=='S')
{
sx=i;
sy=j;
sz=k;
}
if(m[k][i][j]=='E')
{
ex=i;
ey=j;
ez=k;
}
}
if(sx==ex&&sy==ey&&sz==ez)
{
cout<<"Escaped in "<<0<<" minute(s)."<<endl;
continue;
}
int ans=bfs();
if(!ans)
cout<<"Trapped!"<<endl;
else
cout<<"Escaped in "<<ans<<" minute(s)."<<endl;
}
return 0;
}
poj2251
最新推荐文章于 2022-08-18 12:12:11 发布