#include"cstdio" #include"queue" #include"cstring" using namespace std; const int MAXN=205; typedef pair<int,int> P; int N,A,B; int K[MAXN]; int vis[MAXN]; int bfs() { queue<P> que; que.push(P(0,A)); vis[A]=1; while(!que.empty()) { P now=que.front();que.pop(); if(now.second==B) { return now.first; } for(int i=-1;i<=1;i++) { int next=now.second+K[now.second]*i; if(0<next&&next<=N&&!vis[next]) { vis[next]=1; que.push(P(now.first+1,next)); } } } return -1; } int main() { while(scanf("%d",&N)!=EOF&&N!=0) { scanf("%d %d",&A,&B); for(int i=1;i<=N;i++) { scanf("%d",&K[i]); } memset(vis,0,sizeof(vis)); printf("%d\n",bfs()); } return 0; }
时间: 2024-11-10 01:01:00