POJ2251 Dungeon Master【3维空间BFS】

Dungeon Master

Time Limit: 1000MS   Memory Limit: 65536K
Total Submissions: 49402   Accepted: 18663

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

问题链接:POJ2251 Dungeon Master

问题描述:在一个3维空间中,一个点可以向x,y,z方向移动一个单位长度花费1分钟,问能否从'S'出发到达'E',如果不行输出Trapped!。否则输出需要花费的最短时间。

解题思路:在3维空间上使用bfs

AC的C++代码:

#include<iostream>
#include<queue>
#include<cstring>

using namespace std;

const int N=33;
int L,R,C;
bool vis[N][N][N];

struct DIR{
	int x,y,z;
}d[6]={{-1,0,0},{1,0,0},{0,-1,0},{0,1,0},{0,0,1},{0,0,-1}};

struct Node{
	int x,y,z,t;
	Node(){}
	Node(int x,int y,int z,int t):x(x),y(y),z(z),t(t){}
};

int bfs(Node s,Node e)
{
	queue<Node>q;
	q.push(s);
	while(!q.empty()){
		Node f=q.front();
		q.pop();
		if(f.x==e.x&&f.y==e.y&&f.z==e.z)
		  return f.t; 
		for(int i=0;i<6;i++){
			int x=f.x+d[i].x;
			int y=f.y+d[i].y;
			int z=f.z+d[i].z;
			int t=f.t+1;
			if(0<x&&x<=L&&0<y&&y<=R&&0<z&&z<=C&&!vis[x][y][z]){
				vis[x][y][z]=true;
				q.push(Node(x,y,z,t));
			}
		}
	}
	return -1;//没找到返回-1 
}

int main()
{
	char c;
	Node s,e;
	while(~scanf("%d%d%d",&L,&R,&C)&&(L||R||C)){
		memset(vis,false,sizeof(vis));
		for(int i=1;i<=L;i++)
		  for(int j=1;j<=R;j++)
		    for(int k=1;k<=C;k++){
		    	scanf(" %c",&c);
		    	if(c=='S'){
		    		s.x=i,s.y=j,s.z=k,s.t=0;
		    		vis[i][j][k]=true;
				}
				else if(c=='E')
					e.x=i,e.y=j,e.z=k,e.t=0;
				else if(c=='#')
				  vis[i][j][k]=true;
			}
		int ans=bfs(s,e);
		if(ans==-1)
		  printf("Trapped!\n");
		else
		  printf("Escaped in %d minute(s).\n",ans);
	}
	return 0;
 } 

猜你喜欢

转载自blog.csdn.net/SongBai1997/article/details/82859900