【POJ 2251】Dungeon Master(BFS)
2016-08-02 19:39
330 查看
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?
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 formEscaped 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!
题目大意
一个三维迷宫,问能否从迷宫中逃出。对于第一个样例可以如下移动:(1,1,1)->(1,1,2)->(1,1,3)->(1,1,4)->(1,1,5)->(1,2,5)
->(1,3,5)->(1,3,4)->(1,4,4)->(2,4,4)->(2,4,5)->(3,4,,5)
共11步就可以到达终点
思路
求最短花费,首先肯定想到BFS,这题只要多加两个方向就可以了,因为是三维的,所以还有上楼和下楼这两个方向。代码
#include <iostream> #incl 4000 ude <cstdio> #include <cstring> #include <algorithm> #include <queue> using namespace std; const int maxn=30+5; int dir[6][3]={{0,0,1},{0,0,-1},{0,1,0},{0,-1,0},{1,0,0},{-1,0,0}}; int vis[maxn][maxn][maxn],l,r,c; char map[maxn][maxn][maxn]; int posx,posy,posz,stax,stay,staz; struct proc { int x,y,z; int step; }; int bfs(int x,int y,int z) { proc vw,vn; queue<proc> q; vw.x=x;vw.y=y;vw.z=z; vis[x][y][z]=1; vw.step=0; q.push(vw); while(!q.empty()) { vw=q.front(); q.pop(); if(vw.x==posx&&vw.y==posy&&vw.z==posz) { return vw.step; } for(int i=0;i<6;i++) { vn.x=vw.x+dir[i][0]; vn.y=vw.y+dir[i][1]; vn.z=vw.z+dir[i][2]; if(!vis[vn.x][vn.y][vn.z]&&vn.x>=1&&vn.x<=r&&vn.y>=1&&vn.y<=c&&vn.z>=1&&vn.z<=l) { vn.step=vw.step+1; q.push(vn); vis[vn.x][vn.y][vn.z]=1; } } } return 0; } int main() { while(~scanf("%d %d %d",&l,&r,&c)&&(l&&r&&c)) { memset(vis,0,sizeof(vis)); for(int i=1;i<=l;i++) { for(int j=1;j<=r;j++) { for(int k=1;k<=c;k++) { cin>>map[j][k][i]; if(map[j][k][i]=='S') { stax=j;stay=k;staz=i; } if(map[j][k][i]=='E') { posx=j;posy=k;posz=i; } if(map[j][k][i]=='#') { vis[j][k][i]=1; } } } } int ans=bfs(stax,stay,staz); if(ans) printf("Escaped in %d minute(s).\n",ans); else printf("Trapped!\n"); } return 0; }
相关文章推荐
- POJ 2251 Dungeon Master (bfs)
- POJ-2251-Dungeon Master(BFS 优先队列)
- POJ2251 Dungeon Master(搜索BFS)
- poj 2251 Dungeon Master (BFS)
- poj2251 Dungeon Master(三维bfs)
- POJ 2251 Dungeon Master(BFS)
- POJ ~ 2251 ~ Dungeon Master (3维BFS)
- poj 2251 Dungeon Master(bfs)
- POJ 2251 Dungeon Master (BFS)
- POJ 2251 Dungeon Master【BFS】
- POJ2251 Dungeon Master(3D迷宫 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 Dungeon Master(BFS)
- POJ 2251 Dungeon Master【BFS】
- BFS_poj 2251 Dungeon Master