-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfrog_jump.cpp
More file actions
50 lines (35 loc) · 1.03 KB
/
frog_jump.cpp
File metadata and controls
50 lines (35 loc) · 1.03 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
#include<bits/stdc++.h>
using namespace std;
bool canCross(vector<int>& stones) {
set<int> stone;
for(int i=0; i<stones.size(); i++){
stone.insert(stones[i]);
}
unordered_map<int,set<int>> mp;
mp[0].insert(1);
for(int i=0; i<stones.size(); i++){
for(auto jump: mp[stones[i]]){
int pos = stones[i]+jump;
if(pos == stones[stones.size() - 1]){
return true;
}
if(stone.find(pos) != stone.end()){
if(jump -1 > 0){
mp[pos].insert(jump -1);
}
mp[pos].insert(jump);
mp[pos].insert(jump+1);
}
}
}
return false;
}
int main(){
int n = 1000;
vector<int> arr(n);
for(int i=0; i<n; i++){
cin>>arr[i];
}
cout<<canCross(arr);
return 0;
}