POJ 2251 Dungeon Master
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!
题意:就是有一个三维迷宫,其实就是一个立方体,S代表起点,E代表终点,#代表障碍,问你从S到E的最少用时是多少,很基础的一道三维BFS题。
题解:主要是注意字符串类型数据读入时候的处理,一定要吃掉数据中的每一个换行。笔者今天为此改了一个小时左右的bug,以为每一层迷宫之间没有换行。
#include<cstdio>
#include<cstring>
#include<queue>
using namespace std;
const int maxn=30+7;
int map[maxn][maxn][maxn]; //存地图
int vis[maxn][maxn][maxn]; //标志数组
int step[maxn][maxn][maxn]; //存步数
int l,r,c,sx,sy,sz,ex,ey,ez;
struct node{ //结点 当前位置 ,走到当前位置所走的步数
int x,y,z,cnt;
node(int x=0,int y=0,int z=0,int cnt=0):x(x),y(y),z(z),cnt(cnt){}
};
int dir[6][3]={{0,0,1},{0,0,-1},{1,0,0},{-1,0,0},{0,1,0},{0,-1,0}}; //六个方向
int check(int x,int y,int z) //判断当前结点是否可前进
{
if(x>=1&&x<=l&&y>=1&&y<=r&&z>=1&&z<=c&&map[x][y][z]!='#')return 1;
else return 0;
}
void bfs(int x,int y,int z)
{
memset(vis,0,sizeof(vis));
memset(step,-1,sizeof(step));
queue<node>q;
while(!q.empty()) q.pop(); //清空队列
node now,next;
now.x=x;
now.y=y;
now.z=z;
now.cnt=0;
q.push(now);
vis[now.x][now.y][now.z]=1;
while(!q.empty()){
now =q.front();
q.pop();
step[now.x][now.y][now.z]=now.cnt;
for(int i=0;i<6;i++){ //六种走法 上 下 东 南 西 北
next.x=now.x+dir[i][0];
next.y=now.y+dir[i][1];
next.z=now.z+dir[i][2];
if(check(next.x,next.y,next.z)&&!vis[next.x][next.y][next.z]){
next.cnt=step[now.x][now.y][now.z]+1;
q.push(next);
vis[next.x][next.y][next.z]=1;
}
}
}
}
int main()
{
while(scanf("%d %d %d",&l,&r,&c)==3&&l&&r&&c){
getchar();
for(int i=1;i<=l;i++){
for(int j=1;j<=r;j++){
for(int k=1;k<=c;k++){
scanf("%c",&map[i][j][k]);
if(map[i][j][k]=='S'){ //寻找起点
sx=i;
sy=j;
sz=k;
}
if(map[i][j][k]=='E'){ //寻找终点
ex=i;
ey=j;
ez=k;
}
}
getchar();
}
getchar(); //字符串读取好难处理呀 看掉了数据每一层之间的换行
} // 又改了一个小时bug 又是少吃掉一个换行 这次一定记住
bfs(sx,sy,sz);
if(step[ex][ey][ez]>-1)printf("Escaped in %d minute(s).\n",step[ex][ey][ez]);
else printf("Trapped!\n");
}
return 0;
}

探讨三维迷宫逃脱问题,使用BFS算法求解从起点到终点的最短路径,解析输入输出格式,分享代码实现及调试经验。

322

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



