和迷宫问题区别不大,相比于POJ1321的棋盘问题,这里的BFS是三维的,即从4个方向变为6个方向。

用上队列的进出操作较为轻松。

#include<iostream>
#include<cstdio>
#include<cstring>
#include<queue>
#include<algorithm>
using namespace std; char map[35][35][35];
int vis[35][35][35];
int to[6][3]={{0,0,1},{0,0,-1},{0,1,0},{0,-1,0},{1,0,0},{-1,0,0}};
int k,n,m,sx,sy,sz,ex,ey,ez;
struct node
{
int x,y,z,step;
};
int check(int x,int y,int z)
{
if(x<0||y<0||z<0||x>=k||y>=n||z>=m||map[x][y][z]=='#'||vis[x][y][z])
return 1;
return 0;
}
int bfs()
{
int i;
node a,next;
queue<node> Q;
a.x = sx,a.y= sy,a.z = sz;
a.step = 0;
vis[sx][sy][sz]=1;
Q.push(a);
while(!Q.empty())
{
a=Q.front();
Q.pop();
if(a.x==ex&&a.y==ey&&a.z==ez)
return a.step;
for(i=0;i<6;i++)
{
next=a;
next.x=a.x+to[i][0];
next.y=a.y+to[i][1];
next.z=a.z+to[i][2];
if(check(next.x,next.y,next.z))
continue;
vis[next.x][next.y][next.z]=1;
next.step=a.step+1;
Q.push(next);
}
}
return 0;
}
int main()
{
int i,j,r;
while(scanf("%d%d%d",&k,&n,&m),n+m+k)
{
for(i=0;i<k;i++)
{
for(j=0;j<n;j++)
{
scanf("%s",map[i][j]);
for(r=0;r<m;r++)
{
if(map[i][j][r]=='S')
{
sx=i,sy=j,sz=r;
}
else if(map[i][j][r]=='E')
{
ex=i,ey=j,ez=r;
}
}
}
}
memset(vis,0,sizeof(vis));
int ans;
ans=bfs();
if(ans)
printf("Escaped in %d minute(s).\n",ans);
else
printf("Trapped!\n");
}
return 0;
}

POJ2251——Dungeon Master(三维BFS)的更多相关文章

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

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

  2. POJ 2251 Dungeon Master (三维BFS)

    题目链接:http://poj.org/problem?id=2251 Dungeon Master Time Limit: 1000MS   Memory Limit: 65536K Total S ...

  3. POJ:Dungeon Master(三维bfs模板题)

    Dungeon Master Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 16748   Accepted: 6522 D ...

  4. ZOJ 1940 Dungeon Master 三维BFS

    Dungeon Master Time Limit:1000MS     Memory Limit:65536KB     64bit IO Format:%I64d & %I64u Desc ...

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

  6. Dungeon Master(三维bfs)

    You are trapped in a 3D dungeon and need to find the quickest way out! The dungeon is composed of un ...

  7. POJ2251 Dungeon Master —— BFS

    题目链接:http://poj.org/problem?id=2251 Dungeon Master Time Limit: 1000MS   Memory Limit: 65536K Total S ...

  8. BFS POJ2251 Dungeon Master

    B - Dungeon Master Time Limit:1000MS     Memory Limit:65536KB     64bit IO Format:%I64d & %I64u ...

  9. 【POJ - 2251】Dungeon Master (bfs+优先队列)

    Dungeon Master  Descriptions: You are trapped in a 3D dungeon and need to find the quickest way out! ...

  10. UVa532 Dungeon Master 三维迷宫

        学习点: scanf可以自动过滤空行 搜索时要先判断是否越界(L R C),再判断其他条件是否满足 bfs搜索时可以在入口处(push时)判断是否达到目标,也可以在出口处(pop时)   #i ...

随机推荐

  1. 【网络编程】TCPIP_1_快速入门

    目录 前言 1. 快速入门 1.1 服务端编程简要步骤 1.2 客户端编程简要步骤 1.3 参考 前言 说明: demo 基于 Linux. 1. 快速入门 以下步骤简略了很多细节,但是通过下面的几个 ...

  2. 用于在公网环境下测试的Telnet/SSH服务器

    google: public telnet server list for example: telnet nethack.alt.org ssh nethack@alt.org

  3. Java面试常见基础问题

    1.equals和==有什么区别? ==比较两个对象在内存里是不是同一个对象,就是说在内存里的存储位置一致. 如:两个String对象存储的值是一样的,但是可能在内存里存储在不同的地方. equals ...

  4. Block循环引用详解

    前言 在项目中经常用到block,使用不当就很容易因为循环引用而造成内存泄漏.本文分析了block循环引用形成原因以及处理办法,如果有什么不对或者疑问请留言. 什么情况下block会造成循环引用 bl ...

  5. 带你读AI论文丨用于目标检测的高斯检测框与ProbIoU

    摘要:本文解读了<Gaussian Bounding Boxes and Probabilistic Intersection-over-Union for Object Detection&g ...

  6. 【Vulnhub】DC-1靶机

    一.信息收集 1.1 环境 kali : 192.168.124.141 DC-1 : 192.168.124.150 1.2 nmap进行扫描 :nmap -sV 192.168.124.150 I ...

  7. Linux学习手册

    入门概述 Linux 简介 Linux 内核最初只是由芬兰人林纳斯·托瓦兹(Linus Torvalds)在赫尔辛基大学上学时出于个人爱好而编写的. Linux 是一套免费使用和自由传播的类 Unix ...

  8. 使用F#编写PowerShell模块

    ▲F#和PowerShell模块 作为可能是人类世界最强大的Shell,PowerShell最大的特点是能够直接在命令间传递.NET对象,而支持这种能力的命令被称作cmdlet.自己编写PowerSh ...

  9. adobe cc 系列产品更改默认安装路径方法

    通过Adobe Creative Cloud 修改 1.在开始菜单中找到图下程序点开,并进行如下操作: 2.点击Apps位置 等待加载出软件,如图下所示. 点击右上角图标,会弹出以下窗口,点击首选项 ...

  10. C#中的几种锁:用户模式锁、内核模式锁、动态计数、监视锁

    参考网址: https://blog.csdn.net/weixin_43989331/article/details/105356008 C#中的几种锁:用户模式锁.内核模式锁.动态计数.监视锁介绍 ...