43 lines
818 B
C++
43 lines
818 B
C++
|
|
#include <bits/stdc++.h>
|
||
|
|
/*
|
||
|
|
5
|
||
|
|
2 1 5 1 3
|
||
|
|
|
||
|
|
答案 2
|
||
|
|
*/
|
||
|
|
using namespace std;
|
||
|
|
struct Node {
|
||
|
|
int pos;
|
||
|
|
int step;
|
||
|
|
};
|
||
|
|
const int N = 110;
|
||
|
|
int a[N];
|
||
|
|
int st[N];
|
||
|
|
int n;
|
||
|
|
void bfs() {
|
||
|
|
queue<Node> q;
|
||
|
|
q.push({1, 0});
|
||
|
|
st[1] = 1;
|
||
|
|
while (q.size()) {
|
||
|
|
auto t = q.front();
|
||
|
|
q.pop();
|
||
|
|
for (int i = 1; i <= a[t.pos]; i++) {
|
||
|
|
int x = t.pos + i;
|
||
|
|
// 先判断下一跳是不是终点
|
||
|
|
if (x == n) {
|
||
|
|
cout << t.step + 1 << endl;
|
||
|
|
exit(0);
|
||
|
|
}
|
||
|
|
// 正常入队列
|
||
|
|
if (st[x]) continue;
|
||
|
|
q.push({x, t.step + 1});
|
||
|
|
st[x] = 1;
|
||
|
|
}
|
||
|
|
}
|
||
|
|
}
|
||
|
|
int main() {
|
||
|
|
cin >> n;
|
||
|
|
for (int i = 1; i <= n; i++) cin >> a[i];
|
||
|
|
bfs();
|
||
|
|
return 0;
|
||
|
|
}
|