题目:求从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. springmvc 返回json数据给前台jsp页面展示

    spring mvc返回json字符串的方式 方案一:使用@ResponseBody 注解返回响应体 直接将返回值序列化json            优点:不需要自己再处理 步骤一:在spring- ...

  2. ORACLE内部操作

    当执行查询时,ORACLE采用了内部的操作. 下表显示了几种重要的内部操作. ORACLE Clause 内部操作 ORDER BY SORT ORDER BY UNION UNION-ALL MIN ...

  3. landi pos机

    2015年3月:联迪商用获得2014-2015中国金融POS机市场年度成功企业奖: 2014年5月:联迪商用入选2013年福州市纳税百强企业: 2013年12月:联迪商用入选2013年度中国电子商务物 ...

  4. Python--day25--复习(单继承和多继承的总结)

  5. SpringData Jpa、Hibernate、Jpa 三者之间的关系

    JPA规范与ORM框架之间的关系是怎样的呢? JPA规范本质上就是一种ORM规范,注意不是ORM框架--因为JPA并未提供ORM实现,它只是制订了一些规范,提供了一些编程的API接口,但具体实现则由服 ...

  6. pycharm解析器找不到

    pycharm每个项目都需要设定解释器位置吧,把自己解释器位置添加到项目设置就好了 先装python,再装pycharm,不然找不到解析器的,解析器其实就是python.exe.

  7. react入门:todo应用

    <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...

  8. VisualStudio 扩展开发 获得输出窗口内容

    本文告诉大家如何拿到 VisualStudio 输出窗口的内容 在上一篇告诉大家如何开发添加菜单 点击的时候可以使用方法,如果需要拿到 VisualStudio 的输出窗口的内容,如想要开发一个插件, ...

  9. addEventListener() 方法,事件监听(去哪儿网用到过)

    addEventListener() 方法,事件监听 你可以使用 removeEventListener() 方法来移除事件的监听. 语法 element.addEventListener(event ...

  10. vue-router再学习

    vue路由: 1:动态路由配置 import Vue from 'vue' import Router from 'vue-router' import Index from '@/view/inde ...