简单bfs,
输入 n ,a ,b,
k1~kn.
代表有n楼,起点为a,终点为b,在第i楼,只能上ki或者下ki楼
<span style="font-size:18px;">#include <stdio.h>
#include <queue>
#define INF 0xffffff
using namespace std;
int n,a,b,ans;
int lift[205];<span style="white-space:pre"> </span>//记录每层楼上下的值
int sign[205];<span style="white-space:pre"> </span>//记录是否走过
typedef struct ac
{
int x,step;
}node;
void bfs(int x,int step)
{
queue <node > q;
node next,temp;
next.x=x;
next.step=step;
q.push(next);
sign[x]=1;
while(!q.empty())
{
next=q.front();
q.pop();
if(next.x==b)
{
ans=next.step;
return ;
}
for(int i=1;i<=2;i++)
{
if(i==1)
temp.x=next.x+lift[next.x];
else
temp.x=next.x-lift[next.x];
temp.step=next.step+1;
if(temp.x<1 || temp.x>n || sign[temp.x])
continue ;
sign[temp.x]=1;
q.push(temp);
}
}
}
int main()
{
while(scanf("%d",&n) && n)
{
scanf("%d %d",&a,&b);
for(int i=1;i<=n;i++)
{
scanf("%d",&lift[i]);
sign[i]=0;
}
ans=INF;
bfs(a,0);
if(ans==INF)
printf("-1\n");
else
printf("%d\n",ans);
}
return 0;
}
</span>