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.

43 lines
949 B

This file contains ambiguous Unicode characters!

This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.

#include <bits/stdc++.h>
using namespace std;
const int N = 100010;
typedef pair<int,int> PII;
int n;
int a, b;
int k[N];
int st[N];
queue<PII> q;
int step = -1;
void bfs() {
//初始化队列
q.push({a, 0});
st[a] = 1;
while (q.size()) {
auto t = q.front();
q.pop();
if (t.first == b) {
step = t.second;
break;
}
int x = t.first + k[t.first];
if (x <= n && !st[x]) {
q.push({x, t.second + 1});
st[x] = 1;
}
x = t.first - k[t.first];
if (x >= 1 && !st[x]) {
q.push({x, t.second + 1});
st[x] = 1;
}
}
}
int main() {
//n是几层楼a:从几层出发 ,b:到几层去
cin >> n >> a >> b;
for (int i = 1; i <= n; i++) cin >> k[i]; //记录每层楼的可以按下的数字
bfs();
printf("%d", step);
return 0;
}