一些BFS参考的博客:

https://blog.csdn.net/ldx19980108/article/details/78641127

https://blog.csdn.net/u011437229/article/details/53188837

https://blog.csdn.net/m0_37316917/article/details/70879977

关于queue头文件常用函数的解释:

https://www.cnblogs.com/xuning/p/3321733.html

具体内容等做到了慢慢写,现在还在研究queue

迷宫问题

定义一个二维数组:

int maze[5][5] = {

0, 1, 0, 0, 0,

0, 1, 0, 1, 0,

0, 0, 0, 0, 0,

0, 1, 1, 1, 0,

0, 0, 0, 1, 0,

};

它表示一个迷宫,其中的1表示墙壁,0表示可以走的路,只能横着走或竖着走,不能斜着走,要求编程序找出从左上角到右下角的最短路线。

Input

一个5 × 5的二维数组,表示一个迷宫。数据保证有唯一解。

Output

左上角到右下角的最短路径,格式如样例所示。

Sample Input

0 1 0 0 0
0 1 0 1 0
0 0 0 0 0
0 1 1 1 0
0 0 0 1 0

Sample Output

(0, 0)
(1, 0)
(2, 0)
(2, 1)
(2, 2)
(2, 3)
(2, 4)
(3, 4)
(4, 4)
#include <algorithm>
#include <iostream>
#include <cstring>
#include <cstdio>
#include <cmath>
#include <stack>
#include <queue>
using namespace std;
/*做这题有了很多新感悟,如果是要探求一共有几种解法的话,那么我应该使用DFS算法,
但要是求最短路径的话,BFS算法是按层算的,所以它能更容易算出最短路径*/
/*这道题有个令人舒服的操作就是讲队尾的数据不断抽出(因为有个指针不断指向父节点)
然后放入堆栈中,再一个个从堆栈顶部抽出,所以我最后得出的数据要是最后的指针*/
int map[][], newx[]={-,,,}, newy[]={,,-,};
bool sign[][];
class location
{
public:
int x,y;//坐标
location* father;//指向父节点的位置
}; location* bfs()
{
int x, y;
queue<location*> Q;//建立关于location指针的队列
location* begin = new location();//【重要】这里需要new一个空间来放置新数据
begin->x = ; begin->y = ; begin->father = NULL;
//初始化起点,其父节点为空,要放在函数内创建队列【重要】
Q.push(begin);//放入队列的第一个点
while ( !Q.empty() )//这里是队列和堆栈常用的判断方式,若为空队列则返回1
{
location* linshi = Q.front();
Q.pop();//获得第一个地址后就可以将它从队列里拉出来了
for ( int i= ; i< ; i++ )//开始了哦,四方遍历
{
x = linshi->x + newx[i];
y = linshi->y + newy[i];
if ( x< || x> || y< || y> ) continue;
//超出边界,判断下一个点吧
if ( sign[x][y] == true ) continue;
//如果这个点已经走过了,我也不会再回头
if ( map[x][y] == ) continue;
//如果有墙壁的话,我当然不会傻傻撞上去的啦
//经过上面的三连判断,终于,这个点是可以走过去的
location* NEW = new location();
NEW->x = x; NEW->y = y; NEW->father = linshi;
//建立一个新指针,将这个可行点代入数值
if ( x== && y== ) return NEW;
//已经达到终点了哦,把这个地址带回去吧
Q.push(NEW);//不满足上面这个条件呀,那就将这个点放入队列之中吧
sign[x][y] = true;//本点到此一游,来过了哦
}
}
} int main()
{
for(int i=;i<=;i++)
{
for(int j=;j<=;j++)
{
cin>>map[i][j];//初始化迷宫
sign[i][j]=false;//重置sign数组
}
}
location* p=bfs();//将最后得到的地址赋给p
stack<location*> S;
while ( p != NULL )//只要指针不为空的话
{
S.push(p);
p = p->father;
}
while( !S.empty() )//只要堆栈不为空的话
{
location* temp = S.top();//依旧是新建一个指针,其等于堆顶的地址
S.pop();//【得到它的信息便可以删除啦】
cout<< "(" << temp->x- << ", " << temp->y- << ")" <<endl;
}
return ;
}

这些代码的灵感很多来自于下面的代码,【】中做的是我的注释

#include<iostream>
#include<queue>
#include<stack>
using namespace std;
int migong[][];//保存迷宫图
bool flag[][];//标记改点是否到达过
class Stat
{
public:
int x,y;
int t;
Stat* father;//指向其父状态,用于逆向寻找路径
};
int R[][]={
-,,
,,
,-,
,
};//用于状态扩展
Stat* BFS()//返回终点状态
{
queue<Stat*> Q;//用于存储还没有被扩展的(即将扩展)状态//【指向这个位置】
int x=,y=,t=;
Stat* start = new Stat();//这里是我栽过大坑,由于是在函数内部定义的对象,必须要用new来分配内存与堆中
start->x=start->y=;//【start的位置为(0,0)】
start->t = ;
start->father = NULL;//开始状态没有父状态
//【上面这几句话是初始点的初始化】
Q.push(start);//【这里是放入第一个点的意思】
while( Q.empty() == false )//直到搜索完所有状态退出循环
{
Stat* temp = Q.front();//【新建一个临时的指针指向队头】
Q.pop();//【删除队头,虽然是删除队头,但是这个指针本身并未消失,只是被移出】
for(int i=;i<;i++)//这里就是状态的扩展,向上下左右四个方向扩展
{
x = temp->x + R[i][];
y = temp->y + R[i][];
if(x<||y<||x>||y>)//超出边界,便直接舍弃该状态
continue;
if(flag[x][y] == true)//到达过该状态,也直接舍弃
continue;
if(migong[x][y] == )//没有路,也直接舍弃
continue;
//【上面这三连判断的代码很酷,很对我口味】
Stat* tempS = new Stat();//创建新状态
tempS->x = x;
tempS->y = y;
tempS->t = temp->t+;//时间加一
tempS->father = temp;//指向父节点
//【上面这段作用主要是将可行点放入队列中】
if( x == && y == )
{
return tempS;//搜索到了终点,便返回
}
Q.push(tempS);//将新状态加入队列中
flag[x][y] = true;//标记该状态已经到达过
}
//【这段for循环后,刚开始进入的点的所有可能性都放入队列中,准备进入下一
//个阶段,继续检索这个队列】
}
return start;
} int main()
{
for(int i=;i<;i++)//循环输入迷宫
{
for(int j=;j<;j++)
{
cin>>migong[i][j];
flag[i][j] = false;//全部重置为未标记
}
}
Stat* p = BFS();
stack<Stat*> S;//声明堆栈S
//放入栈中,主要是为了让其反序,不然从目标状态找其父节点遍历的话,是反的
//【这个想法很好,不这么做的话从队列里先出来的是终点】
while(p != NULL)//
{
S.push(p);
p = p->father;
}
while(S.empty() == false)
{
Stat* temp = S.top();
S.pop();//【得到它的信息便可以删除啦】
cout<< "(" << temp->x << "," << temp->y << ")" <<endl;
}
}

Rescue

Angel was caught by the MOLIGPY! He was put in prison by Moligpy. The prison is described as a N * M (N, M <= 200) matrix. There are WALLs, ROADs, and GUARDs in the prison.

Angel's friends want to save Angel. Their task is: approach Angel. We assume that "approach Angel" is to get to the position where Angel stays. When there's a guard in the grid, we must kill him (or her?) to move into the grid. We assume that we moving up, down, right, left takes us 1 unit time, and killing a guard takes 1 unit time, too. And we are strong enough to kill all the guards.

You have to calculate the minimal time to approach Angel. (We can move only UP, DOWN, LEFT and RIGHT, to the neighbor grid within bound, of course.)

InputFirst line contains two integers stand for N and M.

Then N lines follows, every line has M characters. "." stands for road, "a" stands for Angel, and "r" stands for each of Angel's friend.

Process to the end of the file. 
OutputFor each test case, your program should output a single integer, standing for the minimal time needed. If such a number does no exist, you should output a line containing "Poor ANGEL has to stay in the prison all his life." 
Sample Input

7 8
#.#####.
#.a#..r.
#..#x...
..#..#.#
#...##..
.#......
........

Sample Output

13
/*#include <algorithm>
#include <iostream>
#include <cstring>
#include <cstdio>
#include <vector>
#include <cmath>
#include <stack>
#include <queue>
using namespace std;*/
/*这道题想法肯定是要BFS,但我不是很清楚大小顶堆会被用在哪里*/
/*貌似有点想法了,因为杀死卫兵需要多花一秒的时间,所以要BFS出所有的点,再提取出
那个步数最少的位置,因为如果是步数加一的话,我就不能将其马上加入队列中了,必须将
这个步数放到下一层的队列中,这样才算是公平公正*/
/*太难了= =我考虑的东西真的太少了,这个BFS和其他的BFS相比,就是标记步数的时候不
一定要继续标记,因为有卫兵的存在,卫兵还是被拉入下一步考虑的好,因为它和其他的点
不是同等级的运算*/
/*int n, m, step[205][205], newx[4]={-1,1,0,0}, newy[4]={0,0,-1,1}, x2, y2;
char map[205][205]; struct location
{
int x, y, steps;
friend bool operator > ( location a, location b)
{
return a.steps > b.steps;
}
} temp, linshi; priority_queue< location , vector<location> , greater<location> > S;
//用来计算到达终点路线的步数 void BFS(int xx , int yy)
{
int x, y, i, j, steps;
memset( step , 0 , sizeof(step) );
queue<location> Q;//用来BFS的普通队列
step[xx][yy]=1;
temp.x = xx; temp.y = yy; temp.steps = 1;
Q.push(temp);
while ( !Q.empty() )
{
temp = Q.front();
Q.pop();
for ( i=0 ; i<4 ; i++ )
{
x = temp.x + newx[i];
y = temp.y + newy[i];
steps = temp.steps + 1;
if ( x>n || x<1 || y>m || y<1 ) continue;//越界,告辞!
if ( map[x][y] != 'a' )
{
if ( map[x][y] == '#' ) continue; //撞墙,告辞!
if ( step[x][y] != 0 ) continue;//用过,告辞!
if ( map[x][y] == 'x' ) steps+=1;//有怪自增一
}
linshi.x = x; linshi.y = y; linshi.steps = steps;
step[x][y] = steps;
if ( map[x][y] != 'a' ) Q.push(linshi);
//cout<<linshi.x<<' '<<linshi.y<<endl;
if ( map[x][y] == 'a' ) S.push(linshi);
}
}
/*for ( i=1 ; i<=n ; i++ )
{
for ( j=1 ; j<=m ; j++ )
{
printf("%d ", step[i][j]);
}
cout<<endl;
}
return;
} int main(void)
{
int i, j, beginx, beginy;
while ( scanf("%d%d", &n, &m) != EOF )
{
getchar();
for ( i=1 ; i<=n ; i++ )
{
for ( j=1 ; j<=m ; j++ )
{
scanf("%c", &map[i][j] );
if ( map[i][j] == 'r' )
{
beginx=i;
beginy=j;
}
if ( map[i][j] == 'a' )
{
x2=i;
y2=j;
}
}
getchar();
}
while ( !S.empty() ) S.pop();
BFS(beginx, beginy);
if ( !S.empty() ) printf("%d\n", S.top().steps-1 );
else printf("Poor ANGEL has to stay in the prison all his life.\n");
}
return 0;
}*/
#include <stdio.h>
#include <string.h>
#include <queue>
using namespace std; struct node
{
int x,y,step;
friend bool operator < (node n1,node n2)
{
return n1.step > n2.step;
//尽管下面是大顶堆的格式,但这里转换成小顶堆
}
}; int n,m,vis[][];
char map[][];
int x1,x2,y1,y2;
int to[][] = {,,-,,,,,-}; int bfs()
{
int i;
priority_queue<node> Q;//顶堆声明
//将步数最少的放在优先处理,不用像我一样放在一起比较
node a,next;//临时数据声明
a.x = x1;
a.y = y1;
a.step = ;
Q.push(a);
vis[x1][y1] = ;
while( !Q.empty() )
{
a = Q.top();
Q.pop();
if( a.x == x2 && a.y == y2 ) return a.step;
for(i = ; i<; i++)
{
next = a;
next.x += to[i][];
next.y += to[i][];
if(next.x< || next.y< || next.x>=n || next.y>=m || !vis[next.x][next.y] || map[next.x][next.y] == '#') continue;
next.step++;
if( map[next.x][next.y] == 'x')//卫兵处多花费了一秒
next.step++;
if( next.step <= vis[next.x][next.y] )//存入最小时间
{
vis[next.x][next.y] = next.step;
Q.push(next);
//时间要尽量小,否则就不要存进来了
}
}
}
return ;
} int main()
{
int i,j;
while( scanf("%d%d",&n,&m) != EOF )
{
for(i = ; i < n; i++)
{
scanf("%s",map[i]);
for(j = ; map[i][j]; j++)
{
if(map[i][j] == 'r')
{
x1 = i;
y1 = j;
}
else if(map[i][j] == 'a')
{
x2 = i;
y2 = j;
}
}
}
memset( vis , , sizeof(vis) );
int ans = ;
ans = bfs();
if(ans)
printf("%d\n",ans);
else
printf("Poor ANGEL has to stay in the prison all his life.\n");
} return ;
}

【2018.07.30】(广度优先搜索算法/队列)学习BFS算法小记的更多相关文章

  1. 【2018.07.29】(深度优先搜索/回溯)学习DFS算法小记

    参考网站:https://blog.csdn.net/ldx19980108/article/details/76324307 这个网站里有动态图给我们体现BFS和DFS的区别:https://www ...

  2. 【2018.07.28】(字符串/回文串)学习Manacher算法小记

    主要是应用在回文串啦,原理也理解了老半天,如果没有图片的话,我也看不太懂它的原理 学习的灵感来源来自于:https://segmentfault.com/a/1190000008484167 /* 最 ...

  3. 【2018.07.27】(字符串/找相同)学习KMP算法小记

    虽然说原理很好理解,但是代码理解了花费我一个下午的时间,脑阔痛 该注释的地方都标记了,希望以后看到这些代码我还能好好理解吧 学习的链接地址:https://www.cnblogs.com/teble/ ...

  4. 2018.07.30 cogs2632. [HZOI 2016] 数列操作d(线段树)

    传送门 线段树基本操作 区间加等差数列,维护区间和. 对于每个区间维护等差数列首项和公差,易证这两个东西都是可合并的,然后使用小学奥数的知识就可以切掉这题. 代码: #include<bits/ ...

  5. 2018.07.30 bzoj4355: Play with sequence(线段树)

    传送门 维护区间覆盖成非负数,区间变成max(xi+a,0)" role="presentation" style="position: relative;&q ...

  6. python 递归深度优先搜索与广度优先搜索算法模拟实现

    一.递归原理小案例分析 (1)# 概述 递归:即一个函数调用了自身,即实现了递归 凡是循环能做到的事,递归一般都能做到! (2)# 写递归的过程 1.写出临界条件2.找出这一次和上一次关系3.假设当前 ...

  7. Day1 BFS算法的学习和训练

    ​ 因为自己的原因,之前没有坚持做算法的相应学习,总是觉得太难就半途而废,真的算是一个遗憾了,所以现在开始,定一个30天入门学习算法计划. ​ 我是根据<算法图解>的顺序进行安排的,自己对 ...

  8. python 递归,深度优先搜索与广度优先搜索算法模拟实现

    一.递归原理小案例分析 (1)# 概述 递归:即一个函数调用了自身,即实现了递归 凡是循环能做到的事,递归一般都能做到! (2)# 写递归的过程 1.写出临界条件 2.找出这一次和上一次关系 3.假设 ...

  9. 广度优先搜索 BFS算法

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

随机推荐

  1. 基于【 MySql 】二 || mysql详细学习笔记

    mysql重点学习笔记 /* Windows服务 */ -- 启动MySQL net start mysql -- 创建Windows服务 sc create mysql binPath= mysql ...

  2. Python中函数的知识点

    1.函数的定义与调用 2.函数的参数 3.函数的返回值   1. 1.1.函数的定义: 函数是组织好的,可重复使用的,用来实现单一,或相关联功能的代码段. 1.2.定义一个函数: 规则: 函数代码块以 ...

  3. Linux软件包(源码包和二进制包)及其区别和特点

    Linux 下的软件包众多,而且几乎都是经 GPL 授权的,也就是说这些软件都免费,振奋人心吧?而且更棒的是,这些软件几乎都提供源代码(开源的),只要你愿意,就可以修改程序源代码,以符合个人的需求和习 ...

  4. Computer Vision_33_SIFT:An efficient SIFT-based mode-seeking algorithm for sub-pixel registration of remotely sensed images——2015

    此部分是计算机视觉部分,主要侧重在底层特征提取,视频分析,跟踪,目标检测和识别方面等方面.对于自己不太熟悉的领域比如摄像机标定和立体视觉,仅仅列出上google上引用次数比较多的文献.有一些刚刚出版的 ...

  5. 搭建jumpser堡垒机

    安装环境centos7 生产环境建议使用 1.4.8 版本 1.开始安装 echo -e "\033[31m 1. 防火墙 Selinux 设置 \033[0m" \ && ...

  6. git命令——git status、git diff

    前言 当对项目做了更改时,我们通常需要知道具体改了哪些文件,哪些文件更改了没有暂存,哪些文件改了并且已加入到暂存区等待下次commit.上述任务使用git status都可以帮我们解决.但是想要知道文 ...

  7. XSS挑战之旅平台通关练习

    1.第一关 比较简单,测试语句: <svg/onload=alert(1)> <script>confirm(1)</script> <script>p ...

  8. Java springboot 中上传图片文件的1Md的限制

    在配置文件中修改了 spring.servlet.multipart.max-file-size=3.40spring.servlet.multipart.max-request-size=3M 遇到 ...

  9. 如何在CentOS上搭建gitlab服务器

    步骤 1. 打开HTTP和SSH访问 1.1 安装 sudo yum install -y curl policycoreutils-python openssh-server 1.2 开启SSH 这 ...

  10. linux网络编程之socket编程(九)

    转眼又快到十一月份了,北京已经是完全进入冬天的节奏,外面冷风嗖嗖的,不过在夜深人静之时,学习永远成了我最快乐的时光,只有此时会觉得自己是如此踏实,虽说白天工作也是编一天程,但是此时的编程,是一种业余爱 ...