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 5
S ....
.###.
.##..
###.#
#####
#####
##.##
##...
#####
#####
#.###
# # #E
1 3 3
s##
#E #
###
0 0 0 Sample Output
Escaped in minute (s).
trapped!
To inspire a problem, from the two-dimensional to three-dimensional, in fact, even to the four-dimensional space, the method is similar, let us see the bfs+ to find the strength of the heavy.
10000*z+100*x+y, my sentence weight function.
#include <cstdio> #include <cstring> #include <iostream> #include <algorithm> #include <
queue> #include <set> const int n=30+10;
using namespace Std;
struct state{int z,x,y,step;};
Queue<state> Q;
Set<int> S;
int L,r,c,goalx,goaly,goalz,x,y,z,code;
char tmp;
int dx[6]={1,-1,0,0,0,0}, dy[6]={0,0,1,-1,0,0}, dz[6]={0,0,0,0,1,-1};
int a[n][n][n]; void BFs () {while (! Q.empty ()) {State t=q.front ();
Q.pop ();
for (int i=0;i<6;i++) {x=t.x+dx[i];
Y=t.y+dy[i];
Z=t.z+dz[i]; if (a[z][x][y]==1| | x<0| | x>=r| | z<0| | z>=l| | y<0| |
Y>=C) continue;
Code=10000*z+100*x+y; if (X==goalx&&y==goaly&&z==goalz) {cout<< "escaped in" <<t.step+1<< "Minu Te (s). "
<<endl;
Return
} if (S.find (code) ==s.end ()) {S.insert (code); StAte node={z,x,y,t.step+1};
Q.push (node); }}} cout<< "trapped!"
<<endl; } int main () {while (cin>>l>>r>>c) {if (! l&&! r&&!
C) break;
memset (A,0,sizeof (a));
S.clear (); while (!
Q.empty ()) Q.pop ();
for (int i=0;i<l;i++) {for (int. j=0;j<r;j++) {for (int k=0;k<c;k++) {
cin>>tmp;
if (tmp== ' S ') {State first={i,j,k,0};
Q.push (first);
Code=10000*i+100*j+k;
S.insert (code);
a[i][j][k]=0;
} else if (tmp== ' E ') {goalz=i;
Goalx=j;
Goaly=k;
a[i][j][k]=0; } else if (tmp== ' # ') {A[i][j][K]=1; } else if (tmp== '. ')
{a[i][j][k]=0;
}}}} BFS ();
} return 0; }