You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

33 lines
759 B

#include <bits/stdc++.h>
using namespace std;
typedef pair<int, int> PII;
const int N = 110;
int a[N];
int st[N];
int n;
void bfs() {
queue<PII> q; // first:当前木桩号 sencond:从起点走了几步到达了当前的木桩
q.push({1, 0});
st[1] = 1;
while (q.size()) {
auto t = q.front();
q.pop();
if (t.first == n) {
cout << t.second << endl;
exit(0);
}
for (int i = 1; i <= a[t.first]; i++) {
int x = t.first + i;
if (st[x]) continue;
q.push({x, t.second + 1});
st[x] = 1;
}
}
}
int main() {
cin >> n;
for (int i = 1; i <= n; i++) cin >> a[i];
bfs();
return 0;
}