POJ 2251 Dungeon Master <三维BFS水题>
2017-10-13 20:36
323 查看
[align=center]Dungeon Master[/align]
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
Source
Ulm Local 1997
看到题 ,第一反应BFS ,但是这是一种多个区域的“走迷宫”问题,那该怎么办呢?
其实转化一下,就是一道简单的三维BFS 题目,道理很简单,方向数组改成6个方向,点的坐标设置x,y,z ,然后
套用普通的BFS就能过了
#include<cstdio>
#include<string>
#include<cstring>
#include<iostream>
#include<queue>
using namespace std;
int l,r,c;
int sx,sy,sz,ex,ey,ez;
struct Node{
int x,y,z,step;
bool friend operator < (Node a, Node b){
return a.step>b.step;}
};
int vis[50][50][50];
int dir[][3]={{0,0,1},{0,1,0},{1,0,0},{0,0,-1},{0,-1,0},{-1,0,0}};
priority_queue<Node>q;
char maps[50][50][50];
int bfs(){
Node a;
a.x=sx;
a.y=sy;
a.z=sz;
a.step=0;
q.push(a);
while(!q.empty()){
Node tp=q.top(),tmp;
if(tp.x==ex&&tp.y==ey&&tp.z==ez){
return tp.step;
}
q.pop();
for(int i=0;i<6;i++)
{
tmp.x=tp.x+dir[i][0];
tmp.y=tp.y+dir[i][1];
tmp.z=tp.z+dir[i][2];
if(tmp.x<0||tmp.x>=l||tmp.y<0||tmp.y>=r||tmp.z<0||tmp.z>=c||vis[tmp.x][tmp.y][tmp.z]||maps[tmp.x][tmp.y][tmp.z]=='#')
continue;
vis[tmp.x][tmp.y][tmp.z]=1;
tmp.step=tp.step+1;
q.push(tmp);
}
}
return -1;
}
int main()
{
while(~scanf("%d%d%d",&l,&r,&c)&&l+r+c)
{
memset(vis,0,sizeof(vis));
for(int i=0; i<l; i++)
for(int j=0; j<r; j++)
{
scanf("%s",maps[i][j]);
for(int k=0; k<c; k++)
if(maps[i][j][k]=='S')
sx=i,sy=j,sz=k;
else if(maps[i][j][k]=='E')
ex=i,ey=j,ez=k;
}
int ans=bfs();
if(ans==-1)
printf("Trapped!\n");
else
printf("Escaped in %d minute(s).\n",ans);
}
return 0;
}
Time Limit: 1000MS | Memory Limit: 65536K | |
Total Submissions: 37966 | Accepted: 14520 |
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!
Source
Ulm Local 1997
看到题 ,第一反应BFS ,但是这是一种多个区域的“走迷宫”问题,那该怎么办呢?
其实转化一下,就是一道简单的三维BFS 题目,道理很简单,方向数组改成6个方向,点的坐标设置x,y,z ,然后
套用普通的BFS就能过了
#include<cstdio>
#include<string>
#include<cstring>
#include<iostream>
#include<queue>
using namespace std;
int l,r,c;
int sx,sy,sz,ex,ey,ez;
struct Node{
int x,y,z,step;
bool friend operator < (Node a, Node b){
return a.step>b.step;}
};
int vis[50][50][50];
int dir[][3]={{0,0,1},{0,1,0},{1,0,0},{0,0,-1},{0,-1,0},{-1,0,0}};
priority_queue<Node>q;
char maps[50][50][50];
int bfs(){
Node a;
a.x=sx;
a.y=sy;
a.z=sz;
a.step=0;
q.push(a);
while(!q.empty()){
Node tp=q.top(),tmp;
if(tp.x==ex&&tp.y==ey&&tp.z==ez){
return tp.step;
}
q.pop();
for(int i=0;i<6;i++)
{
tmp.x=tp.x+dir[i][0];
tmp.y=tp.y+dir[i][1];
tmp.z=tp.z+dir[i][2];
if(tmp.x<0||tmp.x>=l||tmp.y<0||tmp.y>=r||tmp.z<0||tmp.z>=c||vis[tmp.x][tmp.y][tmp.z]||maps[tmp.x][tmp.y][tmp.z]=='#')
continue;
vis[tmp.x][tmp.y][tmp.z]=1;
tmp.step=tp.step+1;
q.push(tmp);
}
}
return -1;
}
int main()
{
while(~scanf("%d%d%d",&l,&r,&c)&&l+r+c)
{
memset(vis,0,sizeof(vis));
for(int i=0; i<l; i++)
for(int j=0; j<r; j++)
{
scanf("%s",maps[i][j]);
for(int k=0; k<c; k++)
if(maps[i][j][k]=='S')
sx=i,sy=j,sz=k;
else if(maps[i][j][k]=='E')
ex=i,ey=j,ez=k;
}
int ans=bfs();
if(ans==-1)
printf("Trapped!\n");
else
printf("Escaped in %d minute(s).\n",ans);
}
return 0;
}
相关文章推荐
- 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 - 2251 Dungeon Master BFS
- POJ-2251 Dungeon Master bfs搜索
- POJ 2251 Dungeon Master bfs 难度:0
- POJ 2251 Dungeon Master BFS
- POJ 2251:Dungeon Master【bfs】
- POJ - 2251 Dungeon Master BFS
- POJ 2251 Dungeon Master BFS
- Dungeon Master<poj2251>
- B - Dungeon Master POJ - 2251(广搜三维)
- poj2251 Dungeon Master bfs
- POJ:Dungeon Master(三维bfs模板题)
- POJ 2251 Dungeon Master BFS(广度优先搜索)
- poj 2251 Dungeon Master(bfs)
- Dungeon Master POJ - 2251 BFS
- poj-2251 Dungeon Master BFS