[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平台上的“伟大的航线”问题 目前未找到直接针对“伟大的航线”的具体C++实现或解决方案的相关引用。然而,基于常见的算法竞赛题目设计模式以及类似的路径规划类问题解决方法,以下是可能的思路和代码框架。 #### 可能的解题思路 此类问题通常涉及图论中的最短路径或者动态规划求解。假设问题是给定一张地图(可能是网格形式),需要计算从起点到终点的最佳路径,则可以采用如下策略: 1. **广度优先搜索 (BFS)** 如果每一步移动的成本相同,那么可以通过 BFS 来寻找最短路径[^1]。 2. **Dijkstra 算法** 若不同步数具有不同的权重成本,则 Dijkstra 是一种更通用的选择[^2]。 3. **Floyd-Warshall 算法** 当涉及到多个源点之间的距离查询时,可考虑使用 Floyd-Warshall 进行预处理[^3]。 4. **记忆化递归 / 动态规划** 对某些特定条件下的状态转移方程建模,通过 DP 表格存储中间结果来优化时间复杂度[^4]。 下面是基于上述几种常见技术之一——BFS 的伪代码模板及其解释: ```cpp #include <iostream> #include <queue> #include <vector> using namespace std; // 定义方向向量用于探索邻居节点 const int dx[] = {0, 0, -1, 1}; const int dy[] = {-1, 1, 0, 0}; bool isValid(int x, int y, int n, int m){ return x >= 0 && x < n && y >= 0 && y < m; } int bfs(vector<string> grid, pair<int,int> start, pair<int,int> end){ queue<pair<int,int>> q; vector<vector<bool>> visited(grid.size(), vector<bool>(grid[0].size(), false)); q.push(start); visited[start.first][start.second] = true; int steps = 0; while(!q.empty()){ int size = q.size(); for(int i=0;i<size;i++){ auto current = q.front(); q.pop(); if(current == end) return steps; for(int dir=0;dir<4;dir++){ int nx = current.first + dx[dir]; int ny = current.second + dy[dir]; if(isValid(nx,ny,(int)grid.size(),(int)grid[0].size()) && !visited[nx][ny] && grid[nx][ny]!='#'){ q.push({nx,ny}); visited[nx][ny]=true; } } } steps++;
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值