三维空间里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. php中的修饰符

    上面使用了一个修饰符U,详见关于修饰符的介绍. PHP正则表达式修饰符的理解: 在PHP正则表达式里面的修饰符可以改变正则的很多特性,使得正则表达式更加适合你的需要(注意:修饰符对于大小写是敏感的,这 ...

  2. 如何在Exe和BPL插件中实现公共变量共享及窗口溶入技术Demo源码

    如何在Exe和BPL插件中实现公共变量共享及窗口溶入技术Demo源码 1.Delphi编译方式介绍: 当我们在开发一个常规应用程序时,Delphi可以让我们用两种方式使用VCL,一种是把VCL中的申明 ...

  3. python27读书笔记0.2

    # -*- coding:utf-8 -*- ##s.partition(d)##Searches string s for the first occurrence of some delimite ...

  4. Sublime Text 2 插件

    一直以来写代码都是用的EditPlus,也尝试了一段时间学习Vim这神器,后来因为使用不习惯还是改回了原来的EditPlus.前几天朋友想我推荐了Sublime Text 2,喜欢尝鲜我的肯定是不会放 ...

  5. Python 列表实现字典的get功能

    字典有一个很好用的方法,就是get,既可以预防KeyError异常,也可以为不存在的key设置一个默认的value 例如: v=d.get('k','default') 而列表没有一个类似的方法,如果 ...

  6. Linux下STM32开发环境的搭建

    目录 一.概述 1.目的 2.开发环境描述 3.Eclipse构建的STM32集成开发环境结构 4.GDB / GDB Server 调试模型 二.搭建步骤 三.详细的搭建过程 1.安装eclipse ...

  7. 4.MVC框架开发(母版页的应用、按钮导致的Action处理、从界面向控制器传数据和HtmlHelper控件的实现(注册的实现))

    1.在视图里如何引入母版页 1)在视图里母版页都是放在View目录下面的Shared文件夹下面 2)母版页里的RenderBody()类似于ASP.NET里面的ContentPalceHolder占位 ...

  8. 使用KVC

    KVC是Key Value Coding的简称,意思是键值编码,号称Cocoa的大招.它是一种可以直接通过字符串key(对象在名称)来访问或修改对象属性的机制. 使用 1.利用KVC可以随意修改一个对 ...

  9. Memcached(六)Memcached的并发实例

    package com.sinosuperman.memcached; import java.io.IOException; import java.net.InetSocketAddress; i ...

  10. Discuz CDN优化方案

    DZ整体来说CDN是有点蛋疼的,因为毕竟琐碎,貌似大部分帖子都没有说全,这里罗列一下,给在用的孩儿们一点参考: 1.在后台设置CSS/JS走CDN路径,具体[全局]-[性能优化]-[服务器优化] 2. ...