poj 2251 Dungeon Master

题目连接

http://poj.org/problem?id=2251

Dungeon Master

Description

You are trapped in a 3D dungeon and need to find the quickest way out! The dungeon is composed of unit cubes which may or may not be 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 is surrounded by solid rock on all sides. 

Is an escape possible? If yes, how long will it take? 

Input

The input consists of a number of dungeons. Each dungeon description starts with a line containing three integers L, R and C (all limited to 30 in size). 
L is the number of levels making up the dungeon. 
R and C are the number of rows and columns making up the plan of each level. 
Then there will follow L blocks of R lines each containing C characters. Each character describes one cell of the dungeon. A cell full of rock is indicated by a '#' and empty cells are 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 is not 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 11 minute(s).
Trapped!

三维迷宫。。

 1 #include<algorithm>
 2 #include<iostream>
 3 #include<cstdlib>
 4 #include<cstring>
 5 #include<cstdio>
 6 #include<vector>
 7 #include<queue>
 8 #include<map>
 9 using std::cin;
10 using std::cout;
11 using std::endl;
12 using std::find;
13 using std::sort;
14 using std::map;
15 using std::pair;
16 using std::queue;
17 using std::vector;
18 using std::multimap;
19 #define pb(e) push_back(e)
20 #define sz(c) (int)(c).size()
21 #define mp(a, b) make_pair(a, b)
22 #define all(c) (c).begin(), (c).end()
23 #define iter(c) decltype((c).begin())
24 #define cls(arr,val) memset(arr,val,sizeof(arr))
25 #define cpresent(c, e) (find(all(c), (e)) != (c).end())
26 #define rep(i, n) for (int i = 0; i < (int)(n); i++)
27 #define tr(c, i) for (iter(c) i = (c).begin(); i != (c).end(); ++i)
28 const int N = 32;
29 typedef unsigned long long ull;
30 char G[N][N][N];
31 bool vis[N][N][N];
32 int L, R, C, Sx, Sy, Sz, Dx, Dy, Dz;
33 const int dx[] = { -1, 1, 0, 0, 0, 0 };
34 const int dy[] = { 0, 0, -1, 1, 0, 0 }, dz[] = { 0, 0, 0, 0, -1, 1 };
35 struct Node {
36     int x, y, z, s;
37     Node() {}
38     Node(int i, int j, int k, int l) :x(i), y(j), z(k), s(l) {}
39 };
40 int bfs() {
41     cls(vis, false);
42     queue<Node> q;
43     q.push(Node(Sx, Sy, Sz, 0));
44     vis[Sx][Sy][Sz] = true;
45     while (!q.empty()) {
46         Node t = q.front(); q.pop();
47         if (t.x == Dx && t.y == Dy && t.z == Dz) return t.s;
48         rep(i, 6) {
49             int x = t.x + dx[i], y = t.y + dy[i], z = t.z + dz[i];
50             if (x < 0 || x >= L || y < 0 || y >= R || z < 0 || y >= C) continue;
51             if (vis[x][y][z] || G[x][y][z] == '#') continue;
52             q.push(Node(x, y, z, t.s + 1));
53             vis[x][y][z] = true;
54         }
55     }
56     return 0;
57 }
58 int main() {
59 #ifdef LOCAL
60     freopen("in.txt", "r", stdin);
61     freopen("out.txt", "w+", stdout);
62 #endif
63     while (~scanf("%d %d %d", &L, &R, &C) && L + R + C) {
64         rep(i, L) {
65             rep(j, R) {
66                 scanf("%s", G[i][j]);
67                 rep(k, C) {
68                     if (G[i][j][k] == 'S') Sx = i, Sy = j, Sz = k;
69                     if (G[i][j][k] == 'E') Dx = i, Dy = j, Dz = k;
70                 }
71             }
72         }
73         int ret = bfs();
74         if (!ret) puts("Trapped!");
75         else printf("Escaped in %d minute(s).
", ret);
76      }
77     return 0;
78 }
View Code
By: GadyPu 博客地址:http://www.cnblogs.com/GadyPu/ 转载请说明
原文地址:https://www.cnblogs.com/GadyPu/p/4621883.html