POJ 2251 Dungeon Master(BFS)
2014-02-10 10:23
453 查看
Dungeon Master
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 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
Sample Output
Time Limit: 1000MS | Memory Limit: 65536K | |
Total Submissions: 15022 | Accepted: 5829 |
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!
题解:是一个三维坐标的搜索,跟二位的差不多。没有什么好说的,简单题一次性过了。 AC code:
#include <iostream> #include <queue> using namespace std; #define MAX 101 char map[MAX][MAX][MAX]; int vist[MAX][MAX][MAX]; int L,R,C,xs,ys,xe,ye,zs,ze; int dist[6][3]={1,0,0,-1,0,0,0,1,0,0,-1,0,0,0,1,0,0,-1}; struct point { int x,y,z,step; }; queue <point> Q; int inMap(int x,int y,int z) { if(x<0 || x>=L || y<0 || y>=R ||z<0 || z>=C) return 0; return 1; } void bfs(int x,int y,int z) { while(!Q.empty()) Q.pop(); point start,now,next; start.x=x; start.y=y; start.z=z; start.step=0; vist[start.x][start.y][start.z]=1; Q.push(start); while(!Q.empty()) { now=Q.front(); Q.pop(); if(now.x==xe && now.y==ye && now.z==ze) { cout<<"Escaped in "<<now.step<<" minute(s)."<<endl; return; } for(int i=0;i<6;i++) { next.x=now.x+dist[i][0]; next.y=now.y+dist[i][1]; next.z=now.z+dist[i][2]; if(inMap(next.x,next.y,next.z) && !vist[next.x][next.y][next.z] && map[next.x][next.y][next.z]!='#') { vist[next.x][next.y][next.z]=1; next.step=now.step+1; Q.push(next); } } } cout<<"Trapped!"<<endl; } int main() { int i,j,k; while(cin>>L>>R>>C) { if(L+R+C==0)break; memset(vist,0,sizeof(vist)); for(i=0;i<L;i++) for(j=0;j<R;j++) for(k=0;k<C;k++) { cin>>map[i][j][k]; if(map[i][j][k]=='S') { xs=i; ys=j; zs=k; } if(map[i][j][k]=='E') { xe=i; ye=j; ze=k; } } bfs(xs,ys,zs); } return 0; }
相关文章推荐
- POJ-2251 Dungeon Master(简单的三维bfs)
- poj 2251 — Dungeon Master (3D + BFS)
- POJ 2251 Dungeon Master (bfs)
- POJ 2251 Dungeon Master(BFS最短路)
- POJ 2251 Dungeon Master【BFS】
- poj2251 Dungeon Master(三维bfs)
- poj 2251 Dungeon Master(bfs)
- POJ ~ 2251 ~ Dungeon Master (3维BFS)
- 2道裸BFS(POJ 3278 Catch That Cow / POJ 2251 Dungeon Master)
- POJ 2251 Dungeon Master 简单的三维bfs
- POJ - 2251 Dungeon Master(BFS)
- poj2251 dungeon master【BFS】~
- Dungeon Master(bfs)逃离迷宫 POJ - 2251
- POJ-2251 Dungeon Master (BFS模板题)
- POJ - 2251 Dungeon Master(15.10.10 搜索专题)bfs
- poj 2251 Dungeon Master(多起点bfs)
- POJ2251 Dungeon Master(bfs)
- POJ 2251 Dungeon Master(bfs)
- poj2251-dungeon master(bfs广搜)
- POJ 2251 Dungeon Master(BFS)