POJ 2251 Dungeon Master (BFS最短路)
三维空间里BFS最短路
#include <iostream>
#include <cstdio>
#include <cstring>
#include <sstream>
#include <string>
#include <algorithm>
#include <list>
#include <map>
#include <vector>
#include <queue>
#include <stack>
#include <cmath>
#include <cstdlib>
using namespace std;
char mapp[][][];
bool vis[][][];
struct Node {
int f,r,c,step;
} s,e;
int l,r,c;
int dir[][]= {{,,},{,,-},{,,},{,-,},{,,},{-,,}}; bool ok(int x,int y,int z) {
if(x< || y< || z< || x>=l || y>=r || z>=c)
return ;
else if(mapp[x][y][z] == '#')
return ;
else if(vis[x][y][z])
return ;
return ;
}
int dfs() {
Node q,t;
queue<Node>Q;
q.f=s.f,q.r=s.r,q.c=s.c;
q.step=;
vis[s.f][s.r][s.c]=;
Q.push(q);
while(!Q.empty()) {
t=Q.front();
Q.pop();
Node a;
if(t.f==e.f&&t.r==e.r&&t.c==e.c) {
return t.step;
}
for(int i=; i<; i++) {
a.f=t.f+dir[i][];
a.r=t.r+dir[i][];
a.c=t.c+dir[i][];
if(!ok(a.f,a.r,a.c))
continue;
vis[a.f][a.r][a.c]=;
a.step=t.step+;
Q.push(a);
}
}
return ;
}
int main() {
while(scanf("%d%d%d",&l,&r,&c),l||r||c) {
memset(vis,,sizeof(vis));
for(int k=; k<l; k++) {
for(int i=; i<r; i++) {
scanf("%s",mapp[k][i]);//遇到空格自动结束
for(int j=; j<c; j++) {
//scanf("%c",mapp[k][i][j]);
if(mapp[k][i][j]=='S') {
s.f=k;
s.r=i;
s.c=j;
}
if(mapp[k][i][j]=='E') {
e.f=k;
e.r=i;
e.c=j;
}
}
}
}
int ans;
ans=dfs();
if(ans)
printf("Escaped in %d minute(s).\n",ans);
else
printf("Trapped!\n");
}
return ;
}
POJ 2251 Dungeon Master (BFS最短路)的更多相关文章
- poj 2251 Dungeon Master( bfs )
题目:http://poj.org/problem?id=2251 简单三维 bfs不解释, 1A, 上代码 #include <iostream> #include<cst ...
- POJ 2251 Dungeon Master bfs 难度:0
http://poj.org/problem?id=2251 bfs,把两维换成三维,但是30*30*30=9e3的空间时间复杂度仍然足以承受 #include <cstdio> #inc ...
- poj 2251 Dungeon Master (BFS 三维)
You are trapped in a 3D dungeon and need to find the quickest way out! The dungeon is composed of un ...
- POJ 2251 Dungeon Master --- 三维BFS(用BFS求最短路)
POJ 2251 题目大意: 给出一三维空间的地牢,要求求出由字符'S'到字符'E'的最短路径,移动方向可以是上,下,左,右,前,后,六个方向,每移动一次就耗费一分钟,要求输出最快的走出时间.不同L层 ...
- POJ.2251 Dungeon Master (三维BFS)
POJ.2251 Dungeon Master (三维BFS) 题意分析 你被困在一个3D地牢中且继续寻找最短路径逃生.地牢由立方体单位构成,立方体中不定会充满岩石.向上下前后左右移动一个单位需要一分 ...
- BFS POJ 2251 Dungeon Master
题目传送门 /* BFS:这题很有意思,像是地下城,图是立体的,可以从上张图到下一张图的对应位置,那么也就是三维搜索,多了z坐标轴 */ #include <cstdio> #includ ...
- POJ 2251 Dungeon Master(地牢大师)
p.MsoNormal { margin-bottom: 10.0000pt; font-family: Tahoma; font-size: 11.0000pt } h1 { margin-top: ...
- 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 ...
- POJ 2251 Dungeon Master (三维BFS)
题目链接:http://poj.org/problem?id=2251 Dungeon Master Time Limit: 1000MS Memory Limit: 65536K Total S ...
随机推荐
- C++ 输入输出文件流(ifstream&ofstream)
ofstream是从内存到硬盘,ifstream是从硬盘到内存,其实所谓的流缓冲就是内存空间; 在C++中,有一个stream这个类,所有的I/O都以这个“流”类为基础的,包括我们要认识的文件I/O, ...
- ASP.NET MVC Spring.NET NHibernate 整合
请注明转载地址:http://www.cnblogs.com/arhat 在整合这三个技术之前,首先得说明一下整合的步骤,俗话说汗要一口一口吃,事要一件一件做.同理这个三个技术也是.那么在整合之前,需 ...
- IOS 获得通讯录中联系人的所有属性 备用参考
ABAddressBookRef addressBook = ABAddressBookCreate(); CFArrayRef results = ABAddressBookCopyArrayOfA ...
- SWFUpload 按钮显示问题
问题: 今天遇到一个这样的问题,我用Vs2010打开用SWFUpload上传文件的网页时,按钮显示不出来,试了好多方法,终于被我找到了! 解决方法: 原来是vs2010自带的Asp.net Devel ...
- php调用whois接口域名查询
由两部分组成,一个index.php文件,一个whois的接口文件: <html> <head> <title>域名到期查询</title> <s ...
- BZOJ 3993 [SDOI 2015] 星际战争 解题报告
首先我们可以二分答案. 假设当前二分出来的答案是 $Ans$ ,那么我们考虑用网络流检验: 设武器为 $X$,第 $i$ 个武器的攻击力为 $B_i$: 设机器人为 $Y$,第 $i$ 个机器人的装甲 ...
- MongoDB服务器启动异常 1067
删掉mongod.lock文件就重启就可以了.
- 输出排名第k的法雷级数的值;
#include<cstdio> #include<cstring> using namespace std; int k,n; void dfs(int a,int b,in ...
- C语言预处理运算符
转自C语言预处理运算符 预处理还需要运算符?有没有搞错? ^_^, 没有搞错,预处理是有运算符,而且还不止一个: #(单井号) -- 字符串化运算符. ##(双井号 )-- 连接运算符 #@ ...
- size_t为何这么重要?
原文Why size_t matters 合理的使用size_t可以提高程序的可移植性和代码的可读性,让你的程序更高效. Numerous functions in the Standard C li ...