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.
36 lines
879 B
36 lines
879 B
#include <iostream>
|
|
#include <queue>
|
|
#include <vector>
|
|
using namespace std;
|
|
|
|
int main() {
|
|
int N; // 木桩的数量
|
|
cin >> N;
|
|
|
|
vector<int> nums(N); // 每个木桩上的数字
|
|
for (int i = 0; i < N; i++) {
|
|
cin >> nums[i];
|
|
}
|
|
|
|
vector<int> steps(N, -1); // 每个木桩的步数,初始值设为-1
|
|
steps[0] = 0; // 第一个木桩的步数为0
|
|
|
|
queue<int> q; // 存储已经遍历了的木桩
|
|
q.push(0);
|
|
|
|
while (!q.empty()) {
|
|
int cur = q.front();
|
|
q.pop();
|
|
|
|
for (int i = cur + 1; i <= cur + nums[cur] && i < N; i++) {
|
|
if (steps[i] == -1 || steps[i] > steps[cur] + 1) {
|
|
steps[i] = steps[cur] + 1;
|
|
q.push(i);
|
|
}
|
|
}
|
|
}
|
|
|
|
cout << steps[N - 1] << endl; // 输出最后一个木桩的步数
|
|
|
|
return 0;
|
|
} |