POJ 2251 Dungeon Master

Dungeon Master

Time Limit: 1000ms
Memory Limit: 65536KB
This problem will be judged on PKU. Original ID: 2251
64-bit integer IO format: %lld      Java class name: Main
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!

Source

 
解题:搜索。。。
 1 #include <iostream>
 2 #include <cstdio>
 3 #include <cstring>
 4 #include <cmath>
 5 #include <cstdlib>
 6 #include <algorithm>
 7 #include <stack>
 8 #include <set>
 9 #include <map>
10 #include <queue>
11 #include <ctime>
12 #define LL long long
13 #define INF 0x3f3f3f3f
14 #define pii pair<int,int>
15 
16 using namespace std;
17 const int maxn = 40;
18 char table[maxn][maxn][maxn];
19 int L,R,C,sx,sy,sz,ex,ey,ez;
20 bool vis[maxn][maxn][maxn];
21 struct node {
22     int x,y,z,step;
23     node(int tx = 0,int ty = 0,int tz = 0,int tp = 0) {
24         x = tx;
25         y = ty;
26         z = tz;
27         step = tp;
28     }
29 };
30 const int dir[6][3] = {
31     {0,0,-1},//left
32     {0,0,1},//right
33     {0,-1,0},//up
34     {0,1,0},//down
35     {-1,0,0},//front
36     {1,0,0}//back
37 };
38 bool isIn(const node &tmp) {
39     int a = (tmp.x >= 0 && tmp.x < L);
40     int b = (tmp.y >= 0 && tmp.y < R);
41     int c = (tmp.z >= 0 && tmp.z < C);
42     return a + b + c == 3;
43 }
44 queue<node>q;
45 int bfs() {
46     while(!q.empty()) q.pop();
47     q.push(node(sx,sy,sz,0));
48     vis[sx][sy][sz] = true;
49     while(!q.empty()) {
50         node now = q.front();
51         q.pop();
52         if(table[now.x][now.y][now.z] == 'E') return now.step;
53         for(int i = 0; i < 6; ++i) {
54             node tmp(now.x+dir[i][0],now.y+dir[i][1],now.z+dir[i][2],now.step+1);
55             if(isIn(tmp)&&table[tmp.x][tmp.y][tmp.z] != '#' &&!vis[tmp.x][tmp.y][tmp.z]) {
56                 vis[tmp.x][tmp.y][tmp.z] = true;
57                 if(table[tmp.x][tmp.y][tmp.z] == 'E') return tmp.step;
58                 q.push(tmp);
59             }
60         }
61     }
62     return -1;
63 }
64 int main() {
65     while(scanf("%d %d %d",&L,&R,&C),L||R||C) {
66         memset(table,'',sizeof(table));
67         memset(vis,false,sizeof(vis));
68         for(int i = 0; i < L; ++i) {
69             for(int j = 0; j < R; ++j) {
70                 scanf("%s",table[i][j]);
71                 for(int k = 0; k < C; ++k)
72                     if(table[i][j][k] == 'S') {
73                         sx = i;
74                         sy = j;
75                         sz = k;
76                     }
77             }
78         }
79         int ans = bfs();
80         if(~ans) printf("Escaped in %d minute(s).
",ans);
81         else puts("Trapped!");
82     }
83     return 0;
84 }
View Code
原文地址:https://www.cnblogs.com/crackpotisback/p/4236289.html