A - Dungeon Master

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

三维的迷宫.其中用'.'表示空地,'#'表示障碍物,'S'表示起点,'E'表示终点,

东南西北上下搜索。

<pre name="code" class="plain">#include <stdio.h>
#include <string.h>
#include <queue>
using namespace std;
struct Node
{
	int x;
	int y;
	int z;
	int minute;
};
Node coordinate;
char map[32][32][32];
int used[32][32][32];
int i,j,k;
int main()
{
	int l,m,n;
	while(scanf("%d%d%d",&l,&m,&n) && (l+m+n))
	{
		memset(map,'#',sizeof(map));
		memset(used,0,sizeof(used));
		for(i=1;i<=l;i++)
		{
			for(j=1;j<=m;j++)
			{
				scanf("%s",&map[i][j][1]);
				for(k=1;k<=n;k++)
				{
					if(map[i][j][k]=='S')
					{
						coordinate.x = i;
						coordinate.y = j;
						coordinate.z = k;
						coordinate.minute = 0;
						used[i][j][k] = 1;
						
					}
				}
			}
		}
		int f = 0;
		queue<Node>Q;
		Q.push(coordinate);
		while(!Q.empty())
		{
			coordinate = Q.front();
			Q.pop();	
			
			i = coordinate.x;
			j = coordinate.y;
			k = coordinate.z;			
			if(map[i][j][k] == 'E')
			{
				f = 1;
				break;
			}
			coordinate.minute ++ ;	
			coordinate.x = i+1;
			coordinate.y = j;
			coordinate.z = k;
			if(map[coordinate.x][coordinate.y][coordinate.z] != '#' && used[coordinate.x][coordinate.y][coordinate.z]==0)
			{
				 used[coordinate.x][coordinate.y][coordinate.z]=1;
				Q.push(coordinate);
			}
			coordinate.x = i-1;
			coordinate.y = j;
			coordinate.z = k;
			if(map[coordinate.x][coordinate.y][coordinate.z] != '#' && used[coordinate.x][coordinate.y][coordinate.z]==0)
			{
				 used[coordinate.x][coordinate.y][coordinate.z]=1;
				Q.push(coordinate);
			}
			coordinate.x = i;
			coordinate.y = j+1;
			coordinate.z = k;
			if(map[coordinate.x][coordinate.y][coordinate.z] != '#'&&  used[coordinate.x][coordinate.y][coordinate.z]==0)
			{
				 used[coordinate.x][coordinate.y][coordinate.z]=1;
				Q.push(coordinate);
			}
			coordinate.x = i;
			coordinate.y = j-1;
			coordinate.z = k;
			if(map[coordinate.x][coordinate.y][coordinate.z] != '#' &&  used[coordinate.x][coordinate.y][coordinate.z]==0 )
			{
				 used[coordinate.x][coordinate.y][coordinate.z]=1;
				Q.push(coordinate);
			}
			coordinate.x = i;
			coordinate.y = j;
			coordinate.z = k+1;
			if(map[coordinate.x][coordinate.y][coordinate.z] != '#' && used[coordinate.x][coordinate.y][coordinate.z]==0 )
			{
				 used[coordinate.x][coordinate.y][coordinate.z]=1;
				Q.push(coordinate);
			}
			coordinate.x = i;
			coordinate.y = j;
			coordinate.z = k-1;
			if(map[coordinate.x][coordinate.y][coordinate.z] != '#' && used[coordinate.x][coordinate.y][coordinate.z]==0 )
			{
				 used[coordinate.x][coordinate.y][coordinate.z]=1;
				Q.push(coordinate);
			}	
		}
		if(f)
		{
			printf("Escaped in %d minute(s).\n",coordinate.minute);
		}
		else
		{ 
			printf("Trapped!\n");
		}
	
	}
	return 0;
}


 

你可能感兴趣的:(A - Dungeon Master)