joj 1313Dungeon Master

Dungeon Master

Result TIME Limit MEMORY Limit Run Times AC Times JUDGE
3s 8192K 367 129 Standard

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 Specification

The input file 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 Specification

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 <queue>
using namespace std;


struct p 
{
	int x, y, z,d;
};


int vis[35][35][35], dis[35][35][35];
int dir[6][3] = {{1,0,0},{-1,0,0},{0,0,1},{0,0,-1},{0,1,0},{0,-1,0}};


int main()
{
	queue <p> q;
	char g[35][35][35];
	int m, n,t,k, i, j, count; 
	int sx, sy, sz, ex, ey, ez;
	int xx, yy ,zz;
	while(cin >> t >> m >> n,t,m,n)
	{
		count=1;
		while(!q.empty())
		{
			q.pop();
		}
		for(k = 0; k < t; k++)
		{
			for(i = 0; i < m; i++)
			{
				for(j = 0; j < n; j++)
				{
					cin >> g[k][i][j];
					vis[k][i][j] = 0;
					dis[k][i][j] = 0;
				}
			}
		}
		for(k = 0; k < t; k++)
		{
			for(i = 0; i < m; i++)
			{
				for(j = 0; j < n; j++)
				{
					if(g[k][i][j] == 'S')
					{
						sx = j;
						sy = i;
						sz = k;
					}
					if(g[k][i][j] == 'E')
					{
						ex = j;
						ey = i;
						ez = k;
					}
				}
			}
		}
		p temp, cur;
		temp.x = sx;
		temp.y = sy;
		temp.z = sz;
		temp.d = 0;
		q.push(temp);
		while(!q.empty())
		{
			cur = q.front();
			q.pop();
			if(cur.z == ez && cur.x == ex && cur.y == ey)
			{
				cout << "Escaped in " << dis[ez][ey][ex] <<" minute(s)."<< endl;
				count= 0;
				break;
			}
			for(i = 0; i < 6; i++)
			{
				yy = cur.y + dir[i][0];
				xx = cur.x + dir[i][1];
				zz = cur.z + dir[i][2];
				if(g[zz][yy][xx] != '#' && vis[zz][yy][xx] == 0 && n > xx && xx >= 0 && m > yy && yy >= 0&& t > zz && zz >= 0)
				{
					temp.x = xx;
					temp.y = yy;
					temp.z = zz;
					temp.d = cur.d + 1;
					dis[zz][yy][xx] = cur.d + 1;
					q.push(temp);
					vis[zz][yy][xx] = 1;
				}	
			}
		}
		if(vis[ez][ey][ex] == 0 && count )
		{
			cout << "Trapped!" << endl;
		}
		else if( count != 0)
		{
			cout << "Escaped in " << dis[ez][ey][ex] <<" minute(s)."<< endl;
		}
	}
	return 0;
}


  
	
	  

你可能感兴趣的:(File,input,UP,character,each,output)