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++

 #include <cstdio>
#include <cstring>
#include <queue>
char map[][][];
short data[][][];
struct Point{
int z, y, x;
}stP, edP;
short getData(Point a){ return data[a.z][a.y][a.x]; }
void setData(Point a, int d){ data[a.z][a.y][a.x] = d; }
char getMap(Point a){ return map[a.z][a.y][a.x]; }
void setMap(Point a, char d){ map[a.z][a.y][a.x] = d; }
void SPFA(){
Point nowP, nxtP;
std::queue<Point> q; q.push(stP);
int i;
short nxtData; setData(stP, );
while (!q.empty()){
nowP = q.front(); q.pop();
nxtData = getData(nowP) + ;
for (i = ; i < ; ++i){
memcpy(&nxtP, &nowP, sizeof nxtP);
switch (i){
case :nxtP.z += ; break;
case :nxtP.z -= ; break;
case :nxtP.y += ; break;
case :nxtP.y -= ; break;
case :nxtP.x += ; break;
default:nxtP.x -= ; break;
}
if (getMap(nxtP) != '.' || getData(nxtP) <= nxtData) continue;
setData(nxtP, nxtData); q.push(nxtP);
}
}
}
int main(){
int l, r, c, i, j, k, opt;
while (scanf("%d%d%d ", &l, &r, &c), l + r + c){
memset(data, 0x7F, sizeof data); memset(map, '#', sizeof map);
for (i = ; i <= l; ++i){
for (j = ; j <= r; ++j){
gets(&map[i][j][]);
for (k = ; k <= c; ++k){
if (map[i][j][k] == 'S') stP = { i, j, k };
else if (map[i][j][k] == 'E') edP = { i, j, k }, setMap(edP, '.');
}
}
getchar();
}
SPFA();
if ((opt = getData(edP)) == 0x7F7F) puts("Trapped!");
else printf("Escaped in %d minute(s).\n", opt);
}
return ;
}

上一篇:Python : 熟悉又陌生的字符编码(转自Python 开发者)


下一篇:二十二 Python分布式爬虫打造搜索引擎Scrapy精讲—scrapy模拟登陆和知乎倒立文字验证码识别