这次整理了一下广度优先搜索的框架,以后可以拿来直接用了。TjuOj1140是一个三维的迷宫题,在BFS时我增加了一个控制数组,用来对队列的出队进行控制,确保每次出队的结点均为同一步长的结点,个人认为比较适合这种迷宫搜索题。

BFS部分的代码如下:

int BFS ( node S , node T )
{
int s = ; //充当指针作用
memset(n,,sizeof(n));
n[s] = ; //初始化当前距离为1的点数为1(即原点)
node now = S;
visit[now.z][now.y][now.x] = ;
node temp;
queue<node> Q;
Q.push(now);
while( !Q.empty() ){
for(int j = ; j < n[s] ; j++ ){ //依次弹出所有距离为s的方块,进行四周搜索;
now = Q.front();
Q.pop();
for(int i = ; i < ;i++){ //向6个方向探索是否有通路
temp.x = now.x + dx[i];
temp.y = now.y + dy[i];
temp.z = now.z + dz[i];
if( visit[temp.z][temp.y][temp.x] == && inmap(temp) ){ //防止越界访问
temp.ch = maze[temp.z][temp.y][temp.x];
if (temp.ch == "E")
return s;
if (temp.ch == "."){ //如果探索到通路,将该通路标记为(当前距离+1) ,压入队列;
n[s + ]++; //(当前距离+1)的方块个数++
visit[temp.z][temp.y][temp.x] = ;
Q.push(temp);
}
}
}
}
s++;
}
return ;
}

题目比较好理解,从S出发找E(但不一定有解),原文如下:

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?

Input Specification

The input file consists of a number of dungeons. Each dungeon description starts with a line containing three integers LR and C (all limited to 30 in size). 
L is the number of levels making up the dungeon. 
R and C are the number of rows and columns making up the plan of each level. 
Then there will follow L blocks of R lines each containing C characters. Each character describes one cell of the dungeon. A cell full of rock is indicated by a '#' and empty cells are represented by a '.'. Your starting position is indicated by 'S' and the exit by the letter 'E'. There's a single blank line after each level. Input is terminated by three zeroes for LR and C.

Output Specification

Each maze generates one line of output. If it is possible to reach the exit, print a line of the form

Escaped in x minute(s).

where x is replaced by the shortest time it takes to escape. 
If it is not possible to escape, print the line

Trapped!

Sample Input

3 4 5
S....
.###.
.##..
###.# #####
#####
##.##
##... #####
#####
#.###
####E 1 3 3
S##
#E#
### 0 0 0

Sample Output

Escaped in 11 minute(s).
Trapped!
/*
* 1140_Dungeon Master.cpp
*
* Created on: 2018年11月14日
* Author: Jeason
*/ #include <iostream>
#include <stdio.h>
#include <cstring>
#include <sstream>
#include <queue>
#define N 70
using namespace std;
int levels,rows,columns;
char maze[N][N][N];
int visit[N][N][N];
int n[ ] = {}; //用于记录每个距离的方块个数;
string line;
int dx[] = {,-,,,,};
int dy[] = {,,,-,,};
int dz[] = {,,,,,-}; struct node {
int x,y,z;
string ch;
}; bool inmap(node A) {
if(A.x < || A.x >= columns) return false;
if(A.y < || A.y >= rows) return false;
if(A.z < || A.z >= levels) return false;
return true;
} int BFS(node S,node T)
{
int s = ; //充当指针作用
memset(n,,sizeof(n));
n[s] = ; //初始化当前距离为1的点数为1(即原点)
node now = S;
visit[now.z][now.y][now.x] = ;
node temp;
queue<node> Q;
Q.push(now);
while( !Q.empty() ){
for(int j = ; j < n[s] ; j++ ){ //依次弹出所有距离为s的方块,进行四周搜索;
now = Q.front();
Q.pop();
for(int i = ; i < ;i++){ //向6个方向探索是否有通路
temp.x = now.x + dx[i];
temp.y = now.y + dy[i];
temp.z = now.z + dz[i];
if( visit[temp.z][temp.y][temp.x] == && inmap(temp) ){ //防止越界访问
temp.ch = maze[temp.z][temp.y][temp.x];
if (temp.ch == "E")
return s;
if (temp.ch == "."){ //如果探索到通路,将该通路标记为(当前距离+1) ,压入队列;
n[s + ]++; //(当前距离+1)的方块个数++
visit[temp.z][temp.y][temp.x] = ;
Q.push(temp);
}
}
}
}
s++;
}
return ;
} int main()
{
node S,T;
cin >> levels >> rows >> columns; while( (levels != )&&(rows != )&&(columns != ) ){
memset(maze, , sizeof(maze)); //读入数据;
memset(visit, , sizeof(visit));
for(int i = ;i < levels ;i++){
for(int j = ;j < rows ;j++){
cin >> line;
for(int k = ;k < columns ;k++){
maze[i][j][k] = line[k];
if(line[k] == 'S'){ //找起点
S.z = i; S.x = k; S.y = j;
S.ch = "S";
}
}
}
}
int minutes = BFS( S, T );
if(minutes != )
cout << "Escaped in "<< minutes << " minute(s)."<< endl;
else
cout << "Trapped!" <<endl;
// for(int i = 0;i < levels ;i++){ //输出地图
// for(int j = 0;j < rows ;j++){
// for(int k = 0;k < columns ;k++){
// cout << maze[i][j][k];
// }
// cout << endl;
// }
// cout << endl;
// }
cin >> levels >> rows >> columns;
} return ;
} /* Sample Input
3 4 5
S....
.###.
.##..
###.# #####
#####
##.##
##... #####
#####
#.###
####E 1 3 3
S##
#E#
### 0 0 0
Sample Output
Escaped in 11 minute(s).
Trapped!
*/

广度优先搜索(BFS)----------------(TjuOj1140_Dungeon Master)的更多相关文章

  1. 深度优先搜索DFS和广度优先搜索BFS简单解析(新手向)

    深度优先搜索DFS和广度优先搜索BFS简单解析 与树的遍历类似,图的遍历要求从某一点出发,每个点仅被访问一次,这个过程就是图的遍历.图的遍历常用的有深度优先搜索和广度优先搜索,这两者对于有向图和无向图 ...

  2. 利用广度优先搜索(BFS)与深度优先搜索(DFS)实现岛屿个数的问题(java)

    需要说明一点,要成功运行本贴代码,需要重新复制我第一篇随笔<简单的循环队列>代码(版本有更新). 进入今天的主题. 今天这篇文章主要探讨广度优先搜索(BFS)结合队列和深度优先搜索(DFS ...

  3. 深度优先搜索DFS和广度优先搜索BFS简单解析

    转自:https://www.cnblogs.com/FZfangzheng/p/8529132.html 深度优先搜索DFS和广度优先搜索BFS简单解析 与树的遍历类似,图的遍历要求从某一点出发,每 ...

  4. 【数据结构与算法Python版学习笔记】图——词梯问题 广度优先搜索 BFS

    词梯Word Ladder问题 要求是相邻两个单词之间差异只能是1个字母,如FOOL变SAGE: FOOL >> POOL >> POLL >> POLE > ...

  5. 广度优先搜索 BFS 学习笔记

    广度优先搜索 BFS 学习笔记 引入 广搜是图论中的基础算法之一,属于一种盲目搜寻方法. 广搜需要使用队列来实现,分以下几步: 将起点插入队尾: 取队首 \(u\),如果 $u\to v $ 有一条路 ...

  6. 广度优先搜索(BFS)

    定义 维基百科:https://en.wikipedia.org/wiki/Breadth-first_search 给定图G=(V,E)和一个可识别的源结点s,广度优先搜索对图G中的边进行系统性的探 ...

  7. 数据结构和算法总结(一):广度优先搜索BFS和深度优先搜索DFS

    前言 这几天复习图论算法,觉得BFS和DFS挺重要的,而且应用比较多,故记录一下. 广度优先搜索 有一个有向图如图a 图a 广度优先搜索的策略是: 从起始点开始遍历其邻接的节点,由此向外不断扩散. 1 ...

  8. 深度优先搜索DFS和广度优先搜索BFS

    DFS简介 深度优先搜索,一般会设置一个数组visited记录每个顶点的访问状态,初始状态图中所有顶点均未被访问,从某个未被访问过的顶点开始按照某个原则一直往深处访问,访问的过程中随时更新数组visi ...

  9. (转)广度优先搜索BFS和深度优先搜索DFS

    1. 广度优先搜索介绍 广度优先搜索算法(Breadth First Search),又称为"宽度优先搜索"或"横向优先搜索",简称BFS. 它的思想是:从图中 ...

  10. 广度优先搜索 BFS算法

    广度优先搜索算法(Breadth-First-Search,BFS),又称作宽度优先搜索.BFS算法是从根节点开始,沿着树的宽度遍历树的节点.如果所有节点均被访问,则算法中止. 算法思想 1.首先将根 ...

随机推荐

  1. JAVA每日一旅2

    1.关于类型转换 两个数值进行二元操作时,会有如下的转换操作: 如果两个操作数其中有一个是double类型,另一个操作就会转换为double类型. 否则,如果其中一个操作数是float类型,另一个将会 ...

  2. 20135202闫佳歆--week6 课本第三章学习笔记

    第三章 进程管理 一.进程 1.进程 进程就是处于执行期的程序. 进程就是正在执行的程序代码的实时结果. 进程是处于执行期的程序以及相关的资源的总称. 进程包括代码段和其他资源. 2.线程 执行线程, ...

  3. 关于java中指针的概念

    今天寡人遇到一个问题,扫描非关系数据库中的图(由node和rel组成),将其转化成由寡人自定义的gnode和gedge组成的图. gnode类包含结点的id,label和包含此gnode的gedge的 ...

  4. 团队作业四-WBS练习

    我们团队开发的是四则运算,主要面对的用户是小学生.老师及学生家长.经过我们组成员的讨论和结合实际及自身能力,对团队成员分配任务,队长负责全局工作主要负责任务,统一进度,和适量的编码,露哥和阮磊主要负责 ...

  5. wordpress 点击文章图片 不能编辑(chrome下面) wordpress Uncaught DOMException: Failed to execute 'setBaseAndExtent' on 'Selection': There is no child at offset 1.

    说明:在chrome下面,编辑文章插入的图片,点击到图片上面,没有菜单显示. 报错: tinymce.min.js:10 Uncaught DOMException: Failed to execut ...

  6. Python的数据结构

    目录 Python内置的数据结构 序列Sequence 映射Mapping 集合Sets Python内置的数据结构 Python语言简洁明了,可以用较少的代码实现同样的功能.这其中Python内置的 ...

  7. java使用JMail通过QQ邮件服务器实现自动发送邮件

    前言:项目开发的过程中,我们项目需要一个自动发送邮件提醒的小功能,于是简单的研究了一下java的JMail来实现自动发送邮件的功能.已被后期需要参考. 一.准备 实现的原理很简单:发送人 , 中转的邮 ...

  8. Duplicate spring bean id

    问题背景:从本地调用服务器的dubbo接口进行测试 实现思路:基于IDEA+Spring+maven+Dubbo搭建测试项目,从本地直接调用   具体实现思路可参考博客:https://www.cnb ...

  9. mininet *** Error: RTNETLINK answers: No such file or directory 问题及解决方法

    一.问题 按照mininet官网中从源码安装步骤进行安装后,运行命令sudo mn --link tc,bw=10,提示说*** Error: RTNETLINK answers: No such f ...

  10. 【题解】 bzoj2435: [Noi2011]道路修建 (傻逼题)

    bzoj2435,懒得复制,戳我戳我 Solution: 模拟即可(有点傻逼啊 Code: //It is coded by Ning_Mew on 5.13 #include<bits/std ...