POJ 2251 Dungeon Master (Guang suo)
Dungeon Master
Time Limit:1000 MS |
|
Memory Limit:65536 K |
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 '. '. 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 5S.....###..##..###.#############.####...###########.#######E1 3 3S###E####0 0 0
Sample Output
Escaped in 11 minute(s).Trapped!
Extend the search in two dimensions to three dimensions, water problems, and pay attention to the representation of 3D coordinates in the three-dimensional array.
# Include
# Include
# Include
# Include 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] = '#'; // note that sz, sy, sx, instead of sx, sy, sz !!! Queue
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 ", & L, & R, & C) {if (L = 0 & R = 0 & C = 0) break; for (I = 1; I <= L; I ++) {// zfor (j = 1; j <= R; j ++) // yfor (k = 1; k <= C; k ++) {// xscanf ("% 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 (re S = 0) printf ("Trapped! \ N "); elseprintf (" Escaped in % d minute (s). \ n ", res);} return 0 ;}