题目链接:http://poj.org/problem?id=2251

题目:

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?

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.

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!

Sample Input

3 4 5
S....
.###.
.##..
###.# #####
#####
##.##
##... #####
#####
#.###
####E 1 3 3
S##
#E#
### 0 0 0

Sample Output

Escaped in 11 minute(s).
Trapped!

题意:你处在一个三维的地牢里,从S出发逃到出口E,问最少要跑多远。 思路:这题虽然是一个三维的地图,但是做法和二维的没多大区别,不过要从当前层到其他层的要求是你所在位置为非#,且你将到的那层的这个位置也是非#。 代码实现如下:
 #include <queue>
#include <cstdio>
#include <cstring>
using namespace std; const int inf = 0x3f3f3f3f;
int l, r, c, ans;
int sx, sy, sz;
char mp[][][];
int vis[][][]; struct node {
int x, y, z, step;
}nw, nxt; int dx[] = {, -, , , , }, dy[] = {, , , -, , },
dz[] = {, , , , , -}; void bfs(int z, int x, int y) {
vis[z][x][y] = ;
nw.z = z, nw.x = x, nw.y = y, nw.step = ;
queue<node> q;
q.push(nw);
while(!q.empty()) {
nw = q.front(), q.pop();
if(mp[nw.z][nw.x][nw.y] == 'E') {
ans = nw.step;
return;
}
for(int i = ; i < ; i++) {
nxt.z = nw.z + dz[i];
nxt.x = nw.x + dx[i];
nxt.y = nw.y + dy[i];
if(nxt.z >= && nxt.z < l && nxt.x >= && nxt.x < r && nxt.y >= && nxt.y < c && vis[nxt.z][nxt.x][nxt.y] == && mp[nxt.z][nxt.x][nxt.y] != '#') {
nxt.step = nw.step + ;
vis[nxt.z][nxt.x][nxt.y] = ;
q.push(nxt);
}
}
}
} int main() {
while(~scanf("%d%d%d", &l, &r, &c) && (l + r + c)) {
for(int i = ; i < l; i++) {
for(int j = ; j < r; j++) {
scanf("%s", mp[i][j]);
for(int k = ; k < c; k++) {
if(mp[i][j][k] == 'S') {
sx = j, sy = k, sz =i;
}
}
}
}
memset(vis, , sizeof(vis));
ans = inf;
bfs(sz, sx, sy);
if(ans >= inf) {
printf("Trapped!\n");
} else {
printf("Escaped in %d minute(s).\n", ans);
}
}
return ;
}
												

Dungeon Master(三维bfs)的更多相关文章

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

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

  2. POJ 2251 Dungeon Master (三维BFS)

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

  3. POJ:Dungeon Master(三维bfs模板题)

    Dungeon Master Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 16748   Accepted: 6522 D ...

  4. ZOJ 1940 Dungeon Master 三维BFS

    Dungeon Master Time Limit:1000MS     Memory Limit:65536KB     64bit IO Format:%I64d & %I64u Desc ...

  5. Dungeon Master(三维bfs)

    You are trapped in a 3D dungeon and need to find the quickest way out! The dungeon is composed of un ...

  6. UVa532 Dungeon Master 三维迷宫

        学习点: scanf可以自动过滤空行 搜索时要先判断是否越界(L R C),再判断其他条件是否满足 bfs搜索时可以在入口处(push时)判断是否达到目标,也可以在出口处(pop时)   #i ...

  7. 【POJ - 2251】Dungeon Master (bfs+优先队列)

    Dungeon Master  Descriptions: You are trapped in a 3D dungeon and need to find the quickest way out! ...

  8. 棋盘问题(DFS)& Dungeon Master (BFS)

    1棋盘问题 在一个给定形状的棋盘(形状可能是不规则的)上面摆放棋子,棋子没有区别.要求摆放时任意的两个棋子不能放在棋盘中的同一行或者同一列,请编程求解对于给定形状和大小的棋盘,摆放k个棋子的所有可行的 ...

  9. Dungeon Master (简单BFS)

    Problem Description You are trapped in a 3D dungeon and need to find the quickest way out! The dunge ...

  10. POJ 2252 Dungeon Master 三维水bfs

    题目: http://poj.org/problem?id=2251 #include <stdio.h> #include <string.h> #include <q ...

随机推荐

  1. JS DOM(2017.12.28)

    一.获得元素节点的方法 document.getElementById()    根据Id获取元素节点 document.getElementsByName()    根据name获取元素节点   遍 ...

  2. Cacti自定义脚本,监测Docker信息(Script/Command方式)

    一 环境背景 监控主机A:192.168.24.231:被监控主机B:192.168.24.233 A/B主机,通过公私钥建立ssh连接 [操作B主机时不需要输入密码,详见笔记:http://app. ...

  3. 获得system32等系统文件权限

    SYSTEM是至高无上的超级管理员帐户.默认情况下,我们无法直接在登录对话框上以SYSTEM帐户的身份登录到Windows桌面环境.实际上SYSTEM帐户早就已经“盘踞”在系统中了.根据http:// ...

  4. bzoj4760[USACO2017 Jan]Hoof,Paper,Scissors

    题意:玩n次剪刀石头布,对方每次出什么已经知道了.你出的招数必须是连续的几段(不能超过k+1段),问你最多赢几次.(n<=100000,k<=20) 正常做法:f[i][j][k]表示前i ...

  5. 【bzoj1609】[Usaco2008 Feb]Eating Together麻烦的聚餐 dp

    题目描述 为了避免餐厅过分拥挤,FJ要求奶牛们分3批就餐.每天晚饭前,奶牛们都会在餐厅前排队入内,按FJ的设想所有第3批就餐的奶牛排在队尾,队伍的前端由设定为第1批就餐的奶牛占据,中间的位置就归第2批 ...

  6. Python爬取B站视频信息

    该文内容已失效,现已实现scrapy+scrapy-splash来爬取该网站视频及用户信息,由于B站的反爬封IP,以及网上的免费代理IP绝大部分失效,无法实现一个可靠的IP代理池,免费代理网站又是各种 ...

  7. 【国家集训队】聪聪可可 ——树形DP

    感觉是一道很妙的树形DP题,充分利用到了树的性质(虽然说点分治也可以做,,,,但是本蒟蒻不会啊) 然而某Twilight_Sx大佬表示这道题真的非常水,,,本蒟蒻也只能瑟瑟发抖了 本蒟蒻表示还是要经过 ...

  8. BZOJ3195:[JXOI2012]奇怪的道路——题解

    http://www.lydsy.com/JudgeOnline/problem.php?id=3195 Description 小宇从历史书上了解到一个古老的文明.这个文明在各个方面高度发达,交通方 ...

  9. BZOJ2693:JZPTAP——题解

    http://www.lydsy.com/JudgeOnline/problem.php?id=2693 Description   Input 一个正整数T表示数据组数 接下来T行 每行两个正整数 ...

  10. HDU 2083(排序+绝对值+中间值求和)

    简易版之最短距离 点我跳转到HDOJ Time Limit: 1000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) ...