POJ 2251 Dungeon Master (广搜)

Dungeon Master
Time Limit: 1000MS   Memory Limit: 65536K
Total Submissions: 18773   Accepted: 7285

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!
将搜索在二维扩展到三维,水题,注意三维坐标在三维数组里下标的表示就好了。

#include <stdio.h>
#include <string.h>
#include <queue>
#include <algorithm>
using namespace std;

const int maxl=40;

typedef struct pp
{
	int x,y,z,cnt;
}point;

char map[maxl][maxl][maxl];
int vis[maxl][maxl][maxl];
int sx,sy,sz,ex,ey,ez,L,R,C;

int bfs(){
	int i;
	memset(vis,0,sizeof(vis));
	point s;
	s.x=sx;s.y=sy;s.z=sz;
	s.cnt=0;
	map[sz][sy][sx]='#';		//一定注意这里对应的是sz,sy,sx,而不是sx,sy,sz !!!
	queue<point> que;
	que.push(s);
	while(!que.empty()){
		point t=que.front();
		if(t.x==ex && t.y==ey && t.z==ez) return t.cnt;
		que.pop();
		for(i=-1;i<=1;i+=2)
			if(t.x+i>=1 && t.x+i<=C && map[t.z][t.y][t.x+i]!='#'){
				point tt;
				tt.x=t.x+i;tt.y=t.y;tt.z=t.z;tt.cnt=t.cnt+1;
				map[tt.z][tt.y][tt.x]='#';
				que.push(tt);
			}
		for(i=-1;i<=1;i+=2)
			if(t.y+i>=1 && t.y+i<=R && map[t.z][t.y+i][t.x]!='#'){
				point tt;
				tt.x=t.x;tt.y=t.y+i;tt.z=t.z;tt.cnt=t.cnt+1;
				map[t.z][t.y+i][t.x]='#';
				que.push(tt);
			}
		for(i=-1;i<=1;i+=2)
			if(t.z+i>=1 && t.z+i<=L && map[t.z+i][t.y][t.x]!='#'){
				point tt;
				tt.x=t.x;tt.y=t.y;tt.z=t.z+i;tt.cnt=t.cnt+1;
				map[t.z+i][t.y][t.x]='#';
				que.push(tt);
			}
	}
	return 0;
}






int main()
{
	int i,j,k,t,res;
	while(scanf("%d%d%d",&L,&R,&C)){
		if(L==0 && R==0 && C==0) break;
		for(i=1;i<=L;i++){	//z
			for(j=1;j<=R;j++)	//y
				for(k=1;k<=C;k++){	//x
					scanf(" %c",&map[i][j][k]);
					if(map[i][j][k]=='S'){
						sx=k;sy=j;sz=i;
					}
					if(map[i][j][k]=='E'){
						ex=k;ey=j;ez=i;
					}
				}
			getchar();getchar();
		}
		res=bfs();
		if(res==0)
			printf("Trapped!\n");
		else
			printf("Escaped in %d minute(s).\n",res);
	}
	return 0;
}


你可能感兴趣的:(poj)