#include <iostream>
#include <string>
#include <cstring>
#include <queue>
using namespace std;
struct node//用结构体构造
{
int x,y,z;
int count;
};
int l,r,c,i,j,k;
char qipan[35][35][35];
int tansuo[35][35][35];
int d[6][3] = {{0,0,1},{0,0,-1},{0,1,0},{0,-1,0},{1,0,0},{-1,0,0}};//6个方向
int x1,y1,z1;
void bfs()
{
queue<node>Q;//创建并初始化队列
node t1;
t1.x=x1,t1.y=y1,t1.z=z1,t1.count=0;
tansuo[x1][y1][z1]=1;
Q.push(t1);
while(!Q.empty())
{
node t2;
t2=Q.front();
Q.pop();
int xx=t2.x;
int yy=t2.y;
int zz=t2.z;
int count=t2.count;
if(qipan[xx][yy][zz]=='E')//成功到达出口,程序结束
{
cout<<"Escaped in "<<count<<" minute(s)."<<endl;
return ;
}
for(i=0; i<6; i++)
{
node t3;
int xi=t3.x=xx+d[i][0];
int yi=t3.y=yy+d[i][1];
int zi=t3.z=zz+d[i][2];
if(xi<0||xi>l-1||yi<0||yi>r-1||zi<0||zi>c-1)
continue;//越界,跳过
if(qipan[xi][yi][zi] != '#' && !tansuo[xi][yi][zi])//进行搜索
{
tansuo[xi][yi][zi]=1;//标记
t3.count=count+1;
Q.push(t3);//搜索完出队列
}
}
}
cout<<"Trapped!"<<endl;
}
int main()
{
while(cin>>l>>r>>c)
{
if(l==0&&r==0&&c==0)
break;
for(i=0; i<l; i++)
{
for(j=0; j<r; j++)
{
for(k=0; k<c; k++)
{
cin>>qipan[i][j][k];
if(qipan[i][j][k]=='S')//标记起点
{
x1=i,y1=j,z1=k;
}
}
}
}
memset(tansuo,0,sizeof(tansuo));
bfs();
}
return 0;
}
Dungeon Master bfs
最新推荐文章于 2023-03-22 21:36:10 发布