您的位置:首页 > 其它

POJ 2251 Dungeon Master

2013-04-24 11:29 239 查看
Dungeon Master

Time Limit: 1000MSMemory Limit: 65536K
Total Submissions: 13226Accepted: 5135
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
基础的BFS训练题。。。那个POJ训练计划上说用DFS。。。难道要遍历整个搜索树吗。。。
迷宫问题,三维的。仅此而已、

好久没写过BFS了,有些手生

还有,Discuss中有人说数据很大,我测试了下,迷宫小于30^3题目描述应该是没错的

还发现个问题。POJ中用G++编译器不支持printf。。。。加了#include<cstdio>也会CE,不知道为什么

#include <iostream>
#include <string>
using namespace std;

const int maxn=31,maxm=30000;
string map[maxn][maxn];
int dx[6]={-1, 1, 0, 0, 0, 0};
int dy[6]={ 0, 0,-1, 1, 0, 0};
int dz[6]={ 0, 0, 0, 0,-1, 1};
int n,m,p,visit[maxn][maxn][maxn];

struct point{
int x,y,z;
int step;
}queue[maxm];

int check(int,int,int);
int bfs(int,int,int);
int main()
{
int i,j,k,x,y,z;
while(cin>>p>>m>>n && p!=0){
for (i=0;i<p;++i)
for (j=0;j<m;++j){
cin>>map[i][j];
for (k=0;k<n;++k){
visit[i][j][k]=0;
if(map[i][j][k]=='S'){
z=i; x=k; y=j;
}
}

}
//	memset(queue,0,sizeof(queue));
int ans=bfs(x,y,z);
if (ans==-1) cout<<"Trapped!\n";
else cout<<"Escaped in "<<ans<<" minute(s).\n";
}
return 0;
}

int check(int x,int y,int z){
if (x>=0 && x<n && y>=0 && y<m && z>=0 && z<p
&& !visit[z][y][x] && map[z][y][x] != '#') return 1;
else return 0;
}

int bfs(int x,int y, int z){
int i,fa=0,so=0; point temp;
visit[z][y][x]=1;
queue[0].x=x; queue[0].y=y;
queue[0].z=z; queue[0].step=0;
while (so>=fa && so<maxm){
for (i=0;i<6;++i){
temp.x=queue[fa].x+dx[i];
temp.y=queue[fa].y+dy[i];
temp.z=queue[fa].z+dz[i];
if (check(temp.x,temp.y,temp.z))
if (map[temp.z][temp.y][temp.x]=='E')
return queue[fa].step+1;
else{
visit[temp.z][temp.y][temp.x]=1;
queue[++so]=temp;
queue[so].step=queue[fa].step+1;
}
}
fa++;
}
return -1;
}


kdwycz的网站: http://kdwycz.com/

kdwyz的刷题空间:http://blog.csdn.net/kdwycz
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: