代码:
#include<bits/stdc++.h>
using namespace std;
int M[201];
int n, a, b;
int k[201];
queue<int> q;
int main(){
cin >> n >> a >> b;
for(int i=1; i<=n; i++) cin >> k[i];
memset(M, -1, sizeof(M));
M[a] = 0;
q.push(a);
while(!q.empty()){
int p = q.front();
q.pop();
if(p==b) break;
if(p+k[p]<=n) {
M[p+k[p]] = M[p]+1;
q.push(p+k[p]);
}
if(p-k[p]>=1) {
M[p-k[p]] = M[p]+1;
q.push(p-k[p]);
}
}
cout << M[b] << endl;
system("pause");
return 0;
}
测试点