<a target=_blank href="http://acm.hdu.edu.cn/showproblem.php?pid=1548">原题链接</a>
/*
广度优先搜索题
到每一层楼都可以上楼和下楼,但要注意的是不能小于1当然也不能大于n
每到一层楼就把层数放在队列里,取出层数后又去掉
然后通过每个层数生成子节点进行搜索
*/
#include<iostream>
#include<cstdio>
#include<cmath>
#include<cstring>
#include<string>
#include<algorithm>
#include<cstdlib>
#include<queue>
#define INF 0xfffffff
using namespace std;
int time[205], upd[205][2];
int n, a, b;
int bfs()
{
memset(time,-1,sizeof(time));
queue<int> p;//建立队列
p.push(a);//将开始所在的楼层添加到队列中
time[a] = 0;//用time保存到达所在楼层按电梯的次数,在开始所在楼层时,当然赋值为0
int t, next;
while(!p.empty())//只有队列非空才进行搜索
{
t = p.front();//获取队头
p.pop();//去队头
if(t == b) return time[t];//当队头等于b时,到达,返回到达所在楼层按电梯的次数
for(int i = 0; i < 2; i++)
{
next = t + upd[t][i];//上下楼
if(next>=1&&next<=n&&time[next]==-1)//所到楼层不能小于1也不能大于n
{
time[next] = time[t] + 1;//到达满足条件的楼层,所在楼层次数加一
p.push(next);//将所在楼层添加到队列中
}
}
}
return -1;//如果到不了指定楼层,返回-1
}
int main()
{
while(cin >> n && n)
{
cin >> a >> b;
for(int i = 1; i <= n; i++)
{
cin >> upd[i][0];
upd[i][1] = -upd[i][0];//upd[i][0]为上楼,upd[i][1]为下楼
}
cout << bfs() << endl;
}
return 0;
}