您的位置:首页 > 其它

【POJ】-2251-Dungeon Master(BFS+队列)

2016-07-28 21:09 351 查看
Dungeon Master

Time Limit: 1000MS Memory Limit: 65536K
Total Submissions: 26414 Accepted: 10282
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
3 4 5
S....
.###.
.##..
###.#

#####
#####
##.##
##...

#####
#####
#.###
####E

1 3 3
S##
#E#
###

0 0 0

Sample Output
Escaped in 11 minute(s).
Trapped!


BFS+队列比DFS用时少,只把最优的情况推入队列。

这道题看成一个三维的,可以前后左右上下走。

#include<cstdio>
#include<cstring>
#include<queue>
#include<algorithm>
using namespace std;
int t,n,m;
int stx,sty,stz;
int edx,edy,edz;
char a[33][33][33];
int vis[33][33][33];
int dx[6]={0,0,0,0,1,-1}; //前后 左右 上下 走
int dy[6]={0,0,1,-1,0,0};
int dz[6]={1,-1,0,0,0,0};
struct node
{
int x,y,z,step;
}pr,ne;
bool dis(node h)
{
if(h.x>t||h.x<1||h.y>n||h.y<1||h.z>m||h.z<1||a[h.x][h.y][h.z]=='#'||vis[h.x][h.y][h.z]==1)
return false; //超出边界 或 遇到墙 或 被访问过不可走
return true;
}
int bfs()
{
queue<node> q;
memset(vis,0,sizeof(vis));
pr.x=stx;
pr.y=sty;
pr.z=stz;
pr.step=0;
q.push(pr); //开始的点进队列
vis[pr.x][pr.y][pr.z]=1;
while(!q.empty())
{
pr=q.front();
q.pop();
for(int i=0;i<6;i++) //把 ne 点上下左右前后的点都查询一遍,可以走的都进队列,并标记
{
ne=pr; //pr又赋值给ne,就是要他回到 pr点,对其六个方向全部遍历
ne.x+=dx[i];
ne.y+=dy[i];
ne.z+=dz[i];
ne.step++; //调试发现步数就是正确的,开始担心每次都加会错。但是他是结构体,步数是和位置绑定的
if(!dis(ne)) //判断 ne 是否可以走
continue;
if(ne.x==edx&&ne.y==edy&&ne.z==edz)
return ne.step;
vis[ne.x][ne.y][ne.z]=1; //走过,标记
q.push(ne); //可以走,进队列
} //六个方向全部判断以后 for 循环退出,继续 while,只要 q队列不为空(就是还有点可以走),从可以走的点开始,继续其六个方向的判断
}
return -1;
}
int main()
{
while(~scanf("%d %d %d",&t,&n,&m)&&t&&n&&m)
{
for(int i=1;i<=t;i++)
{
for(int j=1;j<=n;j++)
{
scanf("%s",a[i][j]+1);
for(int k=1;k<=m;k++)
{
if(a[i][j][k]=='S') //找到 S ,E 的位置
{
stx=i;
sty=j;
stz=k;
}
else if(a[i][j][k]=='E')
{
edx=i;
edy=j;
edz=k;
}
}
}
}
int ans=bfs();
if(ans==-1)
printf("Trapped!\n");
else
printf("Escaped in %d minute(s).\n",ans);
}
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: