Is an escape possible? If yes, how long will it take?
L is the number of levels making up the dungeon.
R and C are the number of rows and columns making up the plan of each level.
Then there will follow L blocks of R lines each containing C characters. Each character describes one cell of the dungeon. A cell full of rock is indicated by a '#' and empty cells are represented by a '.'. Your starting position is indicated by 'S' and the exit by the letter 'E'. There's a single blank line after each level. Input is terminated by three zeroes for L, R and C.
Escaped in x minute(s).
where x is replaced by the shortest time it takes to escape.
If it is not possible to escape, print the line
Trapped!
3 4 5 S.... .###. .##.. ###.# ##### ##### ##.## ##... ##### ##### #.### ####E 1 3 3 S## #E# ### 0 0 0
Escaped in 11 minute(s). Trapped!
#include<iostream>
#include<cstdio>
#include<cstring>
#include<queue>
using namespace std;
char map[32][32][32];
int vis[32][32][32];
int dir[6][3]={{0,0,1},{0,0,-1},{0,1,0},{0,-1,0},{1,0,0},{-1,0,0}};
int n,m,o;
struct node
{
int x,y,z;
int s;
};
int sx,sy,sz;
int ex,ey,ez;
int ans;
int BFS(int x,int y,int z)
{
queue<struct node>q;
struct node s;
s.x=x;
s.y=y;
s.z=z;
s.s=0;
q.push(s);
while(!q.empty())
{
struct node t;
t=q.front();
q.pop();
if(t.x==ex&&t.y==ey&&t.z==ez)
{
return t.s;
}
int i;
for(i=0;i<=5;i++)
{
int xx=dir[i][0]+t.x;
int yy=dir[i][1]+t.y;
int zz=dir[i][2]+t.z;
if(xx>=0&&xx<=n-1&&yy>=0&&yy<=m-1&&zz>=0&&zz<=o-1&&map[xx][yy][zz]!='#'&&vis[xx][yy][zz]==0)
{
vis[xx][yy][zz]=1;
struct node f;
f.x=xx;
f.y=yy;
f.z=zz;
f.s=t.s+1;
q.push(f);
}
}
}
return -1;
}
int main()
{
while(~scanf("%d%d%d",&n,&m,&o))
{
if(n==0&&m==0&&o==0) break;
memset(vis,0,sizeof(vis));
int i,j,k;
for(i=0;i<=n-1;i++)
{
for(j=0;j<=m-1;j++)
{
scanf("%s",map[i][j]);
}
}
for(i=0;i<=n-1;i++)
{
for(j=0;j<=m-1;j++)
{
for(k=0;k<=o-1;k++)
{
if(map[i][j][k]=='S')
{
sx=i;
sy=j;
sz=k;
}
else if(map[i][j][k]=='E')
{
ex=i;
ey=j;
ez=k;
}
}
}
}
ans=BFS(sx,sy,sz);
if(ans==-1) printf("Trapped!\n");
else printf("Escaped in %d minute(s).\n",ans);
}
return 0;
}
185



被折叠的 条评论
为什么被折叠?



