您的位置:首页 > 其它

POJ-2251 Dungeon Master

2016-07-29 18:10 288 查看
Dungeon Master

Time Limit: 1000MS Memory Limit: 65536K
Total Submissions: 26537 Accepted: 10341
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算法。


代码在下边:

#include <cstdio>
#include <cstring>
#include <cmath>
#include <algorithm>
#include <climits>
#include <iostream>
#include <queue>
using namespace std;
struct node{
int x,y,z,time;
};
node s,e;
int l,r,c,ans;
int fx[6]={1,-1,0, 0,0, 0};
int fy[6]={0, 0,1,-1,0, 0};
int fz[6]={0, 0,0, 0,1,-1};
char a;
int  vis[35][35][35];
int BFS(node s)
{
vis[s.z][s.x][s.y]=0;
queue<node>q;
q.push(s);
while(!q.empty())
{
node now=q.front();
q.pop();
for(int i=0;i<6;i++)
{
node n;
n.x=now.x+fx[i];
n.y=now.y+fy[i];
n.z=now.z+fz[i];
n.time=now.time;
if(vis[n.z][n.x][n.y]==1&&n.x>=0&&n.x<r&&n.y>=0&&n.y<c&&n.z>=0&&n.z<l)
{
vis[n.z][n.x][n.y]=0;
n.time++;
q.push(n);
if(n.z==e.z&&n.x==e.x&&n.y==e.y) return n.time;
}
}
}
return 0;
}
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++){
for(int k=0;k<c;k++){
cin>>a;
if(a=='S'){s.x=j;s.y=k;s.z=i;vis[i][j][k]=0;}
if(a=='E'){e.x=j;e.y=k;e.z=i;vis[i][j][k]=1;}
if(a=='.'){vis[i][j][k]=1;}
}
}
}//输入TM就这么长,你咋不上天!!!
ans=0;
s.time=0;
ans=BFS(s);
if(ans) printf("Escaped in %d minute(s).\n",ans);
else printf("Trapped!\n");
}
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: