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。的更多相关文章

  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最短路 #include <iostream> #include <cstdio> #include <cstring> #include < ...

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

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

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

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

  7. BFS POJ 2251 Dungeon Master

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

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

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

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

随机推荐

  1. 【字母全排列】 poj 1256

    深搜   注意与STL模版的去重函数唯一的区别就是有去重. #include <iostream> #include <cstdio> #include <string. ...

  2. POJ 2031 Building a Space Station 最小生成树模板

    题目大意:在三维坐标中给出n个细胞的x,y,z坐标和半径r.如果两个点相交或相切则不用修路,否则修一条路连接两个细胞的表面,求最小生成树. 题目思路:最小生成树树模板过了,没啥说的 #include& ...

  3. Java中的值栈

    OGNL表示式使用 和 值栈 OGNL是Object Graphic Navigation Language(对象图导航语言)的缩写,它是一个开源项目. Struts2框架使用OGNL作为默认的表达式 ...

  4. SuperSocket+unity 网络笔记

    学习SuperSocket 必须要注意的 代码是 static void Main(string[] args) { WebSocketServer appServer = new WebSocket ...

  5. mysql HA-keepalived

    关于MySQL-HA,目前有多种解决方案,比如heartbeat.drbd.mmm.共享存储,但是它们各有优缺点.heartbeat.drbd配置较为复杂,需要自己写脚本才能实现MySQL自动切换,对 ...

  6. Python -- OOP高级 -- 定制类

    __str__ 和 __repr__ :实例对象直接显示字符串 class Student: def __init__(self, name): self.name = name def __str_ ...

  7. 转:Ant使用指南

    一.概述 ant 是一个将软件编译.测试.部署等步骤联系在一起加以自动化的一个工具,大多用于Java环境中的软件开发.在实际软件开发中,有很多地方可以用到ant. 开发环境: System:Windo ...

  8. panel 绑定鼠标滚轮事件

    void formsample_mousewheel(object sender, MouseEventArgs e) { //获取光标位置 Point mousepoint = new Point( ...

  9. 优化viewHolder

  10. Javascript面向对象编程(二):构造函数的继承

    这个系列的第一部分,主要介绍了如何"封装"数据和方法,以及如何从原型对象生成实例. 今天要介绍的是,对象之间的"继承"的五种方法. 比如,现在有一个" ...