poj 2251 Dungeon Master(BFS三维)
来源:互联网 发布:东风3怎么运到沙特知乎 编辑:程序博客网 时间:2024/05/16 10:36
Dungeon Master
Time Limit: 1000MS Memory Limit: 65536KTotal Submissions: 20912 Accepted: 8106
点击打开链接
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
where x is replaced by the shortest time it takes to escape.
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 5S.....###..##..###.#############.####...###########.#######E1 3 3S###E####0 0 0
Sample Output
Escaped in 11 minute(s).Trapped!
题意:
给出一三维空间的地牢,要求求出由字符'S'到字符'E'的最短路径
移动方向可以是东、西、南、北、上、下六个个方向
每移动一次就耗费一分钟,要求输出最快的走出时间。
不同L层的地图,相同RC坐标处是连通的
#无法行通
.代表路
题目分析:
此题与hdoj 1242 Rescue 十分相似
只不过这个题稍微复杂的是三维,其实也复杂不了多少,小心就是啦
代码:
#include<cstdio>#include<cstring>#include<queue>#include<cstdlib>#include<algorithm>using namespace std;#define MAX 35char map[MAX][MAX][MAX];int step[MAX][MAX][MAX];int L,R,C;int sx,sy,sz,ex,ey,ez;int f[][3]={{1,0,0},{-1,0,0},{0,1,0},{0,-1,0},{0,0,1},{0,0,-1}};struct node{ int x; int y; int z; int time;};queue<node>q;void input(){ for(int i=0;i<L;i++) for(int j=0;j<R;j++) for(int k=0;k<C;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; } step[i][j][k]=-1; }}int valid(int x,int y,int z){if(x>=0&&x<L&&y>=0&&y<R&&z>=0&&z<C) return 1;else return 0;}void BFS(){node p1,p2;p1.x=sx;p1.y=sy;p1.z=sz;p1.time=0;step[p1.x][p1.y][p1.z]=0;while(!q.empty())q.pop();q.push(p1);while(!q.empty()){ p1=q.front(); q.pop(); for(int i=0;i<6;i++)//注意这儿 { p2.x=p1.x+f[i][0];p2.y=p1.y+f[i][1];p2.z=p1.z+f[i][2]; if(map[p2.x][p2.y][p2.z]!='#'&&valid(p2.x,p2.y,p2.z)) { p2.time=p1.time+1; if(step[p2.x][p2.y][p2.z]==-1||p2.time<step[p2.x][p2.y][p2.z]) {q.push(p2);step[p2.x][p2.y][p2.z]=p2.time; }} } }}int main(){while(~scanf("%d%d%d",&L,&R,&C),(L||R||C)){ for(int i=0;i<L;i++) for(int j=0;j<R;j++) scanf("%s",map[i][j]); input(); BFS(); if(step[ex][ey][ez]==-1) printf("Trapped!\n"); else printf("Escaped in %d minute(s).\n",step[ex][ey][ez]);}return 0;}看了人家的解题报告,我发现人家的思路比我的简练的多了因此我再按照人家的思路再贴一个:
#include<cstdio>#include<cstring>#include<queue>#include<cstdlib>#include<algorithm>using namespace std;#define MAX 35char map[MAX][MAX][MAX];int vis[MAX][MAX][MAX];int step[MAX][MAX][MAX];int L,R,C;int sx,sy,sz;int f[][3]={{1,0,0},{-1,0,0},{0,1,0},{0,-1,0},{0,0,1},{0,0,-1}};struct node{ int x; int y; int z; int time;};queue<node>q;void input(){ for(int i=0;i<L;i++) for(int j=0;j<R;j++) for(int k=0;k<C;k++) { if(map[i][j][k]=='S') { sx=i;sy=j;sz=k; } vis[i][j][k]=0; }}int valid(int x,int y,int z){if(x>=0&&x<L&&y>=0&&y<R&&z>=0&&z<C) return 1;else return 0;}void BFS(){node p1,p2;p1.x=sx;p1.y=sy;p1.z=sz;p1.time=0;while(!q.empty())q.pop();vis[p1.x][p1.y][p1.z]=1;q.push(p1);while(!q.empty()){ p1=q.front(); q.pop(); for(int i=0;i<6;i++)//注意这儿 { p2.x=p1.x+f[i][0];p2.y=p1.y+f[i][1];p2.z=p1.z+f[i][2]; if(map[p2.x][p2.y][p2.z]!='#'&&valid(p2.x,p2.y,p2.z)&&!vis[p2.x][p2.y][p2.z]) { p2.time=p1.time+1; if(map[p2.x][p2.y][p2.z]=='E')//此处直接返回值,不用那么复杂 { printf("Escaped in %d minute(s).\n",p2.time); return ; } vis[p2.x][p2.y][p2.z]=1; q.push(p2);} } }printf("Trapped!\n");}int main(){while(~scanf("%d%d%d",&L,&R,&C),(L||R||C)){ for(int i=0;i<L;i++) for(int j=0;j<R;j++) scanf("%s",map[i][j]); input(); BFS();}return 0;}
0 0
- POJ 2251 Dungeon Master (三维BFS)
- POJ 2251:Dungeon Master(三维BFS)
- POJ 2251 Dungeon Master (三维迷宫 BFS)
- POJ 2251 Dungeon Master(三维BFS)
- poj 2251 Dungeon Master 三维bfs
- poj 2251 Dungeon Master(三维BFS)(中等)
- poj 2251 Dungeon Master(BFS三维)
- POJ 2251 Dungeon Master(三维bfs)
- POJ 2251 Dungeon Master(三维bfs)
- <三维BFS搜索> POJ 2251 Dungeon Master
- POJ 2251:Dungeon Master(三维BFS)
- POJ 2251 Dungeon Master(三维BFS)
- POJ 2251 Dungeon Master 三维BFS
- POJ 2251 Dungeon Master(三维bfs)
- POJ 2251 Dungeon Master (三维BFS)
- POJ 2251(WA)Dungeon Master(三维BFS)
- POJ.2251 Dungeon Master (三维BFS)
- poj 2251Dungeon Master(三维bfs)
- 企业大数据发展的阶段性
- Android 上滑显示底部导航,下滑显示标题bar
- display:flex弹性布局
- 关于Qt中遇到qDebug输出QString时输出Unicode码问题的解决与总结
- strspn 和strcspn
- poj 2251 Dungeon Master(BFS三维)
- CSS3属性选择器
- 数据结构基础(14) --链式队列的设计与实现
- python监控linux系统信息(流量/内存/cpu/负载/磁盘/运转时间) Centos安装X Window和桌面环境
- H5表单类型
- 手游反外挂
- 你所需要知道的一些git 的使用命令:历史
- Tasks and Back Stack
- Android学习遇到的问题及总结