Files
python/TangDou/LanQiaoBei/LanQiaoBei13GuoSai/3_1.cpp
HuangHai 1f397eca87 'commit'
2025-08-30 18:35:01 +08:00

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;
}