(简单) POJ 2251 Dungeon Master,BFS。
Description
You are trapped in a 3D dungeon and need to find the quickest way out! The dungeon is composed of unit cubes which may or may not be filled with rock. It takes one minute to move one unit north, south, east, west, up or down. You cannot move diagonally and the maze is surrounded by solid rock on all sides.
Is an escape possible? If yes, how long will it take?
一个三维的迷宫问题,和二维没什么区别,直接BFS就好。
代码如下:
#include<iostream>
#include<cstdio>
#include<cstring>
#include<queue> using namespace std; bool map1[][][];
bool vis[][][];
int L,R,C;
int Sl,Sr,Sc,El,Er,Ec; struct state
{
int l,r,c;
int num; state() {}
state(int x,int y,int z,int n):l(x),r(y),c(z),num(n) {}
}; bool judge(int x,int y,int z)
{
if(x<=||x>L||y<=||y>R||z<=||z>C)
return ; if(map1[x][y][z]==)
return ; if(vis[x][y][z])
return ; vis[x][y][z]=;
return ;
} int bfs()
{
queue <state> que;
state temp;
int tl,tr,tc; que.push(state(Sl,Sr,Sc,)); while(!que.empty())
{
temp=que.front();
que.pop(); if(temp.l==El&&temp.r==Er&&temp.c==Ec)
return temp.num; tl=temp.l;
tr=temp.r;
tc=temp.c; if(judge(tl-,tr,tc))
que.push(state(tl-,tr,tc,temp.num+));
if(judge(tl+,tr,tc))
que.push(state(tl+,tr,tc,temp.num+));
if(judge(tl,tr-,tc))
que.push(state(tl,tr-,tc,temp.num+));
if(judge(tl,tr+,tc))
que.push(state(tl,tr+,tc,temp.num+));
if(judge(tl,tr,tc-))
que.push(state(tl,tr,tc-,temp.num+));
if(judge(tl,tr,tc+))
que.push(state(tl,tr,tc+,temp.num+));
} return -;
} int main()
{
char s[];
int ans; ios::sync_with_stdio(false); while(cin>>L>>R>>C)
{
memset(vis,,sizeof(vis)); if(!L&&!R&&!C)
break; for(int i=;i<=L;++i)
for(int j=;j<=R;++j)
{
cin>>s;
for(int k=;k<=C;++k)
switch(s[k-])
{
case 'S':
Sl=i;
Sr=j;
Sc=k;
map1[i][j][k]=;
break;
case 'E':
El=i;
Er=j;
Ec=k;
map1[i][j][k]=;
break;
case '.':
map1[i][j][k]=;
break;
case '#':
map1[i][j][k]=;
break;
}
} ans=bfs(); if(ans!=-)
cout<<"Escaped in "<<ans<<" minute(s).\n";
else
cout<<"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最短路 #include <iostream> #include <cstdio> #include <cstring> #include < ...
- 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 ...
随机推荐
- C语言头文件
最近在工作当中遇到了一点小问题,关于C语言头文件的应用问题,主要还是关于全局变量的定义和声明问题.学习C语言已经有好几年了,工作使用也近半年了,但是对于这部分的东西的确还没有深入的思考过.概念上还是比 ...
- iOS真机测试中出现dyld`dyld_fatal_error错误
最近进入一家新公司,接手了一个之前由外包公司承接的项目.首先吐槽一下项目质量,哎毕竟也憋了很久了. 1.上手项目是打不开的,所有framework静态库全体飘红,一编译七八十错误.最终是偷懒还是什么就 ...
- UVA 1400 线段树
input n m 1<=n,m<=500000 a1 a2 ... an |ai|<=1e9 m行查询 每行一对a b output 对于每对a b输出区间[a,b]中最小连续和x ...
- keepalived: Compile & startup
first get keepalived source from git: git clone https://github.com/acassen/keepalived then unzip and ...
- SSH整合中为获取表单对象Action类实现的接口及拦截器配置
保存员工或者用户信息时,以员工为例,是通过表单收集信息的,需要把这些信息赋给一个对象,然后保存到数据库中.对应的Action类须实现Preparable接口及ModelDriven接口,且在Actio ...
- JS-运动基础(一)
<title>无标题文档</title> <style> #div1{width:200px;height:200px; background:red; posit ...
- Hrbustoj 2266 Legendary Weights(辗转相除求最大公约数)
题意:这个题目的意思是给出一些砝码,问我们能不能根据这些砝码称量出任意重量的物品,最大公约数并不难求,难的在于如何建立这个模型. 思路:根据数论的基础知识,两个数a,b的最大公约数是a*x + b*y ...
- zepto.js 学习之(一)
中文文档:http://mweb.baidu.com/zeptoapi/#attr
- MapReduce入门例子
计算文档中不同单词的个数. hello you hello me 步骤如下:
- vm选项大全
http://hllvm.group.iteye.com/group/topic/27945 java -XX:后边的总记不住 vm选项大全 http://www.oracle.com/technet ...