您的位置:首页 > 其它

POJ 2251 Dungeon Master

2014-03-23 16:41 183 查看
这道题目真心不难,只是最简单的bfs,这个迷宫是放在3维空间里的,走迷宫时只需多加两个方向。

思路是找到迷宫入口,开始bfs,找到出口就记录最少步数,没找到就输出Trapped。

Dungeon Master

Time Limit: 1000MSMemory Limit: 65536K
Total Submissions: 15409Accepted: 5960
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!


#include<stdio.h>
#include<string.h>
int l,r,c,ans,vis[35][35][35];
char mat[35][35][35];

int dh[6]={1,-1,0,0,0,0};
int dx[6]={0,0,0,0,1,-1};
int dy[6]={0,0,-1,1,0,0};

struct C
{
int h,x,y,step;
}q[10010];

int bfs(int h,int x,int y)
{
int front=0,rear=0;
q[rear].h=h;
q[rear].x=x;
q[rear].y=y;
q[rear++].step=0;
vis[h][x][y]=1;
while(front<rear)
{
for(int i=0;i<6;i++)
{
int nh=q[front].h+dh[i];
int nx=q[front].x+dx[i];
int ny=q[front].y+dy[i];
if(!vis[nh][nx][ny] && mat[nh][nx][ny]!='#')
{
vis[nh][nx][ny]=1;
q[rear].h=nh;
q[rear].x=nx;
q[rear].y=ny;
q[rear].step=q[front].step+1;
if(mat[nh][nx][ny] == 'E')
{
ans=q[rear].step;
return 1;
}
rear++;
}
}
front++;
}
return 0;
}
int main()
{
while(~scanf("%d %d %d%*c",&l,&r,&c) && l+r+c)
{
memset(mat,'#',sizeof(mat));
memset(vis,0,sizeof(vis));
int i,j,k,ok=0;
for(i=1;i<=l;i++)
{
for(j=1;j<=r;j++)
{
for(k=1;k<=c;k++)
scanf("%c",&mat[i][j][k]);
getchar();
}
getchar();
}
for(i=1;i<=l;i++)
{
for(j=1;j<=r;j++)
{
for(k=1;k<=c;k++)
if(mat[i][j][k]=='S')
{
ok=1;
break;
}
if(ok) break;
}
if(ok) break;
}

ans=0;
if(bfs(i,j,k)) printf("Escaped in %d minute(s).\n",ans);
else printf("Trapped!\n");
}
return 0;
}



内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: