您的位置:首页 > 编程语言 > C语言/C++

POJ 2251 Dungeon Master 比较有趣的三维迷宫bfs搜索路径

2016-04-20 17:08 621 查看
Dungeon Master
Crawling in process...
Crawling failed
Time Limit:1000MS    
Memory Limit:65536KB     64bit IO Format:%I64d & %I64u

  POJ
2251

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!

>>AC代码:

#include<iostream>
#include<string>
#include<vector>
#include<algorithm>
#include<queue>
using namespace std;
char map[31][31][31];
int flag[31][31][31];
int s[3],e[3];
int L,R,C;
int min_steps;
char x;
int per[6][3]={{1,0,0},{0,1,0},{0,0,1},{-1,0,0},{0,-1,0},{0,0,-1}};
struct point{
int x;
int y;
int z;
int step;
point(int a,int b,int c,int d):x(a),y(b),z(c),step(d){};
};
queue<point>pp;
bool inarrange(int x,int y,int z){
if(x>=0&&x<L&&y>=0&&y<R&&z>=0&&z<C){
return true;}
return false;
}
void bfs(int x,int y,int z){
point t(x,y,z,0);
pp.push(t);
flag[x][y][z]=1;
while(!pp.empty()){
point t2=pp.front();
if(t2.x==e[0]&&t2.y==e[1]&&t2.z==e[2]){
min_steps=t2.step;
return;
}
pp.pop();
for(int i=0;i<6;i++){
int newx=t2.x+per[i][0];
int newy=t2.y+per[i][1];
int newz=t2.z+per[i][2];
int nstep=t2.step+1;
if(inarrange(newx,newy,newz)&&flag[newx][newy][newz]==0&&map[newx][newy][newz]!='#'){
point t3(newx,newy,newz,nstep);
pp.push(t3);//判断不是墙就可以走,
因为终点和起点也走了一次,还有bfs一旦第一次到达终点一定是最短路,因为每一回合走的步数都相平
flag[newx][newy][newz]=1;
}
}
}
return;
}

int main(){
while(cin>>L>>R>>C&&(L!=0||R!=0||C!=0)){
memset(map,0,sizeof map);
memset(flag,0,sizeof flag);
while(!pp.empty()){
pp.pop();
}//这一步注意要加,相当于vector.clear();
for(int i=0;i<L;i++){
for(int j=0;j<R;j++){
for(int k=0;k<C;k++){
cin>>x;
map[i][j][k]=x;
if(x=='S'){
s[0]=i;s[1]=j;s[2]=k;}
else if(x=='E'){
e[0]=i;e[1]=j;e[2]=k;}
}
}
}
min_steps=-0x3ffff;
bfs(s[0],s[1],s[2]);
if(min_steps>0)printf("Escaped in %d minute(s).\n",min_steps);
else
cout<<"Trapped!"<<endl;
}
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息