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!
AC代码
#include<iostream>
#include<cstdio>
#include<cmath>
#include<queue>
#include<algorithm>
#include<cstring>
using namespace std;
int L,R,C;
char m[35][35][35];
int vis[35][35][35];
int to[4][2]={0,1,1,0,0,-1,-1,0};
struct node
{
int x,y,step,lev;
}sta,End;
int ans;
void bfs(node sta)
{
queue<node>q;
while(!q.empty())q.pop();
sta.step=0;
q.push(sta);
vis[sta.lev][sta.y][sta.x]=1;
node temp,temp1,temp2,temp3;
while(!q.empty()&&ans==-1)
{
temp=q.front();q.pop();
if(temp.lev==End.lev&&temp.x==End.x&&temp.y==End.y){ans=temp.step;break;}
for(int i=0;i<4;i++)
{
temp1.x=temp.x+to[i][0];temp1.y=temp.y+to[i][1];temp1.lev=temp.lev;temp1.step=temp.step+1;
if(!vis[temp1.lev][temp1.y][temp1.x])
{
vis[temp1.lev][temp1.y][temp1.x]=1;
if(m[temp1.lev][temp1.y][temp1.x]=='.'||m[temp1.lev][temp1.y][temp1.x]=='E')
q.push(temp1);
}
temp2.x=temp.x;temp2.y=temp.y;temp2.lev=temp.lev+1;temp2.step=temp.step+1;
if(!vis[temp2.lev][temp2.y][temp2.x])
{
vis[temp2.lev][temp2.y][temp2.x]=1;
if(m[temp2.lev][temp2.y][temp2.x]=='.'||m[temp2.lev][temp2.y][temp2.x]=='E')
q.push(temp2);
}
temp3.x=temp.x;temp3.y=temp.y;temp3.lev=temp.lev-1;temp3.step=temp.step+1;
if(!vis[temp3.lev][temp3.y][temp3.x])
{
vis[temp3.lev][temp3.y][temp3.x]=1;
if(m[temp3.lev][temp3.y][temp3.x]=='.'||m[temp3.lev][temp3.y][temp3.x]=='E')
q.push(temp3);
}
}
}
}
void in()
{
for(int i=1;i<=L;i++)
{
for(int j=1;j<=R;j++)
{
for(int k=1;k<=C;k++){
cin>>m[i][j][k];
if(m[i][j][k]=='S')
{
sta.x=k,sta.y=j;
sta.lev=i;
}
else if(m[i][j][k]=='E')
{
End.x=k,End.y=j;
End.lev=i;
}
}
}
}
}
int main()
{
while(~scanf("%d%d%d",&L,&R,&C))
{
if(L+R+C==0)break;
memset(vis,0,sizeof vis);
ans=-1;
in();
bfs(sta);
if(ans==-1)
printf("Trapped!\n");
else
printf("Escaped in %d minute(s).\n",ans);
}
return 0;
}
503



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



