三维空间里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最短路)的更多相关文章

  1. poj 2251 Dungeon Master( bfs )

    题目:http://poj.org/problem?id=2251 简单三维 bfs不解释, 1A,     上代码 #include <iostream> #include<cst ...

  2. POJ 2251 Dungeon Master bfs 难度:0

    http://poj.org/problem?id=2251 bfs,把两维换成三维,但是30*30*30=9e3的空间时间复杂度仍然足以承受 #include <cstdio> #inc ...

  3. 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 ...

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

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

  5. POJ.2251 Dungeon Master (三维BFS)

    POJ.2251 Dungeon Master (三维BFS) 题意分析 你被困在一个3D地牢中且继续寻找最短路径逃生.地牢由立方体单位构成,立方体中不定会充满岩石.向上下前后左右移动一个单位需要一分 ...

  6. BFS POJ 2251 Dungeon Master

    题目传送门 /* BFS:这题很有意思,像是地下城,图是立体的,可以从上张图到下一张图的对应位置,那么也就是三维搜索,多了z坐标轴 */ #include <cstdio> #includ ...

  7. POJ 2251 Dungeon Master(地牢大师)

    p.MsoNormal { margin-bottom: 10.0000pt; font-family: Tahoma; font-size: 11.0000pt } h1 { margin-top: ...

  8. 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 ...

  9. POJ 2251 Dungeon Master (三维BFS)

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

随机推荐

  1. Intellij IDEA 14的注册码

    IntelliJ IDEA 14 注册码 IntelliJ IDEA 14 下载地址: IntelliJ IDEA 14 下载 分享几个license: (1) key:IDEA value:6115 ...

  2. Python复杂多重排序

    1. cmp函数是python自带的函数,用于比较两个参数哪个大哪个小 print cmp(2, 3) # -1 如果第一个参数比第二个小,就返回-1,两个元素相等,返回0,否则返回1 2.所以就可以 ...

  3. Popen No such file or directory 错误

    File , in reload_config stderr=PIPE, env={"PATH": '', "HOME": "/root"} ...

  4. ModelState用法

    ModelState.AddModelError:添加错误信息 ModelState是一个字典类型,这句话的作用是向ModelState中添加一条错误信息,第一个参数是Key,第二个参数是Value. ...

  5. SQL中not and or优先级问题

    SQL中 not  and or优先级问题 刚刚在项目中遇到这样一个问题,SQL语句如下: 我想要的结果的条件是:1. LIBRARY_ID=1 或者 LIB_ID=1 2.STATUS=3 但是结果 ...

  6. 安全测试常见的10个问题 ZT

    1, 问题:没有被验证的输入     测试方法:     数据类型(字符串,整型,实数,等)    允许的字符集    最小和最大的长度    是否允许空输入    参数是否是必须的    重复是否允 ...

  7. jquery and event

    jquery阻止事件冒泡 event.stopPropagation(); event.cancelBubble = true; jquery阻止默认操作 event.preventDefault() ...

  8. [jobdu]第一个只出现一次的字符

    刚开始还在想双指针,完全没必要.然后最土的可以扫两遍O(n),用一个数组记录出现次数.然后想到如果数组里记录出现的位置,可以只扫一遍O(n),第二遍扫字符集就行了. #include <iost ...

  9. 如何将Springside4项目转成Eclipse项目

    1)下载springside4 官网地址 http://www.springside.org.cn/download.html 2)运行CMD,进入 C:\Documents and Settings ...

  10. Date简介

    Date类 在JDK1.0中,Date类是唯一的一个代表时间的类,但是由于Date类不便于实现国际化,所以从JDK1.1版本开始,推荐使用Calendar类进行时间和日期处理.这里简单介绍一下Date ...