Dungeon Master
Time Limit: 1000MS   Memory Limit: 65536K
Total Submissions: 32228   Accepted: 12378

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

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!

普通的bfs迷宫题,只是增加了第3维。
 1 #include<iostream>
 2 #include<queue>
 3 #include<cstring>
 4 using namespace std;
 5 
 6 const int MAX=40;
 7 struct Node
 8 {
 9     char c;
10     int pace,z,x,y;//z,x,y--楼层,行,列
11 }maze[MAX][MAX][MAX];//存储迷宫字符,坐标,到达步数
12 int L,R,C;
13 int dre[6][3]={{1,0,0},{-1,0,0},{0,1,0},{0,-1,0},{0,0,1},{0,0,-1}};//方向:东南西北上下
14 bool check(int z,int x,int y)//检查坐标是否超出范围
15 {
16     return (z>=0&&z<=L&&x>=0&&x<=R&&y>=0&&y<=C);
17 }
18 int bfs(int z0,int x0,int y0)//找到从点[z0][x0][y0]到终点的最小步数
19 {
20     queue<Node> Q;
21     Node t;
22     Q.push(maze[z0][x0][y0]);//放入起点
23     while(!Q.empty())
24     {
25         t=Q.front();
26         Q.pop();
27         if(t.c=='E') return t.pace;//找到,返回步数
28         for(int i=0;i<6;i++)//枚举6个方向,若可以走且未走过就push到队尾
29         {
30             int Z=t.z+dre[i][0],X=t.x+dre[i][1],Y=t.y+dre[i][2];
31             if(check(Z,X,Y))
32             {
33                 if(maze[Z][X][Y].c!='#'&&!maze[Z][X][Y].pace)
34                 {
35                     maze[Z][X][Y].pace=t.pace+1;//步数等于上一步(t)的步数加1
36                     Q.push(maze[Z][X][Y]);
37                 }
38             }
39         }
40     }
41     return -1;//找不到返回-1
42 }
43 int main()
44 {
45     while(cin>>L>>R>>C,L||R||C)
46     {
47         int sz,sx,sy;
48         memset(maze,0,sizeof(maze));
49         for(int i=0; i<L; i++)
50             for(int j=0; j<R; j++)
51                 for(int k=0; k<C; k++)
52                 {
53                     cin>>maze[i][j][k].c;
54                     maze[i][j][k].z=i;
55                     maze[i][j][k].x=j;
56                     maze[i][j][k].y=k;
57                     if(maze[i][j][k].c=='S')//记录起点
58                     {
59                         sz=i;
60                         sx=j;
61                         sy=k;
62                     }
63                 }
64         int re=bfs(sz,sx,sy);
65         if(re!=-1) cout<<"Escaped in "<<re<<" minute(s).\n";
66         else cout<<"Trapped!\n";
67     }
68     return 0;
69 }

 

 posted on 2017-03-30 20:17  theFresh  阅读(160)  评论(0编辑  收藏  举报