1. 程式人生 > 實用技巧 >POJ 2251 Dungeon Master (BFS)

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可行。