Dungeon Master
Time Limit: 1000MS |
|
Memory Limit: 65536K |
Total Submissions: 20450 |
|
Accepted: 7917 |
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!
Source
ULM Local 1997
A simple BFS to find the shortest path, but the background is three-dimensional just.
AC Code:
#include <map> #include <set> #include <cmath> #include <deque> #include <queue> #include <stack> #include <cstdio> #include <cctype> #include <string> #include <vector> #include <cstdlib> #include <cstring> #include <iostream> #include <algorithm> #define LL long long# Define INF 0x7fffffffusing namespace Std;int mp[35][35][35];int vis[35][35][35];//go in six directions const int dx[] = {1, 0, 0,-1, 0, 0};const int dy[] = {0, 1, 0, 0,-1, 0};const int dz[] = {0, 0, 1, 0, 0, -1};int L, R, C;int sx, SY, Sz;int ans;struct No de {int x, y, z;int c;node (int _x, int _y, int _z, int _c) {x = _x;y = _y;z = _z;c = _c;}}; BOOL BFS (int x, int y, int z) {memset (Vis, 0, sizeof (VIS));queue<node> Que;que.push (node (x, Y, Z, 0)); Vis[x][y][z] = 1;while (!que.empty ()) {node T = Que.front (); Que.pop (); for (int i = 0; i < 6; i + +) {int xx = t.x + Dx[i];int yy = T.y + Dy[i];int zz = t.z + dz[i];if (xx >= 0 && xx < L && yy >= 0 && yy < R && zz >= 0 && ZZ < C && Mp[xx][yy][zz]! = 1 &&!vis[xx][yy] [ZZ]) {//Well, <l,<r,<c all written <=, inexplicable kneeling for a long time, silly. if (mp[xx][yy][zz] = = 4) {ans = t.c + 1;return true;} Que.push (node (xx, yy, ZZ, t.c + 1)); vis[xx][yy][zz] = 1;}}} return false;} int main () {while (scanf ("%d%d", &l, &r, &c)! = EOF) {if (L = = 0 && R = = 0 && C = = 0) break; for (int i = 0; i < L; i + +) {for (int j = 0; J < R; J + +) {char str[55];scanf ('%s ', str); for (int k = 0; k < C; K + +) {if (str[k] = = '. ') {Mp[i][j][k] = 0;} else if (str[k] = = ' # ') {mp[i][j][k] = 1;} else if (str[k] = = ' S ') {mp[i][j][k] = 3;SX = i; sy = j; sz = k;} else if (str[k] = = ' E ') {mp[i][j][k] = 4;}}}} for (int i = 0; I < L + +, printf ("\ n")) {//for (int j = 0; J < R; J + +, printf ("\ n")) {//for (int k = 0; k < C; K + +) {//printf ("%d", Mp[i][j][k]),//}//}//}ans = 0;if (BFS (SX, SY, SZ)) {printf ("escaped in%d minute (s). \ n", ans); else {printf ("trapped!\n");}}return 0;}
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.
Poj-2251-dungeon Master (Simple BFS)