Dungeon Master

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<iostream>
#include<cstdio>
#include<queue>
#include<cstring>
using namespace std;
struct node{
	int x,y,z,s;
};
int next[6][3]={{0,1,0},{0,-1,0},{1,0,0},{-1,0,0},{0,0,1},{0,0,-1}};
int n,m,k,endx,endy,endz,flag;
char e[35][35][35];
int bfs(int a,int b,int c){
	struct node t1,t2,t3;
	queue<node> q;
	t1.x=a;t1.y=b;t1.z=c;t1.s=0;
	q.push(t1);
	while(!q.empty()){
		t2=q.front();q.pop();
		for(int i=0;i<6;i++){
			t3.x=t2.x+next[i][0];
			t3.y=t2.y+next[i][1];
			t3.z=t2.z+next[i][2];
		    t3.s=t2.s+1;
		  if(t3.x<0 || t3.x>=n || t3.y<0 || t3.y>=m || t3.z<0 || t3.z>=k)
		   continue;
		  if(e[t3.x][t3.y][t3.z]!='#'){
			if(t3.x==endx && t3.y==endy && t3.z==endz){
				 flag=1;
				 return t3.s;
			}
				q.push(t3);
				e[t3.x][t3.y][t3.z]='#';
		  }
	    }
	}
}
int main(){
	int startx,starty,startz;
	while(cin >> n >> m >> k){
		if(n==0 && m==0 && k==0) break;
		for(int i=0;i<n;i++){
		  for(int j=0;j<m;j++){
		  	for(int r=0;r<k;r++){
		  		cin >> e[i][j][r];
		  		if(e[i][j][r]=='S'){
		  			startx=i;starty=j;startz=r;
		  		}
		  		if(e[i][j][r]=='E'){
		  			endx=i;endy=j;endz=r;
		  		}
		  	}
		  }
	    }
	    flag=0;
	     int sum=bfs(startx,starty,startz);
	     	if(flag==1)
	          printf("Escaped in %d minute(s).\n",sum);
	        else
	          cout << "Trapped!" << endl;
	}
	return 0;
}

你可能感兴趣的:(搜索)