http://poj.org/problem?id=2251

一道简单的BFS,只不过是二维数组,变三维数组,也就在原来基础上加了两个方向。

题意就是从S走到E,#不能走。

 #include <stdio.h>
#include <string.h>
#include <queue>
#include <iostream>
using namespace std; #define judge(x,y,z) mark[x][y][z]&&str1[x][y][z]!='#'&&x>=0&&x<l&&y>=0&&y<r&&z>=0&&z<c //判断这个点是否可以走。
char str1[][][];
int l,r,c,sx,sy,sz,ex,ey,ez,step[][][];
int dic[][]{,,,,,-,,,,-,,,,,,,-,}; //方向。 struct note{
int x,y,z;
};
bool mark[][][];
queue<note>s; //定义了一个s的结构体队列。因为一个点要通过三个值来确定,所以这里选择用结构体是比较方便的。 void bfs(int x,int y,int z)
{
note u;
u.x=x,u.y=y,u.z=z;
while(!s.empty())
s.pop();
s.push(u);
while(!s.empty())
{
note v;
v=s.front();
s.pop();
if(v.x==ex&&v.y==ey&&v.z==ez) return;
for(int i=;i<;i++)
{
u.x=v.x+dic[i][];
u.y=v.y+dic[i][];
u.z=v.z+dic[i][];
if(judge(u.x,u.y,u.z)){
step[u.x][u.y][u.z]=step[v.x][v.y][v.z]+;
mark[u.x][u.y][u.z]=false;
s.push(u);
}
}
}
} int main()
{
while(scanf("%d%d%d",&l,&r,&c),l!=&&r!=&&c!=)
{
memset(mark,true,sizeof(mark));
memset(str1,,sizeof(str1));
memset(step,,sizeof(step));
for(int i=;i<l;i++)
for(int j=;j<r;j++)
{
scanf("%s",str1[i][j]);
for(int k=;k<c;k++)
{
if(str1[i][j][k]=='S')
{
sx=i;
sy=j;
sz=k;
}
if(str1[i][j][k]=='E')
{
ex=i;
ey=j;
ez=k;
}
}
}
step[sx][sy][sz]=;
bfs(sx,sy,sz);
if(step[ex][ey][ez]==) printf("Trapped!\n"); //如果走不到那个点,那么那个点的步数肯定是为0的,通过这个条件来判断是否可以找到那一个出口。
else printf("Escaped in %d minute(s).\n",step[ex][ey][ez]);
}
}

poj 2251的更多相关文章

  1. 【BFS】POJ 2251

    POJ 2251 Dungeon Master 题意:有一个地图,三维,走的方向是上下,左右,前后.问你最小步数从起始点走到出口. 思路:三维的BFS,就是多加一组状态,需要细心(不细心如我就找了半个 ...

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

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

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

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

  4. 【POJ 2251】Dungeon Master(bfs)

    BUPT2017 wintertraining(16) #5 B POJ - 2251 题意 3维的地图,求从S到E的最短路径长度 题解 bfs 代码 #include <cstdio> ...

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

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

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

  7. BFS POJ 2251 Dungeon Master

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

  8. [ACM训练] 算法初级 之 搜索算法 之 深度优先算法DFS (POJ 2251+2488+3083+3009+1321)

    对于深度优先算法,第一个直观的想法是只要是要求输出最短情况的详细步骤的题目基本上都要使用深度优先来解决.比较常见的题目类型比如寻路等,可以结合相关的经典算法进行分析. 常用步骤: 第一道题目:Dung ...

  9. poj 2251 Dungeon Master

    http://poj.org/problem?id=2251 Dungeon Master Time Limit: 1000MS   Memory Limit: 65536K Total Submis ...

随机推荐

  1. Centos 7.0查看硬盘使用情况 命令

    查看硬盘使用情况 df -hl [root@localhost ~]# df -hl文件系统         容量    已用    可用    已用%.     挂载点/dev/sda3       ...

  2. js字节转换、字节转换GB等

    //文件大小换算 function bytesToSize(bytes) { if (bytes === 0) return '0 B'; var k = 1024; sizes = ['B','KB ...

  3. 关于VS打开cshtml出现 未能完成该操作。无效指针

    关于VS打开cshtml出现 未能完成该操作.无效指针 第一步:关闭VS 第二部:删除%LocalAppData%\Microsoft\VisualStudio\14.0\ComponentModel ...

  4. Google地图实现

    API地址:https://developers.google.com/maps/documentation/javascript/tutorial <div id="map" ...

  5. html兼容性

    IE  property:value\9; //for all IE IE6 _property:value; IE7 *property:value; IE8 +property:value; IE ...

  6. 【8-15】Markdown语法学习

    学习Markdown语法 来源简书URL #,支持六级标题 列表 用-或*(指无序列表),有序列表直接1. 2. 3. 这样,中间有空格,可乱序(-+*都可,不能混合使用,混合使用为嵌套) 这是一个无 ...

  7. git log --stat常用命令

    ​1,显示被修改文件的修改统计信息,添加或删除了多少行. git log --stat 2,显示最近两条的修改 git log --stat -2 3,显示具体的修改 git log -p -2 4, ...

  8. C/C++/Java/C#语言的基本类型

    C语言的基本类型有:char, short ,int ,long ,float ,double 一共6种基本类型. C++语言有:bool, char ,wchar_t, short, int , l ...

  9. iOS分类、延展和子类的区别

    iOS分类.延展和子类的区别 类别.延展.子类的区别   类别 延展 子类 功能 为类添加方法,不用知道类的源码,添加变量(通过运行时,具体参考下面注解) 为类添加私有变量和私有方法,在类的源文件中书 ...

  10. CentOS的SSH,Putty配置说明

    基本资源: CentOS5.5 (32位) , Mysql6.0 ,Putty ,SSH   Step: 1.VMWare中装好CentOS    A. 可能存在ifconfig等命令无法正常识别) ...