Dungeon Master(三维bfs)
题目链接:http://poj.org/problem?id=2251
题目:
Description
Is an escape possible? If yes, how long will it take?
Input
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
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)的更多相关文章
- POJ 2251 Dungeon Master --- 三维BFS(用BFS求最短路)
POJ 2251 题目大意: 给出一三维空间的地牢,要求求出由字符'S'到字符'E'的最短路径,移动方向可以是上,下,左,右,前,后,六个方向,每移动一次就耗费一分钟,要求输出最快的走出时间.不同L层 ...
- POJ 2251 Dungeon Master (三维BFS)
题目链接:http://poj.org/problem?id=2251 Dungeon Master Time Limit: 1000MS Memory Limit: 65536K Total S ...
- POJ:Dungeon Master(三维bfs模板题)
Dungeon Master Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 16748 Accepted: 6522 D ...
- ZOJ 1940 Dungeon Master 三维BFS
Dungeon Master Time Limit:1000MS Memory Limit:65536KB 64bit IO Format:%I64d & %I64u Desc ...
- Dungeon Master(三维bfs)
You are trapped in a 3D dungeon and need to find the quickest way out! The dungeon is composed of un ...
- UVa532 Dungeon Master 三维迷宫
学习点: scanf可以自动过滤空行 搜索时要先判断是否越界(L R C),再判断其他条件是否满足 bfs搜索时可以在入口处(push时)判断是否达到目标,也可以在出口处(pop时) #i ...
- 【POJ - 2251】Dungeon Master (bfs+优先队列)
Dungeon Master Descriptions: You are trapped in a 3D dungeon and need to find the quickest way out! ...
- 棋盘问题(DFS)& Dungeon Master (BFS)
1棋盘问题 在一个给定形状的棋盘(形状可能是不规则的)上面摆放棋子,棋子没有区别.要求摆放时任意的两个棋子不能放在棋盘中的同一行或者同一列,请编程求解对于给定形状和大小的棋盘,摆放k个棋子的所有可行的 ...
- Dungeon Master (简单BFS)
Problem Description You are trapped in a 3D dungeon and need to find the quickest way out! The dunge ...
- POJ 2252 Dungeon Master 三维水bfs
题目: http://poj.org/problem?id=2251 #include <stdio.h> #include <string.h> #include <q ...
随机推荐
- 【MVC4升级到MVC5】ASP.Net MVC 4项目升级MVC 5的方法
1.备份你的项目 2.从Web API升级到Web API 2,修改global.asax,将 ? 1 WebApiConfig.Register(GlobalConfiguration.Config ...
- 【week6】psp
本周psp
- Jedis源码解析——Jedis和BinaryJedis
1.基本信息 先来看看他们的类定义: public class Jedis extends BinaryJedis implements JedisCommands, MultiKeyCommands ...
- 文件“bin\Debug\WindowsFormsApplication2.exe”正由另一进程使用,因此该进程无法访问该文件。
http://zhidao.baidu.com/question/221394579.html?qbl=relate_question_2&word=%BE%AF%B8%E6%094%09%C ...
- PHP中Session和Cookie的探究
一.Session (1)Session的由来以及介绍 Session:在计算机中,尤其是在网络应用中,称为“会话控制”,生存时间为用户在浏览某个网站时,从进入网站到关闭这个网站所经过的这段时间,也就 ...
- 【ADO.NET】ADO.NET知识点
ADO.NET 是一组向 .NET 程序员公开数据访问服务的类.提供了对各种关系数据.XML 和应用程序数据的访问. 所有的数据访问类位于System.Data.dll中.System.Data包含了 ...
- Shell中sort-cut-wc详解
sort sort 命令对 File 参数指定的文件中的行排序,并将结果写到标准输出.如果 File 参数指定多个文件,那么 sort 命令将这些文件连接起来,并当作一个文件进行排序. sort语法 ...
- Windows7上安装Git
我首先是百度到了这个网站:https://git-scm.com/download/win 这个网站上有下载链接,你可以根据你的系统选择不同的下载链接,我的是Win7 x64位的,下载地址为: htt ...
- [洛谷P4735]最大异或和
题目大意:有一串初始长度为$n$的序列$a$,有两种操作: $A\;x:$在序列末尾加一个数$x$ $Q\;l\;r\;x:$找一个位置$p$,满足$l\leqslant p\leqslant r$, ...
- POJ3335:Rotating Scoreboard——题解
http://poj.org/problem?id=3335 题目大意:给按照顺时针序的多边形顶点,问其是否有内核. —————————————————————————————— 看了两个小时的资料, ...