宽度优先搜索(BFS)基础版


特点
1.BFS可以求最小,第一次搜到就最小
2.BFS是一个基于迭代的算法,所以他有一个很重要的特点就是,他不会爆栈,这个主要是针对DFS说的,也就是当一个问题的搜索层数可能很深的时候,但是节点个数不深的时候, 如过DFS和BFS都可以的话,我们优先选择BFS

Flood Fill(洪水覆盖算法)

核心思想

核心思想:从一个起点开始,然后每一次,我们随机选择一个新加进来的格子,然后看一下它周围,能不能扩展新的格子,如果周围能扩展新格子的话,就把他扩展进来,直到整个算法结束(不能扩展新格子为止),这里面需要判重,一个格子只能覆盖一次

实现效果

可以在线性时间复杂度内,找到某个点所在的连通块

这个算法,深搜宽搜都能实现,一般来说是用的宽搜,因为深搜容易爆栈,

例题

池塘计数
做法是什么呢,就是遍历这个图,一行一行扫描,然后发现水,如果还没有被覆盖过,就开始坐flood fill

#include<bits/stdc++.h>
using namespace std;
#define PII pair<int, int>
#define x first
#define y second
#define endl '\n'
const int N = 1010;
bool st[N][N];
char mp[N][N];
int n, m;

void bfs(int x, int y)
{
    queue<PII> q;
    q.push({x, y});
    while(q.size())
    {
        auto t = q.front();
        q.pop();
        
        for(int i=t.x-1; i<=t.x+1; i++)
        {
            for(int j=t.y-1; j<=t.y+1; j++)
            {
                if(i == t.x && j == t.y) continue;
                if(i < 1 || i > n || j > m || j < 1) continue;
                if(mp[i][j] == '.' || st[i][j]) continue;
                
                st[i][j] = 1;
                q.push({i, j});
            }
        }
    }
}


int main()
{
    cin >> n >> m;
    for (int i = 1; i <= n; i ++ )
    {
        for(int j=1; j<=m; j++)
        {
            scanf(" %c", &mp[i][j]);
        }
    }
    
    // cout << mp[1][1] << endl;
    int cnt = 0;
    for (int i = 1; i <= n; i ++ )
    {
        for(int j=1; j<=m; j++)
        {
            if(mp[i][j] == 'W' && !st[i][j]) 
            {
                bfs(i, j);
                // cout << i << ' ' << j << endl;
                cnt ++;
            }
        }
    }
    
    printf("%d\n", cnt);
    return 0;
}

城堡问题

这个问题的坑点好多,首先就是输入的坑点,这个输入并不好处理,我们只能这样想,就是我们把给定的数,看成二进制,也就是二进制的四个位,然后就按照每个墙的权值来分为0墙,1墙,2墙,3墙如图
请添加图片描述
然后根据编号来写dx,dy数组,然后每次判断就是g[i][j] >> k & 1来判断有没有墙

输入的坑就这样过去了,然后还要注意st数组的标记,一定是加入队列之后就标记上,否则,当数组内元素过多的时候,可能会反复加入队列中同一个数,错误写法就是注释

#include<bits/stdc++.h>
using namespace std;
#define endl '\n'
const int N = 55;
int g[N][N];
bool st[N][N];
#define x first
#define y second
#define PII pair<int, int>

int dx[4] = {0, -1, 0, 1}, dy[4] = {-1, 0, 1, 0};
int n, m;

int bfs(int x, int y)
{
    queue<PII> q;
    q.push({x, y});
    int res = 0;
    st[x][y] = true;
    
    while(q.size())
    {
        res++;
        auto t = q.front();
        // st[t.x][t.y] = 1;错误写法
        q.pop();
        
        for(int i=0; i<=3; i++)
        {
            int xx = t.x + dx[i], yy = t.y + dy[i];
            if(st[xx][yy]) continue;
            if(xx < 1 || xx > n || yy > m || yy < 1 || g[t.x][t.y] >> i & 1) continue;
            
            q.push({xx, yy});
            // cout << xx <<" " << yy << endl;
            st[xx][yy] = 1;
        }
        
    }
    
    // cout << res << endl;
    return res;
}


int main()
{
    
    cin >> n >> m;
    for (int i = 1; i <= n; i ++ )
    {
        for (int j = 1; j <= m; j ++ )
        {
            cin >> g[i][j];
        }
    }
    
    int area = 0, cnt = 0;
    for (int i = 1; i <= n; i ++ )
    {
        for (int j = 1; j <= m; j ++ )
        {
            if(!st[i][j])
            {
                area =max(area, bfs(i, j));
                cnt ++;
            }
        }
    }
    
    cout << cnt << endl;
    cout << area << endl;
    return 0;
}

山峰和山谷

这里要注意,当附近有比他大的,也有比它小的,就既不是山峰也不是山谷,所以判断的时候要判断对面,就是,如果周围比我高,就让山峰 = 1,bfs后判断山峰的bool值是不是0,是0就说明周围没有比它高的,同理,如果周围有比较低的,就让山谷的bool值为1,,最后bfs完看一看有没有周围比较低的

#include<bits/stdc++.h>
using namespace std;
#define endl '\n'
#define x first
#define y second
#define PII pair<int, int>
const int N = 1010;
int g[N][N];
int n;
bool high, low, st[N][N];

int dx[8] = {-1, -1, -1, 0, 1, 1, 1, 0};
int dy[8] = {-1, 0, 1, 1, 1, 0, -1, -1};

void bfs(int x, int y)
{
    queue<PII> q;
    q.push({x, y});
    st[x][y] = 1;
    while(q.size())
    {
        auto t = q.front();
        q.pop();
        
        for(int i=t.x-1; i <= t.x+1; i++)
        {
            for(int j=t.y-1; j <= t.y + 1; j++)
            {
                if(i < 1 || j < 1 || j > n || i > n) continue;
                
                if(g[i][j] != g[x][y])
                {
                    if(g[i][j] > g[x][y]) high = 1;
                    else low = 1;
                }
                else
                {
                    if(!st[i][j])
                    {
                        q.push({i, j});
                        st[i][j] = 1;
                    }
                }
            }
        }
        
    }
}

int main()
{
    scanf("%d", &n);
    for(int i=1; i<=n; i++)
    {
        for(int j=1; j<=n; j++)
        {
            scanf("%d", &g[i][j]);
        }
    }
    
    int feng = 0, gu = 0;
    for (int i = 1; i <= n; i ++ )
    {
        for (int j = 1; j <= n; j ++ )
        {
            if(!st[i][j])
            {
                high = 0, low = 0;
                bfs(i, j);
                
                if(!high) feng ++;
                if(!low) gu++;
            }
        }
    }
    
    cout << feng << ' ' << gu <<endl;
    return 0;
}

最小步数 / 最短路径

bfs相对于dfs有一个比较好的性质就是,当权值相同的时候,bfs第一次搜到的就是单源最短路

如何记录路径呢,就是,将st数组从bool 变成 PII 这个就每次都记录这个点是从哪个点转移过来的,如何倒序输出就可以啦

宽搜本来就具有最短路的性质,所以不需要额外存储距离,第一次搜到一定是最短的
迷宫问题

这个题目用到了一个小技巧,就是如果从起点搜到终点,记录下来还需要倒序输出,但是如果我们从终点往起点搜索的话,就直接从起点开始往后输出就可以了

bfs求最短路径可以看成是一个特殊的dij算法,用优先队列优化的时候,如果每个权值都是1的话,我们就是按照层的顺序来遍历所有的点,因此在队列里面,所有点到起点的距离就是一个严格递增的,因此队首就一定是最小值,所以说,如果说所有的边权都是1的话,那么我们这个队列相当于dij里面的优先队列,队头其实就是一个最小值,就是当前最小值,证明方法与dij类似

证两个方面 1.队列是递增的,假设我们当前队列里面所有的点到起点的距离是递增的,此时起点一定是最小值,起点扩展出来的点一定是应该加到最后

从图上看一下就是一层一层扩展的,就是把起点放进来,然后距离起点距离为1的点,那一层,然后距离为2的点,为3的点,因为是按照层来扩展的,所以当前没有加到队列里面的点,一定大于等于队列最后一个元素,当前这个点又是队列里面的最小值,因此当前这个点就是最小值, 如果当前这个点的距离可以被更新,必然被后面的点更新,更新的点一定要小于当前点,但是后边点都大于我这个点,而且边权是正的,所以不可能。

#include<bits/stdc++.h>
using namespace std;
#define x first
#define y second
#define endl '\n'
#define PII pair<int, int>
const int N = 1010;
int mp[N][N];
PII pre[N][N];
int n;
int dx[4] = {-1, 0, 1, 0}, dy[4] = {0, 1, 0, -1};


void bfs(int x, int y)
{
    queue<PII> q;
    q.push({x, y});
    memset(pre, -1, sizeof pre);
    pre[x][y] = {0, 0};//只要标记一下被遍历过了就可以,是多少都没有问题,不是-1就行
    
    while(q.size())
    {
        auto t = q.front();
        q.pop();
        
        for(int i=0; i<4; i++)
        {
            int xx = t.x + dx[i], yy = t.y + dy[i];
            
            if(mp[xx][yy] || xx < 0 || xx >= n || yy < 0 || yy >= n || pre[xx][yy].x != -1) continue;
            
            
            
            q.push({xx, yy});
            pre[xx][yy] = t;
            
            if(xx == 0 && yy == 0) return;
        }
    }
    
    
}
int main()
{
    scanf("%d", &n);
    for(int i=0; i<n; i++)
    {
        for (int j = 0; j < n; j ++ )
        {
            scanf("%d", &mp[i][j]);
        }
    }
    
    
    
    bfs(n-1, n-1);
    
    PII end(0, 0);
    
    while(1)
    {
        printf("%d %d\n", end.x, end.y);
        if(end.x == n-1 && end.y == n-1) break;
        end = pre[end.x][end.y];
    }
    return 0;
}

武士风度的牛

一个经典的最短路模型,只不过方向有些变化,只能走日字

#include<bits/stdc++.h>
using namespace std;
#define endl '\n'
#define x first
#define y second
const int N = 200;

char mp[N][N];
int dist[N][N];
#define PII pair<int, int>
int n, m;
int dx[8] = {-2, -1, 1, 2, 2, 1, -1, -2};
int dy[8] = {1, 2, 2, 1, -1, -2, -2, -1};

void bfs(int x, int y)
{
    memset(dist, -1, sizeof dist);
    queue<PII> q;
    q.push({x, y});
    dist[x][y] = 0;
    
    while(q.size())
    {
        PII t = q.front();
        q.pop();
        
        for(int i=0; i<8; i++)
        {
            int xx = t.x + dx[i], yy = t.y + dy[i];
            
            if(dist[xx][yy] != -1 || xx < 1 || xx > n || yy < 1 || yy > m || mp[xx][yy] == '*') continue;
            
            dist[xx][yy] = dist[t.x][t.y] + 1;
            if(mp[xx][yy] == 'H') 
            {
                printf("%d\n", dist[xx][yy]);
                return;
            }
            
            q.push({xx, yy});
        }
    }
}
int main()
{
    scanf("%d%d", &m, &n);
    int x1, y1;
    for (int i = 1; i <= n; i ++ )
    {
        for(int j=1; j<=m; j++)
        {
            scanf(" %c", &mp[i][j]);
            
            if(mp[i][j] == 'K')
            {
                x1 = i, y1 = j;
            }
        }
    }
    
    bfs(x1, y1);
    
    return 0;
}

抓住那头牛

这个题目的空间可以不用开2倍的空间,因为我们可以画一下,当K等于1e5的时候,这时候n = 2 3 \frac{2}{3} 32 * 1e5的时候,扩大两倍,看起来近似,但是如果我们先让n–,一直到n = 1 2 \frac{1}{2} 21 * 1e5的时候,似乎更好,当然如果k是奇数的话,也可以先减减再翻倍然后加一,或者先翻倍再减减,所以,就开大10左右应该就够了

#include<bits/stdc++.h>
using namespace std;
#define endl '\n'
const int N = 1e5 + 10;

int n, k;
int dist[N];

void bfs(int x)
{
    memset(dist, -1, sizeof dist);
    dist[x] = 0;
    queue<int> q;
    q.push(x);
    
    while(q.size())
    {
        int t = q.front();
        q.pop();
        
        if(t - 1 >= 0 && dist[t - 1] == -1)
        {
            q.push(t - 1);
            dist[t - 1] = dist[t] + 1;
            if(t - 1 == k) 
            {
                printf("%d\n", dist[t - 1]);
                return;
            }
        }
        
        if(t + 1 <= k && dist[t + 1] == -1)
        {
            q.push(t + 1);
            dist[t + 1] = dist[t] + 1;
            if(t + 1 == k) 
            {
                printf("%d\n", dist[t + 1]);
                return;
            }
        }
        
        if(t * 2 < k + 10 && dist[t * 2] == -1)
        {
            q.push(t * 2);
            dist[t * 2] = dist[t] + 1;
            if(t * 2 == k) 
            {
                printf("%d\n", dist[t * 2]);
                return;
            }
        }
    }
}


int main()
{
    scanf("%d%d", &n, &k);
    if(n == k) puts("0");
    else bfs(n);
    return 0;
}
  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值