POJ 2251 Dungeon Master(地牢大师)

Posted Simon_X

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了POJ 2251 Dungeon Master(地牢大师)相关的知识,希望对你有一定的参考价值。

POJ 2251 Dungeon Master(地牢大师)

Time Limit: 1000MS    Memory Limit: 65536K

 

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? 

你被困在一个3D地牢中且继续寻找最短路径逃生!地牢由立方体单位构成,立方体中不定会充满岩石。向上下前后左右移动一个单位需要一分钟。你不能对角线移动并且迷宫四周坚石环绕。

是否存在逃出生天的可能性?如果存在,则需要多少时间?
CN

 

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,R和C(皆不超过30)。
L表示地牢的层数。
R和C分别表示每层地牢的行与列的大小。

随后L层地牢,每层R行,每行C个字符。
每个字符表示地牢的一个单元。\'#\'表示岩石单元,\'.\'表示空白单元。你的起始位置在\'S\',出口为\'E\'。
每层地牢后都有一个空行。L,R和C均为0时输入结束。
CN

 

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!
每个迷宫对应一行输出。
如果可以逃生,则输出如下
Escaped in x minute(s).
x为最短脱离时间。

如果无法逃生,则输出如下
Trapped!
CN

 

Sample Input - 输入样例

3 4 5
S....
.###.
.##..
###.#

#####
#####
##.##
##...

#####
#####
#.###
####E

1 3 3
S##
#E#
###

0 0 0

 

Sample Output - 输出样例

Escaped in 11 minute(s).
Trapped!

 

题解

  三维迷宫,直接SPFA,注意条件设置即可。

 

代码 C++

 1 #include <cstdio>
 2 #include <cstring>
 3 #include <queue>
 4 char map[35][35][35];
 5 short data[35][35][35];
 6 struct Point{
 7     int z, y, x;
 8 }stP, edP;
 9 short getData(Point a){ return data[a.z][a.y][a.x]; }
10 void setData(Point a, int d){ data[a.z][a.y][a.x] = d; }
11 char getMap(Point a){ return map[a.z][a.y][a.x]; }
12 void setMap(Point a, char d){ map[a.z][a.y][a.x] = d; }
13 void SPFA(){
14     Point nowP, nxtP;
15     std::queue<Point> q; q.push(stP);
16     int i;
17     short nxtData; setData(stP, 0);
18     while (!q.empty()){
19         nowP = q.front(); q.pop();
20         nxtData = getData(nowP) + 1;
21         for (i = 0; i < 6; ++i){
22             memcpy(&nxtP, &nowP, sizeof nxtP);
23             switch (i){
24             case 0:nxtP.z += 1; break;
25             case 1:nxtP.z -= 1; break;
26             case 2:nxtP.y += 1; break;
27             case 3:nxtP.y -= 1; break;
28             case 4:nxtP.x += 1; break;
29             default:nxtP.x -= 1; break;
30             }
31             if (getMap(nxtP) != \'.\' || getData(nxtP) <= nxtData) continue;
32             setData(nxtP, nxtData); q.push(nxtP);
33         }
34     }
35 }
36 int main(){
37     int l, r, c, i, j, k, opt;
38     while (scanf("%d%d%d ", &l, &r, &c), l + r + c){
39         memset(data, 0x7F, sizeof data); memset(map, \'#\', sizeof map);
40         for (i = 1; i <= l; ++i){
41             for (j = 1; j <= r; ++j){
42                 gets(&map[i][j][1]);
43                 for (k = 1; k <= c; ++k){
44                     if (map[i][j][k] == \'S\') stP = { i, j, k };
45                     else if (map[i][j][k] == \'E\') edP = { i, j, k }, setMap(edP, \'.\');
46                 }
47             }
48             getchar();
49         }
50         SPFA();
51         if ((opt = getData(edP)) == 0x7F7F) puts("Trapped!");
52         else printf("Escaped in %d minute(s).\\n", opt);
53     }
54     return 0;
55 }

 

以上是关于POJ 2251 Dungeon Master(地牢大师)的主要内容,如果未能解决你的问题,请参考以下文章

POJ 2251 Dungeon Master (BFS)

POJ-2251 Dungeon Master

POJ2251 Dungeon Master

POJ 2251 Dungeon Master(BFS)

搜索POJ2251:Dungeon Master

[POJ 2251] Dungeon Master