Files
ClassFeedback/C++代码/学习/奇怪的电梯.cpp

56 lines
741 B
C++
Raw 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>
#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;
}