C++算法——BFS(图解)

简介

宽度优先搜索算法(又称广度优先搜索)是最简便的图的搜索算法之一,这一算法也是很多重要的图的算法的原型。其别名又叫BFS,属于一种盲目搜寻法,目的是系统地展开并检查图中的所有节点,以找寻结果。换句话说,它并不考虑结果的可能位置,彻底地搜索整张图,直到找到结果为止。

相信初次接触的人已经蒙圈了,那么我们先不要探究其代码,直接从这个算法的过程来观察它怎样运算

BFS常用于迷宫,即从一点到另一点的最短路径,所以这里以一个迷宫来说明:

这里有一个5*5的迷宫,1是墙,0是路,那么从左上角走道右下角最少需要多少步呢?
0 1 0 0 0
0 1 0 1 0
0 0 0 0 0
0 1 1 1 0
0 0 0 1 0
首先把终点设为3,走过的路设为2,可以得到这样的过程图:
在这里插入图片描述
从图中可以清楚地看到这个算法从(0,0)点开始,向不同方向走,到达指定条件就停止并返回结果

就是因为这个性质,先到达的那条路线一定是步数最少的,所以只要有一条路线走到了,那么它一定就是最短路线,直接返回,所以这个算法非常快(重点!重点!重点!)

代码

这就是上面那个问题的代码(带打印地图)

#include <iostream>
#include <cstdio>
#include <queue>
using namespace std;
int Map[5][5];  //定义地图大小
int dir[4][2]= {1,0,-1,0,0,-1,0,1};  //定义方向
int n,m,ans;
struct node
{
    int x,y,step;

} now,nextt;  //保存走步
int BFS(int x,int y)
{
    queue<node>q;
    int xx,yy,zz;
    Map[x][y]=2;  //走过初始点
    now.x=x;
    now.y=y;
    now.step=0;
    q.push(now);  //从当前点开始
    while(!q.empty())
    {
        now=q.front();
        q.pop();
        for(int i=0; i<4; i++)  //遍历四个方向
        {
            xx=now.x+dir[i][0];
            yy=now.y+dir[i][1];  //走一步
            if(xx>=0&&xx<5&&yy>=0&&yy<5&&Map[xx][yy]!=1&&Map[xx][yy]!=2)  //可以走
            {
                nextt.x=xx;
                nextt.y=yy;
                nextt.step=now.step+1;  //步数加一
                Map[now.x][now.y]=2;   //走过一个点
                if(Map[xx][yy]==3)  //到达终点
                    return nextt.step;
                q.push(nextt);

            }
            for(int i=0; i<5; i++){      //打印地图
                for(int j=0; j<5; j++)
                    cout << Map[i][j];
                cout << endl;
            }
            cout << endl;
        }
    }
    return -1;   //走不过去
}

int main()
{

    for(int i=0; i<5; i++)     //输入地图
        for(int j=0; j<5; j++)
            cin >> Map[i][j];
    Map[4][4]=3;  //定义终点
    ans=BFS(0,0);
    cout << ans<< endl;
    return 0;

}

以上就是一个简单的BFS模板

例题

1.首先来看一个迷宫问题的变种

HDU-1072
这个问题相当于添加了步数限制,只需要把上面改动一下过程

#include <iostream>
#include <cstdio>
#include <queue>
using namespace std;
const int maxn=200;
int Map[maxn][maxn];
int dir[4][2]= {1,0,-1,0,0,-1,0,1};
int n,m,ans;
struct node
{
    int x,y,step,z;

} now,nextt;
int BFS(int x,int y,int z)
{
    queue<node>q;
    int xx,yy,zz;
    Map[x][y]=0;
    now.x=x;
    now.y=y;
    now.step=0;
    now.z = z;
    q.push(now);
    while(!q.empty())
    {
        now=q.front();
        q.pop();
        for(int i=0; i<4; i++)
        {
            xx=now.x+dir[i][0];
            yy=now.y+dir[i][1];
            zz = now.z-1;
            if(xx>=0&&xx<n&&yy>=0&&yy<m&&Map[xx][yy]!=0 && (zz>1 || (Map[xx][yy]!=1 && zz==1)))
            {
                nextt.x=xx;
                nextt.y=yy;
                nextt.step=now.step+1;
                nextt.z = zz;
                if(Map[xx][yy]==4){
                    Map[xx][yy] = 0;
                    nextt.z = 6;
                }
                else if(Map[xx][yy]==3)
                    return nextt.step;
                q.push(nextt);

            }
        }
    }
    return -1;
}

int main()
{
    int sx,sy,o;
    cin >> o;
    while(o--)
    {
        cin >> n >> m;
        for(int i=0; i<n; i++)
        {
            for(int j=0; j<m; j++)
            {
                cin >> Map[i][j];
                if(Map[i][j]==2)
                {
                    sx=i;
                    sy=j;
                }
            }
        }
        ans=BFS(sx,sy,6);
        cout << ans<< endl;
    }
    return 0;

}

2.另一种变化

POJ - 1915
这个问题只需要在简单模板的基础上改一下走法就可以了

#include <iostream>
#include <cstdio>
#include <queue>
#include <cstring>
using namespace std;
const int maxn=301;
int Map[maxn][maxn];
int dir[8][2]= {-2,-1,-2,1,-1,2,1,2,2,1,2,-1,1,-2,-1,-2};
int l,sx,sy,lx,ly,o,ans;
struct node
{
    int x,y,step;

} now,nextt;
int BFS(int x,int y)
{
    queue<node>q;
    int xx,yy;
    Map[x][y]=1;
    now.x=x;
    now.y=y;
    now.step=0;
    q.push(now);
    while(!q.empty())
    {
        now=q.front();
        q.pop();
        for(int i=0; i<8; i++)  //注意八个方向
        {
            xx=now.x+dir[i][0];
            yy=now.y+dir[i][1];
            if(xx>=0&&xx<l&&yy>=0&&yy<l&&Map[xx][yy]!=1)
            {
                nextt.x=xx;
                nextt.y=yy;
                nextt.step=now.step+1;
                if(xx==lx && yy==ly)
                    return nextt.step;
                q.push(nextt);
                Map[xx][yy]=1;
            }
        }
    }
}

int main()
{
    cin >> o;
    while(o--)
    {
        cin >> l >> sx >> sy >> lx >> ly;
        memset(Map,0,sizeof(Map));
        if(sx==lx && sy==ly)
            ans = 0;
        else
            ans=BFS(sx,sy);
        printf("%d\n",ans);
    }
    return 0;

}

3.别的应用(连通块)

POJ - 1562
注意方向和BFS运用方法改变

#include <iostream>
#include <queue>
#include <cstdio>
using namespace std;
const int maxn=105;
char Map[maxn][maxn];
int n,m,cnt=0;
int dir[8][2]={{1,0},{1,1},{1,-1},{-1,0},{-1,1},{-1,-1},{0,1},{0,-1}};
struct node
{
    int x;
    int y;
}now,next;
void BFS(int x,int y)
{
    now.x=x;
    now.y=y;
    Map[x][y]='*';
    queue<node> q;
    q.push(now);
    while(!q.empty())
    {
        now=q.front();
        q.pop();
        for(int i=0;i<8;i++)
        {
            int xx=now.x+dir[i][0];
            int yy=now.y+dir[i][1];
            if(xx>=0&&xx<n&&yy>=0&&y<m&&Map[xx][yy]=='@')
            {
                Map[xx][yy]='*';
                next.x=xx;
                next.y=yy;
                q.push(next);
            }
        }
    }
}
int main()
{
    while(~scanf("%d%d",&n,&m))
    {
        cnt=0;
        if(n==0)///输入0 0 退出
            break;
        for(int i=0;i<n;i++)
        {
            scanf("%s",Map[i]);
        }
        for(int i=0;i<n;i++)
        {
            for(int j=0;j<m;j++)
            {
                if(Map[i][j]=='@')
                {
                    cnt++;
                    BFS(i,j);
                }
            }
        }
        printf("%d\n",cnt);
    }
    return 0;
}

以上举了三个较简单的例题,能完全明白就了解了BFS的基础应用,还需要学会BFS的更多变种以及怎样与别的算法结合

发布了30 篇原创文章 · 获赞 27 · 访问量 1万+

猜你喜欢

转载自blog.csdn.net/qq_44616044/article/details/100276073