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 '. '. 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!
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 '. '. 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!
It is a three-character MAZE, Which is searched like a two-dimensional MAZE.
LANGUAGE: C ++
CODE:
# Include <cstdio>
# Include <cstring>
# Include <queue>
Using namespace std;
Const int dir [6] [3] = {0,-}, {, 0}, {,-1}, {, 1, 0 },{-, 0 }};
Struct node {int x, y, z, step ;};
Char matrix [31] [31] [31];
Bool used [31] [31] [31];
Int n;
Int startx, starty, startz, endx, endy, endz;
Int l, r, c;
Node cur, pre;
Int bfs ()
{
Int I, x, y, z;
Queue <node> que;
Cur. x = startx, cur. y = starty, cur. z = startz, cur. step = 0;
Memset (used, 0, sizeof (used ));
Used [startx] [starty] [startz] = 1;
Que. push (cur );
While (! Que. empty ())
{
Cur = que. front (), que. pop ();
If (cur. x = endx & cur. y = endy & cur. z = endz)
Return cur. step;
For (I = 0; I <6; I ++)
{
X = cur. x + dir [I] [0];
Y = cur. y + dir [I] [1];
Z = cur. z + dir [I] [2];
If (x> = 0 & x <l & y> = 0 & y <r & z> = 0 & z <= c &&! Used [x] [y] [z] & matrix [x] [y] [z]! = '#')
{
Used [x] [y] [z] = 1;
Pre. x = x, pre. y = y, pre. z = z, pre. step = cur. step + 1;
Que. push (pre );
}
}
}
Return-1;
}
Int main ()
{
Int I, j, k, ans;
While (scanf ("% d", & l, & r, & c), l | r | c)
{
Getchar ();
For (I = 0; I <l; I ++)
{
For (j = 0; j <r; j ++)
{
For (k = 0; k <c; k ++)
{
Scanf ("% c", & matrix [I] [j] [k]);
If (matrix [I] [j] [k] ='s ')
{
Startx = I, starty = j, startz = k;
}
Else if (matrix [I] [j] [k] = 'E ')
{
Endx = I, endy = j, endz = k;
}
}
Getchar ();
}
Getchar ();
}
Ans = bfs ();
If (ans =-1)
Printf ("Trapped! \ N ");
Else printf ("Escaped in % d minute (s). \ n", ans );
}
Return 0;
} Www.2cto.com
Author: ultimater