#include<stdio.h>
#include<string.h>
char map[35][35][35];
int mark[35][35][35];
int to[7][4]={{0,0,1},{0,0,-1},{1,0,0},{-1,0,0},{0,1,0},{0,-1,0}};
int a,b,c;
struct
{
int x,y,z,id;
}queue[27005],st,ed,now,next;
int bfs()
{
memset(mark,0,sizeof(mark));
int fro,end;
fro=end=0;
st.id=0;
queue[end++]=st;
mark[st.x][st.y][st.z]=1;
while(fro<end)
{
now=queue[fro++];
if(now.x==ed.x&&now.y==ed.y&&now.z==ed.z)
{
return now.id;
}
for(int i=0;i<6;i++)
{
next.x=now.x+to[i][0];
next.y=now.y+to[i][1];
next.z=now.z+to[i][2];
if(next.x<0||next.y<0||next.z<0||next.x>=a||next.y>=b||next.z>=c||mark[next.x][next.y][next.z]==1||map[next.x][next.y][next.z]=='#')
{
continue;
}
next.id=now.id+1;
mark[next.x][next.y][next.z]=1;
queue[end++]=next;
}
}
return -1;
}
int main()
{
while(scanf("%d%d%d",&a,&b,&c)!=EOF)
{
if(a==0&&b==0&&c==0)
{
break;
}
for(int i=0;i<a;i++)
{
if(i!=0)
{
getchar();
}
for(int j=0;j<b;j++)
{
getchar();
for(int k=0;k<c;k++)
{
scanf("%c",&map[i][j][k]);
if(map[i][j][k]=='S')
{
st.x=i;
st.y=j;
st.z=k;
}
else if(map[i][j][k]=='E')
{
ed.x=i;
ed.y=j;
ed.z=k;
}
}
}
}
int t=bfs();
if(t!=-1)
{
printf("Escaped in %d minute(s).\n",t);
}
else
{
puts("Trapped!");
}
}
}
POJ2251 - Dungeon Master - 广度优先搜索
最新推荐文章于 2021-12-02 22:37:40 发布