Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- class Solution {
- public:
- bool canReach(vector<int>& arr, int start) {
- queue<int> q;
- int sz = arr.size();
- bool vis[arr.size()];
- memset(vis, 0, sizeof(vis));
- q.push(start);
- vis[start] = true;
- while(!q.empty()){
- int idx = q.front();
- q.pop();
- if(arr[idx] == 0) return true;
- int next = idx+arr[idx];
- if(next < sz && next >= 0 && !vis[next]){
- q.push(next);
- vis[next] = true;
- }
- next = idx-arr[idx];
- if(next < sz && next >= 0 && !vis[next]){
- q.push(next);
- vis[next] = true;
- }
- }
- return false;
- }
- };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement