http://acm.hdu.edu.cn/showproblem.php?pid=1548
#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],cost[205];
void dir(int s,int e)
{
int i,j,min,pos;
memset(vis,0,sizeof(vis));
for(int i=0; i<n; i++)
{
cost[i]=map[s][i];
}
cost[s]=0;
vis[s]=1;
for(int i=1; i<=n; i++)
{
min=inf;
for(int j=0; j<n; j++)
{
if(cost[j]<min&!vis[j])
{
pos=j;
min=cost[j];
}
}
if(min==inf)break;
vis[pos]=1;
for(int j=0;j<n;j++){
if(cost[pos]+map[pos][j]<cost[j]&&!vis[j]){
cost[j]=cost[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;//代表第i层到可以到达层数的最短路径是1
if(i-a[i]>=0)
map[i][i-a[i]] = 1;
}
dir(s,e);
printf("%d\n",cost[e]==inf?-1:cost[e]);
}
return 0;
}