Description
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?
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.
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
If it is not possible to escape, print the line
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!
题目链接:http://poj.org/problem?id=2251
解法类型:BFS | DFS
解题思路:又是一道迷宫的最短路径问题,只是由以前的平面地图变成的三维的地图。其实没什么差别,同样BFS而已,只是向6个方位扩展,可以的就入队列。。。但网上流传的此题为DFS类,我觉得BFS更简单,可能这题的数据量很小吧。
算法实现:
//STATUS:C++_AC_16MS_168k
#include<stdio.h>
#include<memory.h>
const int MAXN=40;
int BFS();
char map[MAXN][MAXN][MAXN],vis[MAXN][MAXN][MAXN];
int dis[MAXN][MAXN][MAXN],L,R,C,x1,y1,z1,x2,y2,z2;
int dx[6]={-1,0,1,0,0,0},dy[6]={0,1,0,-1,0,0},dz[6]={0,0,0,0,1,-1};
int main()
{
// freopen("in.txt","r",stdin);
int i,j,k,ok;
while(scanf("%d%d%d",&L,&R,&C)&&L)
{
memset(vis,0,sizeof(vis)); //初始化
for(i=0;i<L;i++){
for(j=0;j<R;j++){
scanf("%s",map[i][j]);
for(k=0;k<C;k++)
if(map[i][j][k]=='S')z1=i,x1=j,y1=k; //分别找出起始点坐标
else if(map[i][j][k]=='E')z2=i,x2=j,y2=k;
}
}
ok=BFS();
ok?printf("Escaped in %d minute(s).\n",dis[z2][x2][y2]):printf("Trapped!\n");
}
return 0;
}
int BFS()
{
int q[MAXN*MAXN*MAXN][2],front=0,rear=0,x,y,z,nx,ny,nz,u;
q[rear][0]=x1*C+y1,q[rear++][1]=z1; //起点入队列
vis[z1][x1][y1]=1;
dis[z1][x1][y1]=0;
while(front<rear) //队列非空
{
u=q[front][0],z=q[front++][1]; //出队列
x=u/C,y=u%C;
if(x==x2&&y==y2&&z==z2)return 1; //search succeed
for(int i=0;i<6;i++){
nx=x+dx[i],ny=y+dy[i],nz=z+dz[i];
if(nx>=0&&nx<R && ny>=0&&ny<C && nz>=0&&nz<L //判断能否入队列
&& (map[nz][nx][ny]=='.'||map[nz][nx][ny]=='E') && (!vis[nz][nx][ny])){
vis[nz][nx][ny]=1;
dis[nz][nx][ny]=dis[z][x][y]+1;
u=nx*C+ny;
q[rear][0]=u,q[rear++][1]=nz; //入队列
}
}
}
return 0; //search fail
}