-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path11060.cpp
46 lines (39 loc) · 856 Bytes
/
11060.cpp
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
#include <iostream>
#include <queue>
#include <algorithm>
using namespace std;
int main() {
cin.tie(0);
std::ios_base::sync_with_stdio(0);
int n;
cin >> n;
vector<int> v(n);
for(auto &i:v){
cin >> i;
}
if(n == 1){
cout << 0;
return 0;
}
const int limit = 987654321;
vector<int> vis(n, limit);
queue<pair<int, int>> q;
q.push({0, 0});
while(!q.empty()){
auto now = q.front();
int pos = now.first;
int cnt = now.second;
q.pop();
if(cnt<vis[pos]){
vis[pos] = cnt;
for(int i = 1; i<=v[pos]; i++){
if(pos + i >= n-1){
cout << cnt + 1;
return 0;
}
q.push({pos + i, cnt + 1});
}
}
}
cout << -1;
}