BFS小记
题目:求从S走到G点所需步数

|
#S######.# |
此代码BFS遍历顺序:

宽度优先搜索关键点:
- 所用容器:队列
- 更新:取队列的队首
- BFS终止条件:队列不为空
- 入队条件:判断边界和障碍。 注:条件满足后可追加处理代码
下面贴上代码:
#include <iostream>
#include <fstream>
#include <queue>
#include <iomanip> using namespace std;
typedef pair<int, int> P;
const int INF = ;
int M;
int N;
int d[][] = { };
char maze[][] = {};
int dx[] = { , , -, };
int dy[] = { , , , - };
int sx, sy;
int gx, gy;
int BFS()
{
queue<P> que;
for (int i = ; i < N; i++)
{
for (int j = ; j < M; j++)
{
d[i][j] = INF;
}
}
que.push(P(sx, sy));
d[sx][sy] = ;
while (que.size())
{
P p = que.front();
que.pop();
if (p.first == gx && p.second == gy)break; for (int i = ; i < ; i++)
{
int nx = p.first + dx[i], ny = p.second + dy[i];
if ( maze[nx][ny] == 'G' || <= nx && nx < N && <= ny && ny < M && maze[nx][ny] != '#' && maze[nx][ny] == '.' && d[nx][ny] == INF )
{
que.push(P(nx, ny));
d[nx][ny] = d[p.first][p.second] + ;
}
}
}
return d[gx][gy]; }
void solve( )
{
int res = BFS();
cout << endl << res << endl;
}
int main()
{
ifstream filein("data.txt");
int i = ;
while (!filein.eof())
{
filein.getline(maze[i], );
cout << maze[i++] << endl;
}
int m = , n = ;
while (maze[][n])n++;
while (maze[m][])
{
for (int i = ; i < n; i++)
{
if (maze[m][i] == 'S')
{
sx = m;
sy = i;
}
else if (maze[m][i] == 'G')
{
gx = m;
gy = i;
}
}
m++;
}
cout << "S:" << sx << " " << sy << " " << endl;
cout << "G:" << gx << " " << gy << " " << endl;
cout << "m:" << m << endl;
cout << "n:" << n << endl;
M = m;
N = n;
solve();
return ;
}
运行结果:

这是典型的BFS迷宫问题,也是最基本的BFS,需要熟练掌握,但并不困难。
2019/2/13更新
lintcode 897
传送门:https://www.lintcode.com/problem/island-city/
class Solution {
public:
/**
* @param grid: an integer matrix
* @return: an integer
*/
class dot{
public:
int x;
int y;
dot(int xx,int yy){
this->x=xx;
this->y=yy;
}
};
bool bfs(vector<vector<int>> &grid,int sx,int sy,int m,int n){
int dx[]={,,,-};
int dy[]={,,-,};
bool res=false;
queue<dot> q;
q.push(dot(sx,sy));
while(q.size()!=){
dot d=q.front();
q.pop();
if(grid[d.x][d.y]==)res=true;
grid[d.x][d.y]=;
for(int i=;i<;i++){
int nx=d.x+dx[i];
int ny=d.y+dy[i];
if(nx>= && ny>= && nx<m && ny<n && grid[nx][ny]!= && grid[nx][ny]!=){
q.push(dot(nx,ny));
}
}
}
return res;
}
int numIslandCities(vector<vector<int>> &grid) {
// Write your code here
int island_count=;
int m=grid.size();
int n=grid[].size();
for(int i=;i<m;i++)
for(int j=;j<n;j++){
if(grid[i][j]==||grid[i][j]==){
continue;
}
if(bfs(grid,i,j,m,n))
{
island_count++;
}
}
return island_count;
}
};
BFS小记的更多相关文章
- 【2018.07.30】(广度优先搜索算法/队列)学习BFS算法小记
一些BFS参考的博客: https://blog.csdn.net/ldx19980108/article/details/78641127 https://blog.csdn.net/u011437 ...
- AtCoder Grand Contest 1~10 做题小记
原文链接https://www.cnblogs.com/zhouzhendong/p/AtCoder-Grand-Contest-from-1-to-10.html 考虑到博客内容较多,编辑不方便的情 ...
- [原]Paste.deploy 与 WSGI, keystone 小记
Paste.deploy 与 WSGI, keystone 小记 名词解释: Paste.deploy 是一个WSGI工具包,用于更方便的管理WSGI应用, 可以通过配置文件,将WSGI应用加载起来. ...
- 图的遍历(搜索)算法(深度优先算法DFS和广度优先算法BFS)
图的遍历的定义: 从图的某个顶点出发访问遍图中所有顶点,且每个顶点仅被访问一次.(连通图与非连通图) 深度优先遍历(DFS): 1.访问指定的起始顶点: 2.若当前访问的顶点的邻接顶点有未被访问的,则 ...
- 【BZOJ-1656】The Grove 树木 BFS + 射线法
1656: [Usaco2006 Jan] The Grove 树木 Time Limit: 5 Sec Memory Limit: 64 MBSubmit: 186 Solved: 118[Su ...
- POJ 3278 Catch That Cow(bfs)
传送门 Catch That Cow Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 80273 Accepted: 25 ...
- POJ 2251 Dungeon Master(3D迷宫 bfs)
传送门 Dungeon Master Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 28416 Accepted: 11 ...
- MySql 小记
MySql 简单 小记 以备查看 1.sql概述 1.什么是sql? 2.sql发展过程? 3.sql标准与方言的关系? 4.常用数据库? 5.MySql数据库安装? 2.关键概念 表结构----- ...
- Sicily 1215: 脱离地牢(BFS)
这道题按照题意直接BFS即可,主要要注意题意中的相遇是指两种情况:一种是同时到达同一格子,另一种是在移动时相遇,如Paris在(1,2),而Helen在(1,2),若下一步Paris到达(1,1),而 ...
随机推荐
- POJ2186 Popular Cows 题解 强连通分量入门题
题目链接:http://poj.org/problem?id=2186 题目大意: 每头牛都想成为牛群中的红人. 给定N头牛的牛群和M个有序对(A, B),(A, B)表示牛A认为牛B是红人: 该关系 ...
- phpstorm IDEA 双击Shift键会弹出 SearchEverywhere 对话框,如何取消这个功能
https://blog.csdn.net/qq_27598243/article/details/80526352 解决方法:一:Open lib/resources.jar/idea/Platfo ...
- JS遍历数组
for 如果用var会造成变量声明提前等问题for(var i = 1; i <= arr.length; i++){ console.log(arr[i - 1]);} for(let i = ...
- Android Animation动画详解(一): 补间动画
前言 你有没有被一些APP中惊艳的动画效果震撼过,有没有去思考,甚至研究过这些动画是如何实现的呢? 啥?你没有思考,更没有研究过? 好吧,那跟着我一起来学习下如何去实现APP中那些让我们惊羡的动画特效 ...
- 洛谷P4018 Roy&October之取石子 题解 博弈论
题目链接:https://www.luogu.org/problem/P4018 首先碰到这道题目还是没有思路,于是寻思还是枚举找一找规律. 然后写了一下代码: #include <bits/s ...
- Java反射机制(四):动态代理
一.静态代理 在开始去学习反射实现的动态代理前,我们先需要了解代理设计模式,那何为代理呢? 代理模式: 为其他对象提供一种代理,以控制对这个对象的访问. 先看一张代理模式的结构图: 简单的理解代理设计 ...
- kindeditor编辑器微软雅黑样式font-family值变成"
http://www.100cm.cn/article-126-764.html kindeditor编辑器中选中文字, 修改字体(字体名称中带有空格, 例如"Microsoft YaHei ...
- 深入理解String、StringBuffer、StringBuilder(转)
文章系转载,非原创,原地址: http://www.cnblogs.com/dolphin0520/p/3778589.html 相信String这个类是Java中使用得最频繁的类之一,并且又是各大公 ...
- H3C 公有地址和私有地址
- python模块之模块导入
模块的导入 """ 模块的导入使用:模块导入一般都要放在代码的最上面 不同模块的导入顺序: 1 内置模块 2 扩展模块 3 自定义模块 """ ...