BFS经典题集(提高)

矩阵距离

/*
本题是一道多源BFS求最短路问题,解决多源BFS问题可以虚拟出一个不存在的点,使得这个点到各起点的距离都为0,这样就把多源BFS问题转化为了普通的BFS问题。

本题的大致意思是:找出来每一个是0的点到所有1的点的最短的距离。

转化过程:我们虚拟出来一个不存在的点,使得这个点到所有1的距离都是0,然后所有为1的点就可以放进队列中,并且路程都是0,就可以用普通的BFS求最短路的方法进行求解了
*/

#include <bits/stdc++.h>

#define x first
#define y second

using namespace std;

typedef pair<int,int> pii;

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

const int N = 1010;

char g[N][N];
int d[N][N];
bool st[N][N];
int n, m;

void bfs()
{
    queue<pii> q;
    for(int i = 0; i < n; i++)
    {
        for(int j = 0; j < m; j++)
        {
            if(g[i][j] == '1')
            {
                q.push({i,j});
                st[i][j] = true;
            }
        }
    }
    
    while(q.size())
    {
        pii t = q.front();
        q.pop();
        
        for(int k = 0; k < 4; k++)
        {
            int nx = t.x + dx[k], ny = t.y + dy[k];
            
            if(nx >= 0 && nx < n && ny >= 0 && ny < m)
            {
                if(!st[nx][ny])
                {
                    d[nx][ny] = d[t.x][t.y] + 1;
                    q.push({nx,ny});
                    st[nx][ny] = true;
                }
            }
        }
    }
}

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 << d[i][j] << ' ';
        }
        cout << endl;
    }
    
    return 0;
}

魔板

/*
当目标状态和初始状态相同的时候,需要特判一下
*/

#include <bits/stdc++.h>
using namespace std;

char target[10];
string basic = "12345678";

unordered_map<string,int> d;
unordered_map<string,bool> st;
unordered_map<string,string> path;

string move_A(string s)
{
    string ts = "";
    for(int i = 7; i >= 0; i --) ts += s[i];
    return ts;
}

string move_B(string s)
{
    string ts = "";
    
    ts += s[3];
    
    for(int i = 0; i < 3; i++) ts += s[i];
    
    for(int i = 5; i < 8; i++) ts += s[i];
    
    ts += s[4];
    
    return ts;
}

string move_C(string s)
{
    string ts = "";
    ts = ts + s[0] + s[6] + s[1] + s[3] + s[4] + s[2] + s[5] + s[7];
    return ts;
}

int bfs()
{
    queue<string> q;
    q.push(basic);
    st[basic] = true;
    d[basic] = 0;
    path[basic] = "";
    
    while(q.size())
    {
        string t = q.front();
        q.pop();
        
        if(t == target) return d[t];
        
        //A变换
        string as = move_A(t);
        if(!st[as])
        {
            st[as] = true;
            d[as] = d[t] + 1;
            path[as] = path[t] + 'A';
            q.push(as);
        }
        
        //B变换
        string bs = move_B(t);
        if(!st[bs])
        {
            st[bs] = true;
            d[bs] = d[t] + 1;
            path[bs] = path[t] + 'B';
            q.push(bs);
        }
        
        //C变换
        string cs = move_C(t);
        if(!st[cs])
        {
            st[cs] = true;
            d[cs] = d[t] + 1;
            path[cs] = path[t] + 'C';
            q.push(cs);
        }
    }
    return -1;
}

int main()
{
    for(int i = 0; i < 8; i++) cin >> target[i];
    
    if(target == basic)
    {
        cout << 0 << endl;
        return 0;
    }
    
    cout << bfs() << endl;
    
    cout << path[target] << endl;
    return 0;
}

电路维修

/*
本题使用了双端队列进行解决
*/

#include <bits/stdc++.h>

#define x first
#define y second

using namespace std;

typedef pair<int,int> pii;

const int dx[4] = {-1,1,1,-1};
const int dy[4] = {-1,-1,1,1};
const int ix[4] = {-1,0,0,-1};
const int iy[4] = {-1,-1,0,0};
const char match[4] = {'\\','/','\\','/'};

const int N = 510;

int T, n, m;
char g[N][N];
int d[N][N];

void bfs()
{
    memset(d,0x3f,sizeof d);
    
    deque<pii> q;
    q.push_back({0,0});
    d[0][0] = 0;
    
    while(q.size())
    {
        pii t = q.front();
        q.pop_front();
        
        for(int k = 0; k < 4; k++)
        {
            int nx = t.x + dx[k], ny = t.y + dy[k];
            
            if(nx >= 0 && nx <= n && ny >= 0 && ny <= m)
            {
                if(g[t.x + ix[k]][t.y + iy[k]] == match[k])
                {
                    if(d[nx][ny] > d[t.x][t.y])
                    {
                        d[nx][ny] = d[t.x][t.y];
                    	q.push_front({nx,ny});
                    } 
                }
                else if(d[nx][ny] > d[t.x][t.y] + 1)
                {
                    d[nx][ny] = d[t.x][t.y] + 1;
                    q.push_back({nx,ny});
                }
            }
        }
    }
}

int main()
{
    cin >> T;
    
    while(T -- )
    {
        cin >> n >> m;
        for(int i = 0; i < n; i++) cin >> g[i];
        
        bfs();
        
        if(d[n][m] == 0x3f3f3f3f) puts("NO SOLUTION");
        
        else cout << d[n][m] << endl;
    }
    
    return 0;
}
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值