Files
python/TangDou/XiTi/1375.cpp
HuangHai 1f397eca87 'commit'
2025-08-30 18:35:01 +08:00

38 lines
1.1 KiB
C++
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

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