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

#S######.#
......#..#
.#.##.##.#
.#........
##.##.####
....#....#
.#######.#
....#.....
.####.###.
....#...G#

此代码BFS遍历顺序:

宽度优先搜索关键点:

  1. 所用容器:队列
  2. 更新:取队列的队首
  3. BFS终止条件:队列不为空
  4. 入队条件:判断边界和障碍。  注:条件满足后可追加处理代码

下面贴上代码:

 #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小记的更多相关文章

  1. 【2018.07.30】(广度优先搜索算法/队列)学习BFS算法小记

    一些BFS参考的博客: https://blog.csdn.net/ldx19980108/article/details/78641127 https://blog.csdn.net/u011437 ...

  2. AtCoder Grand Contest 1~10 做题小记

    原文链接https://www.cnblogs.com/zhouzhendong/p/AtCoder-Grand-Contest-from-1-to-10.html 考虑到博客内容较多,编辑不方便的情 ...

  3. [原]Paste.deploy 与 WSGI, keystone 小记

    Paste.deploy 与 WSGI, keystone 小记 名词解释: Paste.deploy 是一个WSGI工具包,用于更方便的管理WSGI应用, 可以通过配置文件,将WSGI应用加载起来. ...

  4. 图的遍历(搜索)算法(深度优先算法DFS和广度优先算法BFS)

    图的遍历的定义: 从图的某个顶点出发访问遍图中所有顶点,且每个顶点仅被访问一次.(连通图与非连通图) 深度优先遍历(DFS): 1.访问指定的起始顶点: 2.若当前访问的顶点的邻接顶点有未被访问的,则 ...

  5. 【BZOJ-1656】The Grove 树木 BFS + 射线法

    1656: [Usaco2006 Jan] The Grove 树木 Time Limit: 5 Sec  Memory Limit: 64 MBSubmit: 186  Solved: 118[Su ...

  6. POJ 3278 Catch That Cow(bfs)

    传送门 Catch That Cow Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 80273   Accepted: 25 ...

  7. POJ 2251 Dungeon Master(3D迷宫 bfs)

    传送门 Dungeon Master Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 28416   Accepted: 11 ...

  8. MySql 小记

    MySql  简单 小记 以备查看 1.sql概述 1.什么是sql? 2.sql发展过程? 3.sql标准与方言的关系? 4.常用数据库? 5.MySql数据库安装? 2.关键概念 表结构----- ...

  9. Sicily 1215: 脱离地牢(BFS)

    这道题按照题意直接BFS即可,主要要注意题意中的相遇是指两种情况:一种是同时到达同一格子,另一种是在移动时相遇,如Paris在(1,2),而Helen在(1,2),若下一步Paris到达(1,1),而 ...

随机推荐

  1. 系统学习前端之FormData详解

    FormData 1. 概述 FormData类型其实是在XMLHttpRequest 2级定义的,它是为序列化表以及创建与表单格式相同的数据(当然是用于XHR传输)提供便利. 2. 构造函数 创建一 ...

  2. PyCharm自定义代码块设置方法-添加-删除【详细步骤】

    原文:https://blog.csdn.net/chichu261/article/details/82887108 在做项目的时候,有些代码会需要频繁的码.如果去已有的项目中去复制,又需要找很久. ...

  3. 纯JS前端分页方法(JS分页)

    1.JS分页函数:开发过程中,分页功能一般是后台提供接口,前端只要传page(当前页码)和pageSize(每页最大显示条数)及对应的其他查询条件,就可以返回所需分页显示的数据. 但是有时也需要前端本 ...

  4. Koa2 遇到Method Not Allowed 获取不到返回值

    https://q.cnblogs.com/q/114462/          都来找我  Haisen‘s blogs 求求各位大神了,2点多了没解决睡不着啊,我按照网上用的koa2-cors,g ...

  5. Python--day61--Django项目配置相关

    static文件查找:

  6. 怎样在RxJS Observable中使用Async-Await

    怎样在RxJS Observable中使用Async-Await 一般情况下 async-await 和 Observables 并不能“在一起使用”.但RxJS 从一开始就具备与 Promises ...

  7. C# 从零开始写 SharpDx 应用 绘制基础图形

    本文告诉大家通过 SharpDx 画出简单的 2D 界面 本文属于 SharpDx 系列 博客,建议从头开始读 本文分为两步,第一步是初始化,第二步才是画界面 初始化 先创建 RenderForm 用 ...

  8. GitHub上传项目到远程库

    写文章 GitHub上传项目到远程库     GitHub上传项目到远程库 今天把想把文件托管到GitHub仓库,但是执行一系列的命令以后,刷新GitHub网站还是没有任何更新.后来终于找到原因,原来 ...

  9. Windows Server Core Remote Manage Hyper-V

    原帖:https://serverfault.com/questions/852144/how-do-i-remotely-manage-hyper-v-2016-standalone-via-win ...

  10. 创意app1

      app名称: 与我相似的人 app目的: 旨在通过云匹配,搜索到与自己类似爱好或者性格的人用户相似的内容:衣服品牌鞋子手机笔记本键盘鼠标相机刮胡刀自行车工作  说明: 现有的格局 百度贴吧是面向多 ...