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 ;
}

POJ 2251 Dungeon Master(地牢大师)的更多相关文章

  1. POJ 2251 Dungeon Master --- 三维BFS(用BFS求最短路)

    POJ 2251 题目大意: 给出一三维空间的地牢,要求求出由字符'S'到字符'E'的最短路径,移动方向可以是上,下,左,右,前,后,六个方向,每移动一次就耗费一分钟,要求输出最快的走出时间.不同L层 ...

  2. POJ.2251 Dungeon Master (三维BFS)

    POJ.2251 Dungeon Master (三维BFS) 题意分析 你被困在一个3D地牢中且继续寻找最短路径逃生.地牢由立方体单位构成,立方体中不定会充满岩石.向上下前后左右移动一个单位需要一分 ...

  3. POJ 2251 Dungeon Master /UVA 532 Dungeon Master / ZOJ 1940 Dungeon Master(广度优先搜索)

    POJ 2251 Dungeon Master /UVA 532 Dungeon Master / ZOJ 1940 Dungeon Master(广度优先搜索) Description You ar ...

  4. BFS POJ 2251 Dungeon Master

    题目传送门 /* BFS:这题很有意思,像是地下城,图是立体的,可以从上张图到下一张图的对应位置,那么也就是三维搜索,多了z坐标轴 */ #include <cstdio> #includ ...

  5. poj 2251 Dungeon Master

    http://poj.org/problem?id=2251 Dungeon Master Time Limit: 1000MS   Memory Limit: 65536K Total Submis ...

  6. POJ 2251 Dungeon Master (三维BFS)

    题目链接:http://poj.org/problem?id=2251 Dungeon Master Time Limit: 1000MS   Memory Limit: 65536K Total S ...

  7. POJ 2251 Dungeon Master【三维BFS模板】

    Dungeon Master Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 45743 Accepted: 17256 Desc ...

  8. POJ 2251 Dungeon Master(3D迷宫 bfs)

    传送门 Dungeon Master Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 28416   Accepted: 11 ...

  9. POJ 2251 Dungeon Master (非三维bfs)

    Dungeon Master Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 55224   Accepted: 20493 ...

随机推荐

  1. C++设计模式-Singleton单例模式

    template <typename T> class Singleton { public: template <typename... Args> static T* In ...

  2. asp之servervariables全部显示

    <%Response.Write("<p>")%><%for each i in request.servervariables%> <% ...

  3. dsplay:table页面合并表格

    在实际项目中经常会遇到合并表格的情形,现在我将自己合并的方法写出来,供大家教参一下,顺便自己也作个备忘. <%@ page language="java" contentTy ...

  4. css 重新学习系列(1)

    来源: http://www.cnblogs.com/Zigzag/archive/2009/04/16/1394356.html CSS之Position详解(1) CSS的很多其他属性大多容易理解 ...

  5. linux 进程命令

    Linux中的ps命令是Process Status的缩写.ps命令用来列出系统中当前运行的那些进程.ps命令列出的是当前那些进程的快照,就是执行ps命令的那个时刻的那些进程,如果想要动态的显示进程信 ...

  6. tiny210 u-boot 网络ping不通主机解决方案

    站在巨人的肩膀上: http://blog.csdn.net/liukun321/article/details/7438880 http://www.arm9home.net/read.php?ti ...

  7. gcc编译错误表

    conversion from %s to %s not supported by iconv”iconv 不支持从 %s 到 %s 的转换” iconv_open”iconv_open” no ic ...

  8. acm课程练习2--1003

    题目描述 My birthday is coming up and traditionally I'm serving pie. Not just one pie, no, I have a numb ...

  9. 转:loadrunner ---循环输出关联数组

    web_reg_save_param,将Ord参数值设定为ALL,则关联函数将自动把符合条件的关联值保存到参数数组里.在本例中,假设关联值返回三条记录,则LR分别将值保存到sor_1,sor_2,so ...

  10. WebSphere MQ 入门指南【转】

    WebSphere MQ 入门指南 转自 WebSphere MQ 入门指南 - 大CC - 博客园http://www.cnblogs.com/me115/p/3456407.html 这是一篇入门 ...