题目:求从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. HDU-6668-Game 百度之星第一场B

    在多个连续的区间段中,选出连续重复度最高的区间,这样连续选出多个重复度最高的不相交区间,然后从第一个区间的左边已经右边开始,连续贪心即可,答案取最小值 #include<iostream> ...

  2. 神经网络入门——7or 感知器

    OR 感知器 OR 感知器与 AND 感知器很类似,在下图中,OR 感知器与 AND 感知器有相同的分割线,只是 OR 感知器分割线下移了一段距离.对权重或者偏置做怎样的设置可以实现这个效果?用下面的 ...

  3. ngRoute

    ngRoute 模块中包含以下内容, 名称 所属 作用 ngView DIRECTIVE 提供不同路由模板插入的视图层 $routeProvider PROVIDER 提供路由配置 $route SE ...

  4. H3C ISDN网络构成

  5. input 的 pattern 验证表单

    pattern 用于定义验证输入正则表达式 pattern 属性适用于以下 <input> 类型:text, search, url, telephone, email 以及 passwo ...

  6. Codeforces Round #176 (Div. 1 + Div. 2)

    A. IQ Test 模拟. B. Pipeline 贪心. C. Lucky Permutation 每4个数构成一个循环. 当n为偶数时,n=4k有解:当n为奇数时,n=4k+1有解. D. Sh ...

  7. P1113 同颜色询问

    题目描述 现在有一个包含 \(n\) 个元素的数组,它的元素的编号从 \(1\) 到 \(n\) . 每一个元素都有一个初始的颜色 \(C_i\) 以及数值 \(W_i\) . 这个数组支持 \(4\ ...

  8. jQuery中动态创建、添加元素的方法总结

    <input type="button" value="创建元素" id="btn"> <div id="box ...

  9. [转载] linux、Solaris下xdmcp远程桌面服务

    原文链接 http://youlvconglin.blog.163.com/blog/static/52320420106243857254/ 使用图形界面远程登录linux和Solaris,首先要在 ...

  10. H3C IPv6地址构成