36分代码如下:
#include<bits/stdc++.h>
using namespace std;
int main(){
int n,d1,d2,c[10001];
cin>>n>>d1>>d2;
for(int i=1;i<=n;i++){
cin>>c[i];
}
if(d1==d2){
cout<<0<<endl;
return 0;
}
int q[10001];//队列
int h=0,t=0;//h头t尾
int b[10001]={0};//有无走过
int bs[10001]={0};//计算按键数量
//初始层初始化
t++;
bs[d1]=0;
b[d1]=-1;
q[h]=d1;
while(h<t){
h++;
int z1=q[h-1]+c[q[h-1]],z2=q[h-1]-c[q[h-1]];
if(z1>=1&&b[z1]==0&&z1<=n){
//cout<<z1<<endl;
t++;
bs[z1]=bs[q[h-1]]+1;
b[z1]=-1;
q[h]=z1;
}
if(q[h]==d2){
cout<<bs[d2]<<endl;
return 0;
}
if(z2>=1&&b[z2]==0&&z2<=n){
//cout<<z2<<endl;
t++;
bs[z2]=bs[q[h-1]]+1;
b[z2]=-1;
q[h]=z2;
}
if(q[h]==d2){
cout<<bs[d2]<<endl;
return 0;
}
}
cout<<-1<<endl;
return 0;
}