B - Dungeon Master( 巨龙与迷宫)

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!


题目意思:
有一头龙在S处,龙可以上下左右前后移动,#表示无法通过,' . '表示可以通过;

解法:

 这是一道使用BFS来求最短路径的题目,按照BFS的思路写就可以了

 1 #include <iostream>
 2 #include <cstdio>
 3 #include <map>
 4 #include <string>
 5 #include <cstring>
 6 #include <queue>
 7 using namespace std;
 8 #define LL long long
 9 
10 char map1 [50][50][50];
11 int visit[50][50][50];
12 int x,y,z;
13 int x0,y0,z0;
14 int dir[6][3] = {{0,0,1},{0,0,-1},{0,1,0},{0,-1,0},{1,0,0},{-1,0,0}};
15 
16 struct node
17 {
18     int x,y,z;
19     int cont;
20 };
21 
22 
23 
24 void bfs()
25 {
26     queue<node>Q;
27     node t;
28     t.x = x0;t.y = y0;t.z = z0;
29     t.cont = 0;
30     visit[x0][y0][z0] = 1;
31     Q.push(t);
32     while(!Q.empty())
33     {
34         node temp ;
35         temp = Q.front();
36         Q.pop();
37 
38         int xx = temp.x;
39         int yy = temp.y;
40         int zz = temp.z;
41         int cont = temp.cont;
42  //       cout<<"xx yy zz "<<xx<<' '<<yy<<' '<<zz<<' '<<endl;
43 //        cout<<" map1 = "<<map1[xx][yy][zz]<<endl;
44         if(map1[xx][yy][zz] == 'E')
45         {
46             cout<<"Escaped in "<<cont<<" minute(s)."<<endl;
47             return ;
48         }
49 
50 
51         for(int i = 0;i < 6;i++)
52         {
53             node T;
54             T.x = xx + dir[i][0];
55             T.y = yy + dir[i][1];
56             T.z = zz + dir[i][2];
57             T.cont = cont+1;
58             if(T.x < 1||T.x>x||T.y <1||T.y>y||T.z<1||T.z>z) continue;
59             if (map1[T.x][T.y][T.z] != '#'&&visit[T.x][T.y][T.z] != 1 )
60             {
61  //               cout<<"xx yy zz "<<T.x<<' '<<T.y<<' '<<T.z<<' '<<endl;
62                 visit[T.x][T.y][T.z] = 1;
63                 Q.push(T);
64             }
65 
66         }
67     }
68     cout<<"Trapped!"<<endl;
69 
70 
71 
72 }
73 
74 int main()
75 {
76 
77     while(cin>>x>>y>>z)
78     {
79         if(x==0&&y==0&&z==0)
80             break;
81         for(int i = 1;i <= x;i++)
82             for(int j = 1;j <= y;j++)
83                 for(int k = 1;k <=  z;k++)
84                 {
85                     cin>>map1[i][j][k];
86                     if(map1[i][j][k] == 'S')
87                         {
88                             x0 = i; y0 = j;z0 = k;
89                         }
90                 }
91  //       cout<<x0<<y0<<z0<<endl;
92         memset(visit,0,sizeof(visit));
93         bfs();
94 
95     }
96 
97 
98     return 0;
99 }

 

posted @ 2017-07-24 11:02  山杉三  阅读(327)  评论(0编辑  收藏  举报