#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, 0, sizeof(vis));
d[b]=0;
vis[b]=1;
int root=b;
for(int j=1; j<n; ++j){
int minLen=INF, p;
for(int i=1; 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]=1;
}
}
int main(){
while(cin>>n && n){
cin>>b>>e;
memset(map, 0x3f, sizeof(map));
for(int i=1; i<=n; ++i){
int x, xx;
cin>>x;
map[i][i]=0;// i+(-)x 表示 从第i层可以到达第 i+(-)x层
xx=i-x;
if(xx>=1) map[i][xx]=1;
xx=i+x;
if(xx<=n) map[i][xx]=1;
}
Dijkstra();
if(d[e]==INF)
cout<<"-1"<<endl;
else
cout<<d[e]<<endl;
}
return 0;
}
本文转自 小眼儿 博客园博客,原文链接:http://www.cnblogs.com/hujunzheng/p/3904608.html,如需转载请自行联系原作者