Bootstrap

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

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

#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;
    }
};
;