#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cstring>
#define M 205
#define INF 0x3f3f3f3f
using namespace std; int map[M][M];
int d[M], vis[M];
int n;
int b, e; void Dijkstra(){
memset(d, 0x3f, sizeof(d));
memset(vis, , sizeof(vis));
d[b]=;
vis[b]=;
int root=b;
for(int j=; j<n; ++j){
int minLen=INF, p;
for(int i=; i<=n; ++i){
if(!vis[i] && d[i] > d[root] + map[root][i])
d[i] = d[root] + map[root][i];
if(!vis[i] && minLen>d[i]){
p=i;
minLen=d[i];
}
}
if(minLen==INF)
return;
root=p;
vis[root]=;
}
} int main(){
while(cin>>n && n){
cin>>b>>e;
memset(map, 0x3f, sizeof(map));
for(int i=; i<=n; ++i){
int x, xx;
cin>>x;
map[i][i]=;// i+(-)x 表示 从第i层可以到达第 i+(-)x层
xx=i-x;
if(xx>=) map[i][xx]=;
xx=i+x;
if(xx<=n) map[i][xx]=;
}
Dijkstra();
if(d[e]==INF)
cout<<"-1"<<endl;
else
cout<<d[e]<<endl;
}
return ;
}
 /*
思路:当前电梯位置的两个方向进行搜索!
注意:搜索时的界限, 用x表示将要到达的电梯的位置
1. 首先是x>=1
2. x<=n
3. vis[x]!=0 表明bfs时候没有经过这个楼层
*/
#include<iostream>
#include<cstring>
#include<cstdio>
#include<queue>
using namespace std;
struct node{
int x;
int step;
node(){}
node(int x, int step){
this->x=x;
this->step=step;
}
};
queue<node>q;
int n, b, e;
int f[];
int vis[]; bool bfs(){
while(!q.empty()) q.pop();
memset(vis, , sizeof(vis));
q.push(node(b,));
vis[b]=;
while(!q.empty()){
node cur=q.front();
q.pop();
if(cur.x==e){
cout<<cur.step<<endl;
return true;
}
int xx;
xx=cur.x+f[cur.x];
if(xx==e){
cout<<cur.step+<<endl;
return true;
}
if(xx<=n && !vis[xx]){
q.push(node(xx, cur.step+));
vis[xx]=;
}
xx=cur.x-f[cur.x];
if(xx==e){
cout<<cur.step+<<endl;
return true;
}
if(xx>= && !vis[xx]){
q.push(node(xx, cur.step+));
vis[xx]=;
}
}
return false;
} int main(){
while(cin>>n && n){
cin>>b>>e;
for(int i=; i<=n; ++i)
cin>>f[i];
if(!bfs())
cout<<"-1"<<endl;
}
return ;
}
05-11 09:23