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. Array.length vs Array.prototype.length

    I found that both the Array Object and Array.prototype have the length property. I am confused on us ...

  2. 【经典dp】 poj 3671

    开一个dp[30010][3]的数组 其中dp[i][j]表示把第i个数改成j最少要花多少次 那么状态转移方程就列出来了: 令a=1 j!=a[i] 0 j==a[i] 那么dp[i][1]=dp[i ...

  3. OpenGL中glFrustum()和gluPerspective()的相互转换

    OpenGL中在窗口的大小发生变化的时候会触发resize()函数,这里会传入一个新的宽和高,在resize()函数中我们会设置投影矩阵,在可以使用OpenGL基础函数glFrustum()函数和gl ...

  4. JPA 系列教程7-双向多对多

    双向多对多的ddl语句 同单向多对多表的ddl语句一致 Student package com.jege.jpa.many2many; import java.util.HashSet; import ...

  5. Java获取来访者IP

    在JSP里,获取客户端的IP地址的方法是:request.getRemoteAddr(),这种方法在大部分情况下都是有效的.但是在通过了Apache,Squid等反向代理软件就不能获取到客户端的真实I ...

  6. JS定时器的使用--无缝滚动

    <title>无标题文档</title> <style> * {margin:0; padding:0;} #div1{width:1172px; height:2 ...

  7. UVALive 2035 The Monocycle(BFS状态处理+优先队列)

    这道题目真是非常坎坷啊,WA了很多次,但所有的思路都是奔着广搜去想的,一开始出现了比答案大的数据,才想到了应该是优先队列,再说加上也肯定不会错.一开始我读错了题意,以为旋转并且前行需要的时间跟其他一样 ...

  8. 学习笔记——原型模式Prototype

    原型模式,简单说就是具有一个克隆方法,外部可以直接使用此方法得到相应对象的拷贝对象. 比如哆啦A梦的复制镜,一照,就把物品拷贝了一份(虽然是镜子复制是相反的,这里就忽略这个细节了) C++中依靠拷贝构 ...

  9. android 数据存储分配的一些事

    应用程序在运行的过程中如果需要向手机上保存数据,一般是把数据保存在SDcard中的.大部分应用是直接在SDCard的根目录下创建一个文件夹,然后把数据保存在该文件夹中.这样当该应用被卸载后,这些数据还 ...

  10. Gym 100518E Embedding Caterpillars

    构造+DFS 很容易的可以构造两个,最后一个不会构造的话  DFS一下就可以了 #include<iostream> #include<string> #include< ...