leetcode 797 所有可能的路径
class Solution {public:vector<vector<int>> result; // 可行路径集合vector<int> path; // 路径void dfs(const vector<vector<int>>& gps, int x, int n){if(x == n-1){result.push_back(path);return ;}for(int i=0; i<n; ++i){if(gps[x][i] == 1){ path.push_back(i);dfs(gps, i, n);path.pop_back();}}return ;}vector<vector<int>> allPathsSourceTarget(vector<vector<int>>& graph) {int n = graph.size();vector<vector<int>> used_graph(n, vector<int>(n, 0));for(int i=0; i<n; ++i){ if(!graph[i].empty()){ for(int j=0; j<graph[i].size(); ++j){used_graph[i][graph[i][j]] = 1; }}}for(auto i:used_graph){for(auto j:i){cout << j;}}path.push_back(0);dfs(used_graph, 0, n);return result;}
};
@代码随想录