huangyichen 2017-09-06 22:28:47
点我顶贴
收藏
删除
#include<cstdio>
#include<cstring>
int n,a,b;
int dian[210];
struct node{
int a,d;
}h[2110];
int st=1,ed=2;
int fx[2]={-1,1};
bool tf[210]={0};
int bu=-1;
void bfs()
{
while(st!=ed)
{
for(int i=0;i<2;i++)
{
int xx=dian[h[st].a]*fx[i]+h[st].a;
if(xx>=1&&xx<=n&&tf[xx]==0)
{
if(xx==b){bu=h[st].d+1;return;}
h[ed].a=xx;
h[ed].d=h[st].d+1;
tf[xx]=1;
ed++;
}
}
st++;
}
}
int main()
{
scanf("%d %d %d",&n,&a,&b);
for(int i=1;i<=n;i++)
scanf("%d",&dian[i]);
if(a==b)
{printf("0");return 0;}
h[st].d=0;
tf[a]=1;
h[st].a=a;
bfs();
printf("%d",bu);
return 0;
}