Dungeon Master
Time Limit: 1000MS |
|
Memory Limit: 65536K |
Total Submissions: 20912 |
|
Accepted: 8106 |
Click to open link
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!
Test instructions
Give a dungeon of 13 dimensional space, asking for the shortest path from the character ' S ' to the character ' E '
The direction of movement can be east, west, south, north, Upper and lower six directions
Every move takes a minute and requires the fastest exit time.
Maps of different L layers, which are connected at the same RC coordinates
#无法行通
. Representative Road
Topic Analysis:
This problem is very similar to hdoj 1242 Rescue
But the problem is a little more complex three-dimensional, in fact, not much complexity, be careful is
Code:
#include <cstdio> #include <cstring> #include <queue> #include <cstdlib> #include <algorithm >using namespace std; #define MAX 35char map[max][max][max];int step[max][max][max];int l,r,c;int Sx,sy,sz,ex,ey,ez; int f[][3]={{1,0,0},{-1,0,0},{0,1,0},{0,-1,0},{0,0,1},{0,0,-1}};struct node{int x; int y; int z; int time;}; Queue<node>q;void input () {for (int i=0;i<l;i++) for (int. j=0;j<r;j++) for (int k=0;k<c;k++) {if (map I [j] [k]== ' S ') {sx=i;sy=j;sz=k; } if (map[i][j][k]== ' E ') {ex=i;ey=j;ez=k; } step[i][j][k]=-1; }}int valid (int x,int y,int z) {if (x>=0&&x<l&&y>=0&&y<r&&z>=0& &Z<C) return 1;else return 0;} void BFS () {node p1,p2;p1.x=sx;p1.y=sy;p1.z=sz;p1.time=0;step[p1.x][p1.y][p1.z]=0;while (!q.empty ())) Q.pop (); Q.push (p1); while (!q.empty ()) {P1=q.front (); Q.pop (); for (int i=0;i<6;i++)//Note here {P2.x=p1.x+f[i][0];p 2.y=p1.y+f[i][1];p 2.z=p1.z+f[i][2]; if (MAP[P2.X][P2.y][p2.z]!= ' # ' &&valid (p2.x,p2.y,p2.z)) {p2.time=p1.time+1; if (step[p2.x][p2.y][p2.z]==-1| | P2.time<step[p2.x][p2.y][p2.z]) {Q.push (p2); step[p2.x][p2.y][p2.z]=p2.time; }}}}}int main () {while (~scanf ("%d%d%d", &l,&r,&c), (l| | r| | C) {for (int i=0;i<l;i++) for (int j=0;j<r;j++) scanf ("%s", Map[i][j]); Input (); BFS (); if (step[ex][ey][ez]==-1) printf ("trapped!\n"); else printf ("escaped in%d minute (s). \ n", Step[ex][ey][ez]);} return 0;}
Read the people's Problem-solving report, I found that people's thinking more concise than mine so I again according to other people's thinking to paste a:
#include <cstdio> #include <cstring> #include <queue> #include <cstdlib> #include <algorithm >using namespace std; #define MAX 35char map[max][max][max];int vis[max][max][max];int step[max][max][max];int L,R,C ; int sx,sy,sz;int f[][3]={{1,0,0},{-1,0,0},{0,1,0},{0,-1,0},{0,0,1},{0,0,-1}};struct node{int x; int y; int z; int time;}; Queue<node>q;void input () {for (int i=0;i<l;i++) for (int. j=0;j<r;j++) for (int k=0;k<c;k++) {if (map I [j] [k]== ' S ') {sx=i;sy=j;sz=k; } vis[i][j][k]=0; }}int valid (int x,int y,int z) {if (x>=0&&x<l&&y>=0&&y<r&&z>=0& &Z<C) return 1;else return 0;} void BFS () {node p1,p2;p1.x=sx;p1.y=sy;p1.z=sz;p1.time=0;while (!q.empty ())) Q.pop (); Vis[p1.x][p1.y][p1.z]=1;q.push ( p1); while (!q.empty ()) {P1=q.front (); Q.pop (); for (int i=0;i<6;i++)//Note here {P2.x=p1.x+f[i][0];p 2.y=p1.y+f[i][1];p 2.z=p1.z+f[i][2]; if (map[p2.x][p2.y][p2.z]!= ' # ' &&valid (p2.x,p2.y,p2.Z) &&!vis[p2.x][p2.y][p2.z]) {p2.time=p1.time+1; if (map[p2.x][p2.y][p2.z]== ' E ')//Here directly returns the value, not so complex {printf ("escaped in%d minute (s). \ n", p2.time); return; } vis[p2.x][p2.y][p2.z]=1; Q.push (p2);} }}printf ("trapped!\n");} int main () {while (~scanf ("%d%d%d", &l,&r,&c), (l| | r| | C) {for (int i=0;i<l;i++) for (int j=0;j<r;j++) scanf ("%s", Map[i][j]); Input (); BFS ();} return 0;}
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.
POJ 2251 Dungeon Master