Dungeon Master POJ - 2251
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!
题意:
一个三维的题图, . 可以走 # 不能走。如果能走输出最小步数(Escaped in 最小步数 minute(s).),不能走就输出Trapped!
思路:
平时我们写的地图是二维的,这道题是三维的地图。这道题和二维的做法相同,使用bfs算法,用一个队列来维护,求最小步数。
值得注意的是 S 和 E 两点(开始和结束)是可以走的,也就是在判断的时候要加上 S和E 两个点,不能只判断为 . 的点。
代码:
#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<queue>
using namespace std;
const int N = 40;
int l, r, c, sx, sy, sz, ex, ey, ez;
char g[N][N][N];
bool st[N][N][N];
int dx[]={0,0,0,0,1,-1};
int dy[]={0,0,1,-1,0,0};
int dz[]={1,-1,0,0,0,0};
bool flag;
struct node
{
int x,y,z,step;
};
void bfs(int startx, int starty, int startz, int endx, int endy, int endz)
{
flag=false; // 判断是否走得到
queue<node> q;
node s;
s.x=startx, s.y=starty, s.z=startz, s.step=0;
q.push(s);
st[startx][starty][startz]=true;
while(!q.empty())
{
node t=q.front();
int x=t.x;
int y=t.y;
int z=t.z;
if(x==endx && y==endy && z==endz)
{
flag=true; // 走得到
printf("Escaped in %d minute(s).\n",q.front().step);
break;
}
for(int i=0;i<6;i++)
{
int tx=x+dx[i];
int ty=y+dy[i];
int tz=z+dz[i];
if(tx>=0 && tx<l && ty>=0 && ty<r && tz>=0 && tz<=c && !st[tx][ty][tz] && (g[tx][ty][tz]=='.' || g[tx][ty][tz]=='S' || g[tx][ty][tz]=='E'))
{// 这里注意S 和 E 也是可以走的所以要特判一下也就是. S E 都能入队
node temp;
temp.x=tx;
temp.y=ty;
temp.z=tz;
temp.step=q.front().step+1;
q.push(temp);
st[tx][ty][tz]=true;
}
}
q.pop();
}
}
int main()
{
while(cin>>l>>r>>c)
{
if(l==0 && r==0 && c==0) break;
memset(g,0,sizeof g);
memset(st,false,sizeof st);
for(int i=0;i<l;i++)
{
for(int j=0;j<r;j++) cin>>g[i][j];
}
for(int i=0;i<l;i++)
{
for(int j=0;j<r;j++)
{
for(int k=0;k<c;k++)
{
if(g[i][j][k]=='S') sx=i,sy=j,sz=k;
if(g[i][j][k]=='E') ex=i,ey=j,ez=k;
}
}
}
bfs(sx,sy,sz,ex,ey,ez);
if(!flag) printf("Trapped!\n");
}
return 0;
}