1.题目要求:
2.题目示例:
3.解题步骤
采用回溯算法,并利用图的深度优先搜索去解此题
4.题目代码 :
class Solution {
public:vector<vector<int>> result;vector<int> array; //采用回溯算法void allpaths(vector<vector<int>>& graph,int v_index){if(v_index == graph.size() - 1){result.push_back(array);return;}for(int j = 0;j < graph[v_index].size();j++){int index = graph[v_index][j];array.push_back(index);allpaths(graph,index);//进行回溯array.pop_back();}}vector<vector<int>> allPathsSourceTarget(vector<vector<int>>& graph) {//设置遍历下标int v_index = 0;//因为要从下标0处开始,所以先把下标0存入数组中array.push_back(v_index);allpaths(graph,v_index);return result;}
};