搜索——bfs

本文主要内容:

  1. 基础复习
  2. 利用bfs进行拓扑排序
  3. 多源bfs

1 bfs基础

  • 主要功能:
  1. 求边权相等的图的单源最短路
  2. 层序遍历
  • 注意事项:
  1. 队列元素类型的确定
  2. 判断状态数组更新的位置是在弹出队头时还是在将新元素加入队列时(取决于队列是否允许存在重复元素)

模板题1:Acwing844. 走迷宫

#include<iostream>
#include<queue>

using namespace std;

const int N = 105;

typedef pair<int, int> PII;

int g[N][N], n, m;
int dist[N][N];

int bfs()
{
    queue<PII>q;
    q.push({1, 1});
    dist[1][1] = 0;
    int dx[] = {0, 1, 0, -1}, dy[] = {1, 0, -1, 0};
    
    while(q.size())
    {
        PII t = q.front();
        q.pop();
        int a = t.first, b = t.second;
        if(a == n && b == m)return dist[n][m];
        for(int i = 0; i < 4; i++)
        {
            int x = a + dx[i], y = b + dy[i];
            if(!x || x > n || !y || y > m || g[x][y] == 1 || dist[x][y])continue;
            dist[x][y] = dist[a][b] + 1;
            q.push({x, y});
        }
    }
}

int main()
{
    cin >> n >> m;
    for(int i = 1; i <= n; i++)
        for(int j = 1; j <= m; j++)
            cin >> g[i][j];
    cout << bfs();
    return 0;
}

模板题2:Acwing845. 八数码

#include<iostream>
#include<unordered_map>
#include<queue>

using namespace std;

string st;

int bfs()
{
    unordered_map<string, int>dist;
    queue<string>q;
    string ed = "12345678x";
    q.push(st);
    dist[st] = 0;
    int dx[] = {0, 1, 0, -1}, dy[] = {1, 0, -1, 0};
    while(q.size())
    {
        string s = q.front();
        q.pop();
        if(s == ed)return dist[s];
        int distance = dist[s];
        int k = s.find('x');
        int a = k / 3, b = k % 3;
        for(int i = 0; i < 4; i++)
        {
            int x = a + dx[i], y = b + dy[i];
            if(x < 0 || x >= 3 || y < 0 || y >= 3)continue;
            swap(s[k], s[x * 3 + y]);
            if(!dist[s])
            {
                dist[s] = distance + 1;
                q.push(s);
            }
            swap(s[k], s[x * 3 + y]);
        }
    }
    return -1;
}
int main()
{
    for(int i = 0; i < 9; i++)
    {
        char c;
        cin >> c;
        st += c;
    }
    cout << bfs();
    return 0;
}

AcWing 847. 图中点的层次

#include<iostream>
#include<queue>
#include<cstring>

using namespace std;

const int N = 100005;

int idx, e[N], ne[N], h[N];
int n, m;
int dist[N];
queue<int> q;
bool st[N];

void add(int a, int b)
{
    e[idx] = b;
    ne[idx] = h[a];
    h[a] = idx++;
}

int bfs()
{
    q.push(1);
    dist[1] = 0;
    
    while(q.size())
    {
        auto t = q.front();
        q.pop();
        int distance = dist[t];
        for(int i = h[t]; i != -1; i = ne[i])
        {
            int j = e[i];
            if(!st[j])
            {
                dist[j] = distance + 1;
                q.push(j);
                st[j] = true;
            }
        }
    }
    return dist[n];
    
}
int main()
{
    memset(h, -1, sizeof h);
    memset(dist, -1, sizeof dist);
    cin >> n >> m;
    while(m--)
    {
        int a, b;
        cin >> a >> b;
        add(a, b);
    }
    cout << bfs() << endl;
    return 0;
}




2 利用bfs进行拓扑排序

例题:Acwing848. 有向图的拓扑序列

  • 思路:
  1. 用一个数组cnt[]存储所有节点的入度,在输入边时边的右端点入度加1
  2. 遍历所有节点,将入度为0的节点放入队列
  3. 每次弹出一个节点存入,遍历该节点所指向的所有相邻节点,所以相邻节点的入度减1
#include<iostream>
#include<cstring>
#include<queue>

using namespace std;

const int N = 100005;

int idx, e[N], ne[N], h[N];
int n, m;
int cnt[N], res[N], c;
queue<int>q;

void add(int a, int b)
{
    e[idx] = b;
    ne[idx] = h[a];
    h[a] = idx++;
}
bool bfs()
{
    for(int i = 1; i <= n; i++)
    if(!cnt[i])q.push(i);
    while(q.size())
    {
        int t = q.front();
        q.pop();
        res[c++] = t;
        for(int i = h[t]; i != -1; i = ne[i])
        {
            int j = e[i];
            cnt[j] -= 1;
            if(!cnt[j])q.push(j);
        }
    }
    return c == n;
}
int main()
{
    memset(h, -1, sizeof h);
    cin >> n >> m;
    while(m--)
    {
        int a, b;
        cin >> a >> b;
        add(a, b);
        cnt[b] ++;
    }
    if(bfs())
    {
        for(int i = 0; i < c; i++)
            cout << res[i] << ' ';
    }
    else cout << "-1" << endl;;
    return 0;
}




3 多源bfs

例题:Acwing173. 矩阵距离

  • 思路:
  1. 最短路的起点有多个
  2. 将所有起点放入队列,然后bfs
#include<iostream>
#include<queue>
#include<cstring>

using namespace std;

typedef pair<int, int> PII;

const int N = 1010;

int dist[N][N], n, m;
char g[N][N];

void bfs()
{
    memset(dist, 0x3f, sizeof dist);
    queue<PII>q;
    int dx[4] = {0, 1, 0, -1}, dy[4] = {1, 0, -1, 0};
    for(int i = 0; i < n; i++)
        for(int j = 0; j < m; j++)
            if(g[i][j] == '1')q.push({i, j});
    while(q.size())
    {
        PII t = q.front();
        q.pop();
        int x = t.first, y = t.second;
        //cout << x << ' ' << y << endl;
        if(g[x][y] == '1')dist[x][y] = 0;
        for(int i = 0; i < 4; i++)
        {
            int a = x + dx[i], b = y + dy[i];
            if(a < 0 || a > n || b < 0 || b > n)continue;
            if(g[a][b] == '1' || dist[a][b] <= dist[x][y] + 1)continue;
            dist[a][b] = dist[x][y] + 1;
            q.push({a, b});
        }
    }
}
int main()
{
    cin >> n >> m;
    for(int i = 0; i < n; i++)cin >> g[i];
    bfs();
    for(int i = 0; i < n; i++)
    {
        for(int j = 0; j < m; j++)
            cout << dist[i][j] << ' ';
        cout << endl;
    }
    return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值