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),而 ...
随机推荐
- PHP内置服务器
PHP在安装的时候会内置了服务器的功能,我们在使用的过程中如果只是调试,可以选择启动PHP内置的服务器,下面是windows下PHP内置服务器的启动步骤: 1.将php的D:\phpStudy\php ...
- 打开phpstorm 的terminal 工具框的快捷键 alt+F12
打开phpstorm 的terminal 工具框的快捷键 alt+F12 Alt + #[0-9] 打开相应的工具窗口
- Python--day69--ORM正反向查找(外键)
ForeignKey操作 正向查找 对象查找(跨表) 语法: 对象.关联字段.字段 示例: book_obj = models.Book.objects.first() # 第一本书对象 prin ...
- 2018-8-10-win10-sdk-是否向下兼容
title author date CreateTime categories win10 sdk 是否向下兼容 lindexi 2018-08-10 19:16:53 +0800 2018-2-13 ...
- vue+element-ui 字体自适应不同屏幕
项目背景:屏幕自适应问题,当在不同分辨率的屏幕上显示页面时,页面的字体需要根据屏幕大小来自适应,想到使用rem作为字体的单位 vue-cli脚手架下的index.html中写入以下js脚本 <s ...
- 解决 el-autocomplete 不显示及没数据时闪一下的问题
项目中用到了elementUI中的远程搜索即 el-autocomplete 组件,估计首次使用的都会遇到一些小问题,只要你能认真看完并且耐心理解,保证能帮到你,效果图如下: 组件代码: <el ...
- 28款GitHub最流行的开源机器学习项目,推荐GitHub上10 个开源深度学习框架
20 个顶尖的 Python 机器学习开源项目 机器学习 2015-06-08 22:44:30 发布 您的评价: 0.0 收藏 1收藏 我们在Github上的贡献者和提交者之中检查了用Python语 ...
- 【33.28%】【BZOJ 1195】[HNOI2006]最短母串
Time Limit: 10 Sec Memory Limit: 32 MB Submit: 1208 Solved: 402 [Submit][Status][Discuss] Descript ...
- 学习Java第三周
这是学习java的第三周 上周想要学习的这周也完成了,研究和测试代码花费了大约2个小时,发现这些内容方法居多,想要熟练掌握就要好好记忆了 包装类 数字处理类 Object类 Java类包 内部类 下一 ...
- 异步加载css 和 谷歌浏览器各实用小工具介绍
异步加载css资源 加开页面首屏显示速度使我们前端一直在追求的目标,而css资源在这些优化中同样也是不可或缺的. 一个网站可能有一部分css资源是必须的,他需要在页面渲染完之前就被加载完,并和html ...