Dungeon Master
Time Limit: 1000MS |
|
Memory Limit: 65536K |
Total Submissions: 18773 |
|
Accepted: 7285 |
Description
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?
Input
The 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.
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 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!
The search in the two-dimensional expansion to three-dimensional, water problems, note three-dimensional coordinates in three-dimensional array of the underlying expression is good.
#include <stdio.h> #include <string.h> #include <queue> #include <algorithm>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));p oint s;s.x=sx;s.y=sy;s.z=sz;s.cnt=0;map[sz][sy][sx]= ' # ';//Be sure to note that this corresponds to SZ,SY,SX, not SX,SY,SZ!!! Queue<point> 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%d%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 (res==0) printf ("trapped!\n"), elseprintf ("escaped in%d minute (s). \ n", res);} return 0;}
POJ 2251 Dungeon Master (Guang Search)