56 lines
741 B
C++
56 lines
741 B
C++
#include <bits/stdc++.h>
|
||
#define endl '\n'
|
||
using namespace std;
|
||
|
||
int n, a, b;
|
||
int cnt = 0;
|
||
int arr[100100];
|
||
|
||
int fx[] = {0, 1, -1};
|
||
|
||
int q[100000][2]; // ϱê0±íʾ´ÎÊý
|
||
|
||
void bfs(int k){
|
||
int head = 1, tail = 1;
|
||
q[head][0] = 0;
|
||
q[head][1] = k;
|
||
|
||
while(head <= tail){
|
||
int curr = q[head][1]; //µ±Ç°Â¥²ã
|
||
|
||
int tx;
|
||
for(int i = 1; i <= 2; i++){
|
||
tx = curr + fx[i] * arr[curr];
|
||
|
||
if(tx >= 1 && tx <= n){
|
||
tail++;
|
||
q[tail][0] = q[head][0] + 1;
|
||
q[tail][1] = tx;
|
||
|
||
if(tx == b) {
|
||
cout << q[tail][0];
|
||
exit(0);
|
||
}
|
||
}
|
||
}
|
||
head++;
|
||
}
|
||
}
|
||
|
||
int main(){
|
||
cin >> n >> a >> b;
|
||
if(a == b){
|
||
cout << 0;
|
||
return 0;
|
||
}
|
||
for(int i = 1; i <= n; i++){
|
||
cin >> arr[i];
|
||
}
|
||
|
||
bfs(a);
|
||
|
||
cout << -1;
|
||
return 0;
|
||
}
|
||
|