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. linux配置使用外部smtp发送邮件

    mail命令需要设定mail.rc(或nail.rc)文件, set from=user@domain.comset smtp=smtp.domain.comset smtp-auth-user=us ...

  2. C#设置默认打印机

    项目中,需要选择打印机,切换打印机.demo如下(wpf应用程序): Xaml: <Window x:Class="PrintersApp.MainWindow" xmlns ...

  3. js.map error

    1. 问题:      1.1 通过bower install 的components 许多在运行的时候报404无法找到js.map文件, 如图:          2. 分析:     2.1 查看 ...

  4. protobuf使用NDK编译Android的静态库(工作记录)

    1.protobuf 编译过程 前提: 确保自己电脑上已经安装了cygwin + ndk, 并且NDK能够编译hello-jni成功 1.1 把protobuf 压缩包解压到protobuf文件夹下 ...

  5. JS动态引入js、CSS动态创建script/link/style标签

    一.动态创建link方式 我们可以使用link的方式.如下代码所示. function addCssByLink(url){ var doc=document; var link=doc.create ...

  6. iOS 判断奇偶数

    if (_bigUrlArray.count%2==0) {//如果是偶数 a = i*(_bigUrlArray.count/count);//每个线程图片初始数 b = (i+1)*(_bigUr ...

  7. linux的学习系列 3---目录

    目录也是一个文件,它的唯一功能是用来保存文件及其相关信息.所有的文件,包括普通文件.设备文件和目录文件,都会被保存到目录中. 主目录 登录后,你所在的位置就是你的主目录(或登录目录),接下来你主要是在 ...

  8. encodeURIComponent与URLDecoder.decode用法

    在输入地址栏时有时一些信息需要在地址栏看不见,我们就需要对其信息在前台转码后台解码 js:encodeURIComponent编码与解码 今天在js往jsp和servlet传参的时候出现:JavaSc ...

  9. oracle10g遇到ORA-16038日志无法归档问题

    SQL> shutdown immediate ORA-01109: 数据库未打开 已经卸载数据库. ORACLE 例程已经关闭. SQL> startup ORACLE 例程已经启动. ...

  10. jQuery 数据滚动(上下)

    setInterval(function() { jq('.sjbg02 li:first').animate({ 'height': '0', 'opacity': '0' }, 'slow', f ...