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. wlcore: firmware chunk too long

    insmod wlcore_sdio.ko 的时候出现的错误 43.767890] Powering on wl12xx [  143.846003] Powering off wl12xx [  1 ...

  2. Hibernate主键生成方式之hilo

    当利用Hibernate的getHibernateTemplate().save(obj);插入的对象的主键ID为null的时候自动生成5位数的主键ID进行插入. 此笔记的由来: 老夫在此处上传材料后 ...

  3. Css 之 px em %

    在页面整体布局中,页面元素的尺寸大小(长度.宽度.内外边距等)和页面字体的大小也是重要的工作之一.一个合理设置,则会让页面看起来层次分明,重点鲜明,赏心悦目.反之,一个不友好的页面尺寸和字体大小设置, ...

  4. Android OpenGL ES(十四)gl10方法解析

    Android 支持 OpenGL 列表 1.GL 2.GL 10 3.GL 10 EXT 4.GL 11 5.GL 11 EXT 6.GL 11 ExtensionPack 我们将使用 GL10 这 ...

  5. Android中购物车的全选、反选、问题和计算价格

    此Demo主要解决的是购物车中的全选,反选计算价格和选中的条目个数的问题,当选中几条时,点击反选,会把当先选中的变为不选中,把不选中的变为选中.点击全选会全部选中,再次点击时,变为全部不选中. //- ...

  6. js表单提交,判断文本框,用户名密码是否为空,JS表单检测!

    当表单提交时先触发验证的js代码,当验证表单的方法返回true时才会提交表单返回false则不提交数据<script type="text/javascript">fu ...

  7. PAT (Advanced Level) 1096. Consecutive Factors (20)

    如果是素数直接输出1与素数,否则枚举长度和起始数即可. #include<cstdio> #include<cstring> #include<cmath> #in ...

  8. Hibernate中,将session绑定到线程时,在保存和查询数据的代码里,要正确的关闭session

    比如有个保存的方法 // 保存 public void save(){ Transaction t = XXX Session s = getSession.beginTransaction(); X ...

  9. JSP标准标签库(JSTL)--国际化标签库 fmt

    JSTL中使用fmt.tld作为格式化标签库的定义文件 No. 功能分类 标签名称 描述 1 国际化标签 <fmt:setLocale> 设置一个全局的地区代码 2 <fmt:req ...

  10. 转载 C++学习第9篇---类和类的封装

    http://blog.csdn.net/zuheyawen/article/details/7324340