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!
is a person caught in the dungeon, the dungeon has several layers (L), each layer of r*c, the mark # of the place can not go, each time you may go up, down, left, right, before, after walking a neighboring lattice, spending 1 minutes. Ask you to walk from S, can escape from this dungeon (go to e).
This is my earnest write the first Dfs, is not a bit late? Oh. Summarize some of the main points of Dfs.
1.struct node is used to mark the coordinates of points, and the number of steps to reach (if possible) the store.
2.vis dir Array, indicating whether the direction of each walk is accessed
3.check function: Check whether the 3-point 1-> is beyond the boundary 2-> whether to walk through 3->
4. The BFS function based on the queue implementation, explained in detail here
First we set up a node queue q, we first add the starting point into the queue. From the beginning of the team of Q, use temp to get this team first, Q.pop (). The node that can go through temp will be added to the queue, and the end is determined.
So knowing that the queue was empty, we went through all the possible ways.
The code is as follows:
1#include <cstdio>2#include <iostream>3#include <cmath>4#include <queue>5#include <cstring>6 using namespacestd;7 structnode8 {9 intl,x,y;Ten intStep; One }; A intl,r,c; - intEl,ex,ey,sl,sx,sy,ans; - Charmp[ +][ +][ +]; the BOOLvis[ +][ +][ +]; - intdir[6][3]={1,0,0, -1,0,0,0,1,0,0,-1,0,0,0,1,0,0,-1}; - BOOLCheck (node now) - { + if(now.l>=l| | now.l<0|| -now.x>=r| | now.x<0|| +now.y>c| | now.y<0) A return 0; at if(mp[now.l][now.x][now.y]=='#'||Vis[now.l][now.x][now.y]) - return 0; - return 1; - } - voidBFS (intLevelintXxintyy) - { inQueue<node>Q; - node now; tonow.l=level,now.x=xx,now.y=yy,now.step=0; +vis[level][xx][yy]=true; - Q.push (now); the while(!q.empty ()) * { $Node temp=Q.front ();Panax Notoginseng Q.pop (); - for(intI=0;i<6;++i) the { +now.l=temp.l+dir[i][0]; Anow.x=temp.x+dir[i][1]; thenow.y=temp.y+dir[i][2]; +now.step=temp.step+1; - ifCheck (now) $ { $ if(mp[now.l][now.x][now.y]=='E') - { -ans=Now.step; the return; - }Wuyivis[now.l][now.x][now.y]=true; the Q.push (now); - } Wu } - } About } $ intMain () - { - //freopen ("De.txt", "R", stdin); - while(~SCANF ("%d%d%d",&l,&r,&C)) A { + if(l==0&&r==0&&c==0) the Break; -el=ex=ey=0; $sl=sx=sy=0; thememset (Vis,false,sizeofvis); the for(intI=0; i<l;++i) the { the for(intj=0; j<r;++j) - { in for(intk=0; k<c;++k) the { theCin>>Mp[i][j][k]; About if(mp[i][j][k]=='S') the { thesl=i,sx=j,sy=K; the } + if(mp[i][j][k]=='E') - { theel=i,ex=j,ey=K;Bayi } the } the } - } -ans=-1; the BFS (sl,sx,sy); the if(ans!=-1) theprintf"escaped in%d minute (s). \ n", ans); the Else -printf"trapped!\n"); the } the return 0; the}
POJ 2251 Dungeon Master (DFS)