题目条件真是变态。。。无语了
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
typedef struct queue
{
int front;
int rear;
int s[210];
}queue;
queue bark;
int flag[210];
int lift[210];
void init()
{
bark.front=bark.rear=1;
}
void enqueue(int k)
{
bark.s[bark.rear]=k;
bark.rear++;
}
int dequeue()
{
int t=bark.front;
bark.front++;
return bark.s[t];
}
int isempty()
{
if(bark.front == bark.rear)return 1;
else return 0;
}
int bfs(int a,int b)
{
int u,k;
init();
enqueue(a);flag[a]=1;
while(!isempty())
{
k=dequeue();
if(k+lift[k]>0 && k+lift[k]<=b && !flag[k+lift[k]])
{
flag[k+lift[k]]=flag[k]+1;
enqueue(k+lift[k]);
}
if(k-lift[k]>0 && !flag[k-lift[k]])
{
flag[k-lift[k]]=flag[k]+1;
enqueue(k-lift[k]);
}
if(k==b)
return flag[k]-flag[a];
}
return -1;
}
int main()
{
int n;
int a,b,i;
while(scanf("%d",&n)&&n)
{
memset(flag,0,sizeof(flag));
scanf("%d%d",&a,&b);
for(i=1;i<=n;i++)
scanf("%d",&lift[i]);
printf("%d\n",bfs(a,b));
}
return 0;
}