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.

38 lines
1.1 KiB

2 years ago
#include <bits/stdc++.h>
using namespace std;
int main() {
//n:n层楼
//a,b:从a楼到b楼
int n, a, b;
cin >> n >> a >> b;
// n个楼层上面的数字分别是多少
// 动态申请
int *k = new int[n];
for (int i = 1; i <= n; ++i) {
cin >> k[i];
}
//因为在某一层,只有两个选择,向下或者向上,把不合理的排除掉后,剩下的就是可以走的楼层,其实就是利用队列实现一个广度优先
queue<int> q;
//将出发点放入队列
q.push(a);
//点击次数
int count = 0;
while (!q.empty()) {
int c = q.front();
if (c == b) break;
//如果可以向上按下按钮上不越界
if (c + k[c] <= n) q.push(c + k[c]);
//如果可以向下按下按钮下不越界
if (c - k[c] >= 0) q.push(c - k[c]);
q.pop();
//pop出去一个才表示进入了下一次就是点了一次
count++;
}
cout << count << endl;
//养成好习惯,释放内存
delete[]k;
return 0;
}