POJ2251 Dungeon Master 解题报告
分类:BFS,迷宫,STL,队列
作者:ACShiryu
时间:2011-7-23
Dungeon Master
Time Limit: 1000MS | Memory Limit: 65536K | |
Total Submissions: 9395 | Accepted: 3652 |
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?
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.
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
where x is replaced by the shortest time it takes to escape.
If it is not possible to escape, print the line
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'表示起点,'E'表示终点,求从起点到终点的最小移动次数,解法和二维的类似,只是在行动时除了东南西北移动外还多了上下。
对于题目给出数据的含义就是输入l,r,c,分别代表迷宫有l层,每层长宽分别是c,r。
对于数据以可以这样移动
(1,1,1)->(1,1,2)->(1,1,3)->(1,1,4)->(1,1,5)->(1,2,5)
->(1,3,5)->(1,3,4)->(1,4,4)->(2,4,4)->(2,4,5)->(3,4,,5)
共11步就可以到达终点
对于数据二明显不能到达,则输出Trapped
这题用BFS解,每次去队首元素,如果是终点则输出结果移动的次数,否则,从该点开始分别向东南西北上下移动(如果可以走的话)并继续搜,如果到队列为空还没搜到解法,则说明无解。
这题我提交一次就AC了,可以看出难度不是很大。
参考代码:
1 #include<iostream>
2 #include<cstdlib>
3 #include<cstdio>
4 #include<cstring>
5 #include<algorithm>
6 #include<cmath>
7 #include<queue>
8 using namespace std;
9 bool hash[35][35][35];
10 char map[35][35][35];
11 struct prog{
12 int x;int y;int z; //定义点的坐标
13 int step; //移动的步子数
14 };
15 int main()
16 {
17 int l , r , c ;
18 while ( cin >> l >> r >> c , l && r && c )
19 {
20 int i , j , k;
21 prog start;
22 memset(hash,false,sizeof(hash));//初始化为false,表示每条路都没走过
23 for ( i = 0 ; i < l ; i ++ )
24 {
25 for ( j = 0 ; j < r ; j ++ )
26 {
27 cin >> map [i][j];
28 for ( k = 0 ; k < c ; k ++ )
29 {
30 if(map[i][j][k]=='S')
31 {//对起点的相关变量初始化
32 start.x=i;
33 start.y=j;
34 start.z=k;
35 start.step=0;
36 hash[i][j][k]=true;
37 }
38 }
39 }
40 }
41
42 queue<prog>bfs;
43 bfs.push(start); //起点入队
44 bool found=false; //标记变量,判断是否找到最优解,如果为true则说明从起点到终点存在着解
45
46 while(!bfs.empty())
47 {
48 prog tmp=bfs.front();
49 bfs.pop();
50
51 if(map[tmp.x][tmp.y][tmp.z]=='E')
52 {//已经到了终点,则将标记变量设为true,并输出答案
53 found=true;
54 cout<<"Escaped in "<<tmp.step<<" minute(s)."<<endl;
55 break;
56 }
57
58 tmp.step++; //移动步子+1
59 prog tmp2;
60 if(tmp.x>0)
61 {//向下走,所以改点不能在第一层,则x要大于0
62 tmp2=tmp;
63 tmp2.x--;
64 if(map[tmp2.x][tmp2.y][tmp2.z]!='#'&&hash[tmp2.x][tmp2.y][tmp2.z]==false)
65 {//如果下一层不是障碍物并且也没走过,则往下走,并标记该点已走过,入队
66 hash[tmp2.x][tmp2.y][tmp2.z]=true;
67 bfs.push(tmp2);
68 }
69 }
70
71 //下面搜索方法同上 注释略
72 if(tmp.x<l-1)
73 {//向上
74 tmp2=tmp;
75 tmp2.x++;
76 if(map[tmp2.x][tmp2.y][tmp2.z]!='#'&&hash[tmp2.x][tmp2.y][tmp2.z]==false)
77 {
78 hash[tmp2.x][tmp2.y][tmp2.z]=true;
79 bfs.push(tmp2);
80 }
81 }
82
83 if(tmp.y>0)
84 {//向北
85 tmp2=tmp;
86 tmp2.y--;
87 if(map[tmp2.x][tmp2.y][tmp2.z]!='#'&&hash[tmp2.x][tmp2.y][tmp2.z]==false)
88 {
89 hash[tmp2.x][tmp2.y][tmp2.z]=true;
90 bfs.push(tmp2);
91 }
92 }
93
94 if(tmp.y<r-1)
95 {//向南
96 tmp2=tmp;
97 tmp2.y++;
98 if(map[tmp2.x][tmp2.y][tmp2.z]!='#'&&hash[tmp2.x][tmp2.y][tmp2.z]==false)
99 {
100 hash[tmp2.x][tmp2.y][tmp2.z]=true;
101 bfs.push(tmp2);
102 }
103 }
104
105 if(tmp.z>0)
106 {//向西
107 tmp2=tmp;
108 tmp2.z--;
109 if(map[tmp2.x][tmp2.y][tmp2.z]!='#'&&hash[tmp2.x][tmp2.y][tmp2.z]==false)
110 {
111 hash[tmp2.x][tmp2.y][tmp2.z]=true;
112 bfs.push(tmp2);
113 }
114 }
115
116 if(tmp.z<c-1)
117 {//向东
118 tmp2=tmp;
119 tmp2.z++;
120 if(map[tmp2.x][tmp2.y][tmp2.z]!='#'&&hash[tmp2.x][tmp2.y][tmp2.z]==false)
121 {
122 hash[tmp2.x][tmp2.y][tmp2.z]=true;
123 bfs.push(tmp2);
124 }
125 }
126
127 }
128 if(!found)//如果没有找到解
129 cout<<"Trapped!"<<endl;
130 }
131 return 0;
132 }
作者:ACShiryu
出处:http://www.cnblogs.com/ACShiryu/
若非注明,本博客文章均为原创,版权归作者和博客园共有,欢迎转载,但必须保留此段声明,且在文章页面明显位置给出原文链接,否则保留追究法律责任的权利。
该文章也同步发布在我的新浪微博中-ACShiryu's weibo,欢迎收听。