POJ 2251 Dungeon Master (BFS)

题目链接:POJ 2251

Describe:
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!

题目大意:

三维空间的牢笼,问你能不能走出去。不出意外应该是搜索吧,第一次用的是dfs,轻轻松松的TLE了,然后冥思苦想加上了记忆化,不出意外又是超时。实在想不到咋优化了,上网搜,发现都用的是bfs,因为dfs要回溯,一条路走到黑,而bfs只搜一遍,然后就写了bfs,成功的MLE了(这题跟我过不去),到现在都不知道为什么MLE,好像是因为队列push时候直接加了大括号?最后修改一下含泪AC。

解题思路:

解题思路用心了。

AC代码:

 

 1 #include <cstdio>
 2 #include <iostream>
 3 #include <queue>
 4 using namespace std;
 5 struct node // 要插入队列的结构体
 6 {
 7     int a,b,c,st;
 8 };
 9 int l,r,c,x,y,z,f;
10 char g[30][30][30],s; // g用来存地牢
11 int  p[30][30][30]; // 判断是否走过,这个好像可以去掉
12 int d[6][3] = {{-1,0,0},{1,0,0},{0,-1,0},{0,1,0},{0,0,-1},{0,0,1}}; // 新学的
13 void init(int l,int r,int c) // 初始化
14 {
15     for(int i = 0; i < l; i++)
16         for(int j = 0; j < r; j++)
17             for(int k = 0; k < c; k++)
18                 p[i][j][k] = 0;
19 }
20 int main()
21 {
22     while(~scanf("%d%d%d",&l,&r,&c) && l+r+c)
23     {
24         init(l,r,c);
25         f = 0; // 是否找到的标志
26         queue<node> q; // 这个用优先队列可能会更好一点
27         for(int i = 0; i < l; i++)
28         {
29             for(int j = 0; j < r; j++)
30             {
31                 for(int k = 0; k < c; k++)
32                 {
33                     s = getchar(); // 蒟蒻的读入
34                     while(s!='#' && s!='.' && s!='S' && s!='E') s = getchar();
35                     if(s == 'S') {z = i; x = j; y = k;} // 保存起点
36                     g[i][j][k] = s;
37                 }
38             }
39         }
40         node t;
41         t.a = x;
42         t.b = y;
43         t.c = z;
44         t.st = 0;
45         q.push(t); // 起点入队
46         p[z][x][y] = 1; // 标记起点
47         while(!q.empty())
48         {
49             node tmp = q.front();
50             q.pop();
51             x = tmp.a; y = tmp.b; z = tmp.c;
52             int step = tmp.st;
53             if(g[z][x][y] == 'E') // 判断是否到达终点
54             {
55                 f = 1;
56                 printf("Escaped in %d minute(s).\n",tmp.st);
57                 break;
58             }
59             for(int i = 0; i < 6; i++)
60             {
61                 int x1 = x+d[i][0];
62                 int y1 = y+d[i][1];
63                 int z1 = z+d[i][2];
64                 if(g[z1][x1][y1] != '#' && p[z1][x1][y1] == 0 && x1 >= 0 && x1 < r && y1 >= 0 && y1 < c && z1 >= 0 && z1 < l)
65                 {
66                     p[z1][x1][y1] = 1;
67                     node temp;
68                     temp.a = x1;
69                     temp.b = y1;
70                     temp.c = z1;
71                     temp.st = step+1;
72                     q.push(temp); // 一个一个的入队
73                 }
74             }
75         }
76         if(f == 0) printf("Trapped!\n");
77     }
78 }

 

小结:

这个题在想dfs优化的时候,我想到了dp,虽然现在不会dp,但是隐约感觉dp可行。

 

posted @ 2020-08-23 21:13  不敢说的梦  阅读(122)  评论(0)    收藏  举报