您的位置:首页 > 其它

POJ 2251 Dungeon Master ( BFS )

2016-07-28 17:51 323 查看
Dungeon Master

Time Limit: 1000MS Memory Limit: 65536K
Total Submissions: 26388 Accepted: 10263
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!

Source

Ulm Local 1997

#include<stdio.h>
#include<queue>
#include<string.h>
#include<algorithm>
using namespace std;
int dis[6][3]={{1,0,0},{-1,0,0},{0,1,0},{0,-1,0},{0,0,-1},{0,0,1}};
struct node
{
int x,y,z,cost;
}p[33000];
int vis[35][35][35];
int map[35][35][35];
int ex,ey,ez,dx,dy,dz;
int bfs(int k,int n,int m)
{
queue<node>q;
node temp,f;
temp.x=ex;
temp.y=ey;
temp.z=ez;
temp.cost=0;
q.push(temp);
memset(vis,0,sizeof(vis));
vis[ez][ex][ey]=1;
while(!q.empty())
{
temp=q.front();
q.pop();
for(int i=0;i<6;i++)
{
int sx=temp.x+dis[i][0];
int sy=temp.y+dis[i][1];
int sz=temp.z+dis[i][2];
if(sx==dx&&sy==dy&&sz==dz)
{
return temp.cost+1;
}
if(sx>=0&&sx<n&&sy>=0&&sy<m&&sz>=0&&sz<k&&map[sz][sx][sy]!=1&&vis[sz][sx][sy]==0)
{
vis[sz][sx][sy]=1;
f.x=sx;
f.y=sy;
f.z=sz;
f.cost=temp.cost+1;
q.push(f);
}

}
}
return -1;
}
int main()
{
int k,n,m,i,j,l;
char str[45];
while(scanf("%d%d%d",&l,&n,&m),l+n+m)
{
for(k=0;k<l;k++)
{
for(i=0;i<n;i++)
{
scanf("%s",str);
for(j=0;j<m;j++)
{
if(str[j]=='#')
map[k][i][j]=1;
else if(str[j]=='.')
map[k][i][j]=0;
else if(str[j]=='S')
{
ez=k;
ex=i;
ey=j;
}
else if(str[j]=='E')
{
dx=i;
dy=j;
dz=k;
}
}
}
}
int ans=bfs(l,n,m);
if(ans==-1)
printf("Trapped!\n");
else
printf("Escaped in %d minute(s).\n",ans);
}
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: