There is a strange lift.The lift can stop can at every floor as you want, and there is a number Ki(0 <= Ki <= N) on every floor.The lift have just two buttons: up and down.When you at floor i,if you press the button "UP" , you will go up Ki floor,i.e,you will go to the i+Ki th floor,as the same, if you press the button "DOWN" , you will go down Ki floor,i.e,you will go to the i-Ki th floor. Of course, the lift can't go up high than N,and can't go down lower than 1. For example, there is a buliding with 5 floors, and k1 = 3, k2 = 3,k3 = 1,k4 = 2, k5 = 5.Begining from the 1 st floor,you can press the button "UP", and you'll go up to the 4 th floor,and if you press the button "DOWN", the lift can't do it, because it can't go down to the -2 th floor,as you know ,the -2 th floor isn't exist. 
Here comes the problem: when you are on floor A,and you want to go to floor B,how many times at least he has to press the button "UP" or "DOWN"? 

Input

The input consists of several test cases.,Each test case contains two lines. 
The first line contains three integers N ,A,B( 1 <= N,A,B <= 200) which describe above,The second line consist N integers k1,k2,....kn. 
A single 0 indicate the end of the input.

Output

For each case of the input output a interger, the least times you have to press the button when you on floor A,and you want to go to floor B.If you can't reach floor B,printf "-1".

Sample Input

5 1 5
3 3 1 2 5
0

Sample Output

3

C++版本一(DFS)

#include <iostream>
#include <cstdio>
#include <cstring>
using namespace std;
int n,a,b, ans;
int d[210];
int vis[210];

void dfs(int a,int step)
{
    if (a==b){
        if(ans>step)
        ans=step;
        return;
    }
    if(ans>step) {
        if(a+d[a-1]<=n&&vis[a+d[a-1]]==0){
            vis[a+d[a-1]]=1;
            dfs(a+d[a-1],step+1);
            vis[a+d[a-1]]=0;
        }

        if(a-d[a-1]>=1&&vis[a-d[a-1]]==0){
            vis[a-d[a-1]]=1;
            dfs(a-d[a-1],step+1);
            vis[a-d[a-1]]=0;
        }

    }

}
int main()
{
    while (scanf("%d",&n)!=EOF){
        if (n==0)   break;
        scanf("%d%d",&a,&b);
        memset(d,0,sizeof(d));
        memset(vis,0,sizeof(vis));

        for(int i=0;i<n;i++){
            scanf("%d",&d[i]);
        }
        vis[a]=1;
        ans=n+1;
        dfs(a,0);
        if(ans==n+1)printf("-1\n");
        else printf("%d\n",ans);



    }

    //cout << "Hello world!" << endl;
    return 0;
}

C++版本二(DFS)

#include<stdio.h>
#include<string.h>
#include<iostream>
#include<algorithm>
#include<queue>
#include<string>
#include<set>
using namespace std;
int n,a,b;
int c[201],vis[201];
void dfs(int x,int s)
{
	if(s==vis[b]) return;
	if(x==b)
	{
		vis[b]=min(vis[b],s);//更新最小步数
		return;
	}
	if(x+c[x]<=n&&vis[x+c[x]]>s+1)//往上能走且到达那层的步数比当前数值小
	{
		vis[x+c[x]]=s+1;//更新走到当前楼层需要的步数
		dfs(x+c[x],s+1);
	}
	if(x-c[x]>0&&vis[x-c[x]]>s+1)//往下走
	{
		vis[x-c[x]]=s+1;
		dfs(x-c[x],s+1);
	}
}
int main()
{
	while (scanf("%d",&n)!=EOF&&n)
	{
		scanf("%d%d",&a,&b);
		for(int i=1;i<=n;i++)
		{
			scanf("%d",&c[i]);
			vis[i]=201;
		}
		vis[a]=0;//必须为0
		dfs(a,0);
		if (vis[b]!=201) printf("%d\n",vis[b]);
		else printf("-1\n");
	}
	return 0;
}

C++版本三(BFS)

#include<stdio.h>
#include<queue>
using namespace std;
int n,a,b;
int c[201];
int vis[201];
void bfs()
{
	queue<int>q;
	q.push(a);
	while(!q.empty())
	{
		int x=q.front();
		q.pop();
		if(x==b) return;//能走到
		if(x+c[x]<=n&&vis[x+c[x]]>vis[x]+1) //向上能走且当前步数小于记录的值
		{
			vis[x+c[x]]=vis[x]+1;
			q.push(x+c[x]);
		}
		if(x-c[x]>0&&vis[x-c[x]]>vis[x]+1) //同理
		{
			vis[x-c[x]]=vis[x]+1;
			q.push(x-c[x]);
		}
	}
}
int main()
{
	while(scanf("%d",&n)!=EOF&&n!=0)
	{
		scanf("%d%d",&a,&b);
		for(int i=1;i<=n;i++)
		{
			scanf("%d",&c[i]);
			vis[i]=201;
		}
		vis[a]=0;//必须为0
		bfs();
		if(vis[b]==201) printf("-1\n");
		else printf("%d\n",vis[b]);
	}
	return 0;
}

C++版本四(Dijkstra)

#include <stdio.h>
#include <string.h>
#include <algorithm>
using namespace std;

const int inf = 1<<30;

int n;
int map[205][205];
int a[205],cnt;
int vis[205],cast[205];

void Dijkstra(int s,int e)
{
    int i,j,min,pos;
    memset(vis,0,sizeof(vis));
    for(i = 0; i<n; i++)
        cast[i] = map[s][i];
    cast[s] = 0;
    vis[s] = 1;
    for(i = 1; i<n; i++)
    {
        min = inf;
        for(j = 0; j<n; j++)
        {
            if(cast[j]<min && !vis[j])
            {
                pos = j;
                min = cast[j];
            }
        }
        if(min == inf)
            break;
        vis[pos] = 1;
        for(j = 0; j<n; j++)
        {
            if(cast[pos]+map[pos][j]<cast[j] && !vis[j])
                cast[j] = cast[pos]+map[pos][j];
        }
    }
}

int main()
{
    int i,j,s,e,x,y;
    while(~scanf("%d",&n),n)
    {
        scanf("%d%d",&s,&e);
        s--,e--;
        for(i = 0; i<n; i++)
            for(j = 0; j<n; j++)
                map[i][j] = inf;
        for(i = 0; i<n; i++)
        {
            scanf("%d",&a[i]);
            if(i+a[i]<n)
                map[i][i+a[i]] = 1;
            if(i-a[i]>=0)
                map[i][i-a[i]] = 1;
        }
        Dijkstra(s,e);
        printf("%d\n",cast[e]==inf?-1:cast[e]);
    }

    return 0;
}

 

10-07 16:07