所有可达路径-卡玛-所有可能的路径-力扣

题目链接:所有可达路径
本题是一道图论的入门题目,这里使用了邻接链表的方式存储图,并使用深度优先搜索的方式对图进行搜索。

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

vector<vector<int>> result;
vector<int> path;

void dfs(const vector<list<int>>& graph, int cur, int end){
    if(cur == end){
        result.push_back(path);
        return;
    }
    for(const int& i : graph[cur]){
        path.push_back(i);
        dfs(graph, i, end);
        path.pop_back();
    }
}

int main(){
    int nodeNum, edgeNum,s,t;
    cin >> nodeNum >> edgeNum;
    vector<list<int>> graph(nodeNum + 1);
    
    for(int i = 0; i < edgeNum; i++){
        cin >> s >> t;
        graph[s].push_back(t);
    }
    path.push_back(1);
    dfs(graph, 1, nodeNum);
    
    if(result.size() == 0){
        cout << -1 << endl;
    }
    for(auto& onepath : result){
        for(int i = 0; i < onepath.size() - 1; i++){
            cout << onepath[i] << ' ';
        }
        cout << onepath[onepath.size() - 1] << endl;
    }
    return 0;
}

以下是使用邻接矩阵的实现代码:

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

vector<vector<int>> result;
vector<int> path;

void dfs(const vector<vector<int>>& graph, int cur, int end){
    if(cur == end){
        result.push_back(path);
        return;
    }
    for(int i = 1; i <= end; i++ ){
        if(graph[cur][i] == 1){
            path.push_back(i);
            dfs(graph, i, end);
            path.pop_back();
        }
    }
}

int main(){
    int nodeNum, edgeNum,s,t;
    cin >> nodeNum >> edgeNum;
    vector<vector<int>> graph(nodeNum + 1, vector<int>(nodeNum + 1, 0));
    
    for(int i = 0; i < edgeNum; i++){
        cin >> s >> t;
        graph[s][t] = 1;
    }
    path.push_back(1);
    dfs(graph, 1, nodeNum);
    
    if(result.size() == 0){
        cout << -1 << endl;
    }
    for(auto& onepath : result){
        for(int i = 0; i < onepath.size() - 1; i++){
            cout << onepath[i] << ' ';
        }
        cout << onepath[onepath.size() - 1] << endl;
    }
    return 0;
}

在写完卡玛网的所有可达路径后,这道题也可以轻松完成。

class Solution {
private:
    vector<vector<int>> result;
    vector<int> path;
public:
    void dfs(vector<vector<int>>& graph, int cur, int end){
        if(cur == end){
            result.push_back(path);
            return;
        }
        for(int i = 0; i < graph[cur].size(); i++){
            path.push_back(graph[cur][i]);
            dfs(graph, graph[cur][i], end);
            path.pop_back();
        }
    }
    vector<vector<int>> allPathsSourceTarget(vector<vector<int>>& graph) {
        int end = graph.size() - 1;
        path.push_back(0);
        dfs(graph, 0, end);
        return result;
    }
};

广度搜索解法,利用一个queue<vector> 存储一条条广度搜索的路径,那么每次广度搜索的点,就是一条路径的最后一个节点

class Solution {
public:
    vector<vector<int>> result;
    vector<vector<int>> allPathsSourceTarget(vector<vector<int>>& graph) {
        queue<vector<int>> que;
        vector<int> path;
        path.push_back(0);
        que.push(path);
        while(!que.empty()){
            int size = que.size();
            for(int i = 0; i < size; i++){
                vector<int> curpath = que.front();
                que.pop();
                int curnode = curpath[curpath.size() - 1];
                if(curnode == graph.size() - 1){
                    result.push_back(curpath);
                    continue;
                }
                for(int next : graph[curnode]){
                    vector<int> tmp = curpath;
                    tmp.push_back(next);
                    que.push(tmp);
                }
            }
        }
        return result;
    }
};
  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值