POJ-2251-Dungeon Master(BFS)
来源:互联网 发布:华中科技大学软件下载 编辑:程序博客网 时间:2024/06/06 12:04
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!
这道题是一道简单的三维BFS题,只是将以前的四个方向变成6个方向就行了
代码如下:
#include <cstdio>#include <queue>#include <cstring>#include <algorithm>using namespace std;int vis[31][31][31];char c[31][31][31];int i[6][3]= {{0,0,1},{0,0,-1},{1,0,0},{-1,0,0},{0,-1,0},{0,1,0}};int j,k,l,sx,sy,sz,ex,ey,ez;struct node{ int x,y,z,step;};bool check(int x,int y,int z){ if(x<0||y<0||z<0||x>=j||y>=k||z>=l) return true; if(c[x][y][z]=='#') return true; if(vis[x][y][z]) return true; return false;}int bfs(){ node s,next; s.x=sx,s.y=sy,s.z=sz; vis[sx][sy][sz]=1; s.step = 0; queue<node>q; q.push(s); while(!q.empty()) { s = q.front(); q.pop(); if(s.x==ex&&s.y==ey&&s.z==ez) return s.step; for(int a = 0; a < 6; a ++) { next = s; next.x = s.x + i[a][0]; next.y = s.y + i[a][1]; next.z = s.z + i[a][2]; if(check(next.x,next.y,next.z)) continue; vis[next.x][next.y][next.z]=1; next.step = s.step+1; q.push(next); } } return 0;}int main(){ while(~scanf("%d%d%d",&j,&k,&l),j+k+l) { for(int a = 0; a < j; a ++) { for(int b = 0; b < k; b ++) { scanf("%s",c[a][b]); for(int d = 0; d < l; d ++) { if(c[a][b][d]=='E') { ex=a; ey=b; ez=d; } else if(c[a][b][d]=='S') { sx=a; sy=b; sz=d; } } } } memset(vis,0,sizeof(vis)); int ans = bfs(); if(ans) printf("Escaped in %d minute(s).\n",ans); else printf("Trapped!\n"); } 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)
- 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)
- CSS+DIV网页样式与布局从入门到精通
- iOS开发——音频播放 (三):AudioFileStream
- ACM数论之旅6---数论倒数,又称逆元(我整个人都倒了( ̄﹏ ̄))
- CCF—工资计算 20161202
- Bootstrap Table使用方法详解 作者:aozeahj
- POJ-2251-Dungeon Master(BFS)
- Thread的interrupt()方法排雷
- Oracle 函数
- P2740 [USACO4.2]草地排水Drainage Ditches
- jdbc数据源连接数据库和jdbc连接数据库
- QT5.9.1入门一:GUI的hello world
- Hibernate中缓存的问题
- 18:验证子串
- ActiveMQ引起的JDK1.7 SSL错误