Description
INPUT
OUTPUT
SAMPLE INPUT
SAMPLE OUTPUT
HINT
#include<iostream>
#include<queue>
using namespace std;
struct node
{
int x,step;
};
int use[201],n,dir[201][2],ans;
int bfs(int s,int e)
{
int i,flag=0;
node p1;
queue<node>Q;
p1.x=s;
p1.step=0;
Q.push(p1);
use[p1.x]=1;
while(!Q.empty())
{
p1=Q.front();
Q.pop();
if(p1.x==e)
{
flag=1;
ans=p1.step;
break;
}
for(i=0;i<=1;i++)
{
node p2;
p2.x=p1.x+dir[p1.x][i];
if(p2.x>=1&&p2.x<=n&&!use[p2.x])
{
p2.step=p1.step+1;
use[p2.x]=1;
Q.push(p2);
}
}
}
if(flag)
return ans;
else
return -1;
}
int main()
{
int s,t,i;
while(cin>>n&&n!=0)
{
cin>>s>>t;
for(i=1;i<=n;i++)
{
cin>>dir[i][0];
dir[i][1]=-dir[i][0];
}
memset(use,0,sizeof(use));
cout<<bfs(s,t)<<endl;
}
}