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 ...
随机推荐
- Ubuntu下安装php调试工具xdebug
安装xdebug: sudo apt-get install php-pear sudo apt-get install php5-dev pecl install xdebug 创建配置文件: /x ...
- Delphi新语法和ifthen的扩展联想
Delphi之前已经重载了好多个ifthen函数 Math单元 ): Integer; overload; inline; ): Int64; overload; inline; ): UInt64; ...
- django1.6之mysql配置
# Database# https://docs.djangoproject.com/en/1.6/ref/settings/#databases setting 配置修改 INSTALLED_APP ...
- oracle忘记用户密码
在cmd命令行下输入sqlplus / as sysdba alter user system identified by abc; 就可以将system用户的密码改成abc了. alter user ...
- Chart Style
How to add label to column chart http://blogs.msdn.com/b/delay/archive/2009/07/27/simple-column-labe ...
- 3.MVC框架开发(Razor内嵌函数)
1.建立没有返回值的Razor内嵌函数(但是能直接输出内容) 必须以@符号开头,没有返回值但能直接输出内容,比如: @helper showTitle(string strTitle){ ){ @(s ...
- Unity3d Shader开发(五)Fallback ,Category
Fallback定义在所有子着色器后.简单来说,它表示"如果没有任何子着色器能被运行在当前硬件上,请尝试使用降级着色器". Syntax 语法 Fallback "nam ...
- 仿window阿里旺旺登陆界面,打印机吐纸动画效果-b
偶然的机会发现window的阿里旺旺的登陆效果蛮有意思的,于是就模仿着做了一下打印机吐纸的动画效果看起来很神奇的东西,实现起来却不难,下面我给大家看下主要的源码. - (void)createUI{ ...
- SQL2008附加数据库提示错误:5120
前几天在附加数据库时,出现了这个错误 在win7 x64系统上使用sql2008进行附加数据库(包括在x86系统正在使用的数据库文件,直接拷贝附加在X64系统中)时,提示无法打开文 ...
- 使用Yeoman搭建 AngularJS 应用 (3) —— 让我们搭建一个网页应用
原文地址:http://yeoman.io/codelab/setup.html 与Yeoman的交互大多数是通过命令行.在苹果机器需要使用Terminal应用,在Linux使用shell.如果使用W ...