You are trapped in a 3D dungeon and need to find the quickest way out! The dungeon is composed of unit cubes which may or may not be filled with rock. It takes one minute to move one unit north, south, east, west, up or down. You cannot move diagonally and the maze is surrounded by solid rock on all sides.
Is an escape possible? If yes, how long will it take?
Input
The input consists of a number of dungeons. Each dungeon description starts with a line containing three integers L, R and C (all limited to 30 in size).
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.
Output
Each maze generates one line of output. If it is possible to reach the exit, print a line of the form
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!
Sample Input
3 4 5
S....
.###.
.##..
###.#
#####
#####
##.##
##...
#####
#####
#.###
####E
1 3 3
S##
#E#
###
0 0 0
Sample Output
Escaped in 11 minute(s).
Trapped!
这个题有点考察三维数组,之前没怎么写过,感觉还好啊,不是特别难做。
只是方向上有点不同,6个方向
#include <cstdio>
#include<queue>
#include<cstring>
using namespace std;
const int Max=31;
char mp[Max][Max][Max];
int vis[Max][Max][Max];
int H,W,L;
int st_x,st_y,st_z,en_x,en_y,en_z;
int move_x[6]={1,-1,0,0,0,0};
int move_y[6]={0,0,1,-1,0,0};
int move_z[6]={0,0,0,0,1,-1};
typedef struct
{
int x,y,z;
int step;
}Node;
int check(int x,int y,int z)
{
if(x<1||y<1||z<1||x>L||y>W||z>H||vis[z][x][y]||mp[z][x][y]=='#')
return 1;
return 0;
}
int bfs()
{
queue<Node> q;
memset(vis,0,sizeof(vis));
Node now,next;
now.x=st_x,now.y=st_y,now.z=st_z,now.step=0;
vis[st_z][st_x][st_y]=1;
q.push(now);
while(q.size())
{
now=q.front();
q.pop();
if((now.x==en_x)&&(now.y==en_y)&&(now.z==en_z))
return now.step;
for(int i=0;i<6;i++)
{
next.x=now.x+move_x[i];
next.y=now.y+move_y[i];
next.z=now.z+move_z[i];
if(check(next.x,next.y,next.z))
continue;
vis[next.z][next.x][next.y]=1;
next.step=now.step+1;
q.push(next);
}
}
return -1;
}
int main()
{
while(scanf("%d%d%d",&H,&L,&W))
{
if(H==0&&L==0&&W==0)
break;
memset(mp,0,sizeof(mp));//千万别再犯傻,把地图放到bfs()里面清空了,太傻了
for(int i=1;i<=H;i++)
{
for(int j=1;j<=L;j++)
for(int k=1;k<=W;k++)
{
scanf(" %c",&mp[i][j][k]);//神笔之作
if(mp[i][j][k]=='S')
{
st_z=i,st_x=j,st_y=k;
}
else if(mp[i][j][k]=='E')
{
en_z=i,en_x=j,en_y=k;
}
}
}
int ans=bfs();
if(ans==-1)
printf("Trapped!\n");
else
printf("Escaped in %d minute(s).\n",ans);
}
return 0;
}