[BFS][51nod]1649 齐头并进

本文介绍了一个使用图搜索算法解决旅行问题的例子。通过建立节点之间的连接并利用广度优先搜索(BFS),分别针对两种不同条件(存在直接路径与不存在直接路径)进行搜索,最终找到从起点到终点的最短步数。

A了 可是为什么呢??

矩阵存边

bfs搜索

//#pragma GCC optimize(2)
#include <cstdio>
#include <iostream>
#include <cstdlib>
#include <cmath>
#include <cctype>
#include <string>
#include <cstring>
#include <algorithm>
#include <stack>
#include <queue>
#include <set>
#include <map>
#include <ctime>
#include <vector>
#include <fstream>
#include <list>
#include <iomanip>
#include <numeric>
using namespace std;

typedef long long ll;

const int MAXN = 4e2 + 10;

int edge[MAXN][MAXN] ={0};
bool trainu[MAXN] = {0};
bool busu[MAXN] = {0};

int n, m;

struct bfsnode
{
    int now, step;
    bfsnode(int a, int b)
    {
        now = a, step = b;
    }
};
 
int bfstrain()
{
    int anstrain = 0;

    queue <bfsnode> Q;

    Q.push(bfsnode(1, 0));

    while(!Q.empty())
    {
        bfsnode t = Q.front();

        Q.pop();

        if(!trainu[t.now])
        {
            if(t.now == n)
            {
                return t.step;
            }

            trainu[t.now] = true;

            for(int j = 1; j <= n; j++)
            {
                if(edge[t.now][j])
                {
                    Q.push(bfsnode(j, t.step + 1));
                }

            }
        }
    }
    return -1;
}

int bfsbus()
{
    int ansbus = 0;

    queue <bfsnode> Q;

    Q.push(bfsnode(1, 0));

    while(!Q.empty())
    {
        bfsnode t = Q.front();

        Q.pop();

        if(!busu[t.now])
        {
            if(t.now == n)
            {
                return t.step;
            }

            busu[t.now] = true;

            for(int j = 1; j <= n; j++)
            {
                if(!edge[t.now][j])
                {
                    Q.push(bfsnode(j, t.step + 1));
                }

            }
        }
    }
    return -1;
}

int main()
{
    cin>>n>>m;

    int x, y;
    
    for(int i = 0; i < m; i++)
    {
        cin>>x>>y;
        edge[x][y] = 1;
        edge[y][x] = 1;
    }
    
    int tlen = bfstrain(), blen = bfsbus();

    if(tlen == -1 || blen == -1)
        cout<<"-1"<<endl;
    else
        cout<<max(tlen, blen)<<endl;

    return 0;
}

 

在解决51nod平台上的“地外遗址”问题时,BFS(广度优先搜索)是一种常用的方法,尤其适用于最短路径问题。该问题通常涉及一个二维网格地图,其中每个格子可能有不同的状态,例如可走、障碍、起点或终点,并要求计算从起点到终点的最短路径,同时满足某些特定条件,如必须访问某些点。 在实现过程中,通常采用状态压缩的方式,将已访问的特殊点用位掩码(bitmask)表示。例如,若地图中有 `n` 个特殊点,那么状态可以使用 `n` 位二进制数来表示哪些点已被访问。为了防止重复访问,使用一个三维的访问数组 `visited[x][y][mask]` 来记录当前坐标 `(x, y)` 和状态 `mask` 是否已经被访问过。 以下是一个基于 BFS 和状态压缩的实现示例: ```cpp #include <bits/stdc++.h> using namespace std; const int MAXN = 20; const int MAX_MASK = 1 << 10; int n, m; char grid[MAXN][MAXN]; int visited[MAXN][MAXN][MAX_MASK]; int sx, sy, ex, ey; struct State { int x, y, mask, dist; }; queue<State> q; int dx[] = {-1, 0, 1, 0}; int dy[] = {0, 1, 0, -1}; bool inBounds(int x, int y) { return x >= 0 && x < n && y >= 0 && y < m; } int main() { cin >> n >> m; for (int i = 0; i < n; ++i) { for (int j = 0; j < m; ++j) { cin >> grid[i][j]; if (grid[i][j] == 'S') { sx = i, sy = j; } else if (grid[i][j] == 'E') { ex = i, ey = j; } } } q.push({sx, sy, 0, 0}); visited[sx][sy][0] = 1; while (!q.empty()) { State curr = q.front(); q.pop(); int x = curr.x, y = curr.y, mask = curr.mask, dist = curr.dist; if (x == ex && y == ey && mask == (1 << 10) - 1) { cout << dist << endl; return 0; } for (int k = 0; k < 4; ++k) { int nx = x + dx[k]; int ny = y + dy[k]; if (!inBounds(nx, ny) || grid[nx][ny] == '#') continue; int new_mask = mask; if (grid[nx][ny] >= '1' && grid[nx][ny] <= '9') { int key = grid[nx][ny] - '1'; new_mask |= (1 << key); } if (!visited[nx][ny][new_mask]) { visited[nx][ny][new_mask] = 1; q.push({nx, ny, new_mask, dist + 1}); } } } cout << -1 << endl; return 0; } ``` 上述代码实现了一个典型的 BFS 搜索过程,其中包含了状态压缩的逻辑。初始状态从起点开始,每次向四个方向扩展,并根据新位置是否为特殊点来更新位掩码。如果当前状态已经访问过,则跳过;否则将其加入队列并标记为已访问。当到达终点且所有特殊点都被访问时,输出当前步数作为最短路径长度。 需要注意的是,该实现假设地图中最多有 10 个特殊点,因此状态总数为 `MAXN x MAXN x (1 << 10)`,在可接受范围内。对于更大的地图或更多的特殊点,可能需要进一步优化,例如使用双向 BFS 或 A* 算法 [^1]。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值