题目链接:https://leetcode.cn/problems/jump-game-iii/ 题目如下:
class Solution {
public:
bool canReach(vector<int>& arr, int start) {
int n=arr.size();
if(n==0) return res;
vector<bool> visited(n,false);
backtracking(arr,start,n,visited);
return res;
}
void backtracking(vector<int>& arr,int start,int n,vector<bool>& visited){
if(res==true) return ;
if(arr[start]==0){
res=true;
return ;
}
for(int i=-1;i<=1;i++){
int way=start+i*arr[start];
if(way>=0&&way<n&&visited[way]==false){
visited[way]=true;
backtracking(arr,way,n,visited);
visited[way]=false;
}
}
}
private:
bool res=false;
};
|