码迷,mamicode.com
首页 > 其他好文 > 详细

暑假集训(1)第三弹 -----Dungeon Master(Poj2251)

时间:2016-07-16 21:33:48      阅读:218      评论:0      收藏:0      [点我收藏+]

标签:

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,但是要用三维数组储存地图,但是有6个分支,用数组来储存方向可以让程序更简洁。

技术分享
 1 #include "iostream"
 2 #include "queue"
 3 using namespace std;
 4 char maze[32][32][32];
 5 int v[6][3]={0,0,-1,0,0,1,0,-1,0,0,1,0,-1,0,0,1,0,0};
 6 int L,R,C;
 7 struct escaper
 8 {
 9     int i;
10     int j;
11     int k;
12     int time;
13 };
14 escaper fir;
15 void mbegin()
16 {
17    for (int i=0;i<=L+1;i++)
18     for (int j=0;j<=R+1;j++)
19      for (int k=0;k<=C+1;k++)
20         if (i*j*k == 0 || i == L+1 || j==R+1 || k==C+1)
21                   maze[i][j][k] = #;
22        else
23        {
24             cin>>maze[i][j][k];
25          if (maze[i][j][k] == S)
26               {
27                   fir.i = i;
28                   fir.j = j;
29                   fir.k = k;
30               }
31         }
32 }
33 void bfs()
34 {
35    queue <escaper> p;
36    escaper sec;
37    fir.time=0;
38    p.push(fir);
39      while (!p.empty())
40    {
41       sec = p.front();
42       p.pop();
43      for (int i=0;i<6;i++)
44       {
45          fir.i = sec.i+v[i][0];
46          fir.j = sec.j+v[i][1];
47          fir.k = sec.k+v[i][2];
48         if (maze[fir.i][fir.j][fir.k] != #)
49           {
50               fir.time = sec.time+1;
51               if (maze[fir.i][fir.j][fir.k] == E)
52                    {
53                        cout<<"Escaped in "<<fir.time<<" minute(s)."<<endl;
54                        return;
55                    }
56               maze[fir.i][fir.j][fir.k] = #;
57               p.push(fir);
58           }
59       }
60    }
61     cout<<"Trapped!"<<endl;
62 }
63 int main()
64 {
65     while (cin>>L>>R>>C && L && R && C)
66     {
67          mbegin();
68           bfs();
69     }
70    return 0;
71 }
View Code

 


暑假集训(1)第三弹 -----Dungeon Master(Poj2251)

标签:

原文地址:http://www.cnblogs.com/huas-zlw/p/5676774.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!