Dungeon Mastertime limit:1000msmemory limit:65536kbthis problem would be judged onPKU. Original id:2251
64-bit integer IO format: %lld Java class name: Main You is trapped in a 3D dungeon and need to find the quickest-on-the-do out! The dungeon is composed of a unit cubes which may or may isn't being 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 are surrounded by solid rock in all sides.
is an escape possible? If Yes, how long would it take?
InputThe input consists of a number of dungeons. Each of the dungeon description starts with a line containing three integers L, R and C (all limited to the size).
L is the number of levels making up the dungeon.
R and C is the number of rows and columns making up the plan of each level.
Then there'll follow L blocks of R lines each containing C characters. Each character describes one cell of the dungeon. A cell full of rock was indicated by a ' # ' and empty cells were 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.OutputEach 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 isn't possible to escape, print the line
trapped!
Sample Input
3 4 5s.....###. ##.. ###.#############.####...###########.###### #E1 3 3s## #E # # #0 0 0
Sample Output
Escaped in minute (s). trapped!
SourceULM Local 1997 Problem solving: Searching ...
1#include <iostream>2#include <cstdio>3#include <cstring>4#include <cmath>5#include <cstdlib>6#include <algorithm>7#include <stack>8#include <Set>9#include <map>Ten#include <queue> One#include <ctime> A #defineLL Long Long - #defineINF 0x3f3f3f3f - #definePII pair<int,int> the - using namespacestd; - Const intMAXN = +; - CharTABLE[MAXN][MAXN][MAXN]; + intL,r,c,sx,sy,sz,ex,ey,ez; - BOOLVIS[MAXN][MAXN][MAXN]; + structNode { A intX,y,z,step; atNodeinttx =0,intTy =0,intTZ =0,intTP =0) { -x =TX; -y =Ty; -z =TZ; -Step =TP; - } in }; - Const intdir[6][3] = { to{0,0,-1},// Left +{0,0,1},// Right -{0,-1,0},// up the{0,1,0},// Down *{-1,0,0},//Front ${1,0,0}// BackPanax Notoginseng }; - BOOLIsIn (ConstNode &tmp) { the intA = (tmp.x >=0&& tmp.x <L); + intb = (Tmp.y >=0&& Tmp.y <R); A intc = (Tmp.z >=0&& Tmp.z <C); the returnA + b + c = =3; + } -Queue<node>Q; $ intBFs () { $ while(!q.empty ()) Q.pop (); -Q.push (Node (Sx,sy,sz,0)); -VIS[SX][SY][SZ] =true; the while(!Q.empty ()) { -Node now =Q.front ();Wuyi Q.pop (); the if(Table[now.x][now.y][now.z] = ='E')returnNow.step; - for(inti =0; I <6; ++i) { WuNode tmp (now.x+dir[i][0],now.y+dir[i][1],now.z+dir[i][2],now.step+1); - if(IsIn (TMP) &&TABLE[TMP.X][TMP.Y][TMP.Z]! ='#'&&!Vis[tmp.x][tmp.y][tmp.z]) { AboutVIS[TMP.X][TMP.Y][TMP.Z] =true; $ if(Table[tmp.x][tmp.y][tmp.z] = ='E')returnTmp.step; - Q.push (TMP); - } - } A } + return-1; the } - intMain () { $ while(SCANF (" %d%d%d", &l,&r,&c), l| | r| |C) { thememset (table,' /',sizeof(table)); thememset (Vis,false,sizeof(Vis)); the for(inti =0; i < L; ++i) { the for(intj =0; J < R; ++j) { -scanf"%s", Table[i][j]); in for(intK =0; K < C; ++k) the if(Table[i][j][k] = ='S') { theSX =i; AboutSY =J; theSZ =K; the } the } + } - intAns =BFS (); the if(~ans) printf ("escaped in%d minute (s). \ n", ans);Bayi ElsePuts"trapped!"); the } the return 0; -}View Code
POJ 2251 Dungeon Master