标签:
1 #include <iostream> 2 #include <cstring> 3 #include <cstdio> 4 #include <queue> 5 using namespace std; 6 int vis[210],le[210],n; 7 int bfs(int a,int b) 8 { 9 int c; 10 memset(vis,0,sizeof(vis)); 11 queue<int>q; 12 q.push(a); 13 while(!q.empty()) 14 { 15 c=q.front(); 16 q.pop(); 17 if(c+le[c]<=n&&!vis[c+le[c]]) 18 { 19 vis[c+le[c]]=vis[c]+1; 20 q.push(c+le[c]); 21 } 22 if(c-le[c]>=1&&!vis[c-le[c]]) 23 { 24 vis[c-le[c]]=vis[c]+1; 25 q.push(c-le[c]); 26 } 27 } 28 if(vis[b]!=0) 29 { 30 return vis[b]; 31 } 32 else 33 { 34 return -1; 35 } 36 } 37 int main() 38 { 39 #ifdef CDZSC_OFFLINE 40 freopen("in.txt","r",stdin); 41 #endif 42 int i,a,b,sum; 43 while(scanf("%d",&n)&&n!=0) 44 { 45 scanf("%d%d",&a,&b); 46 for(i=1; i<=n; i++) 47 { 48 scanf("%d",&le[i]); 49 } 50 if(a==b)//相同时输出0 51 { 52 printf("0\n"); 53 continue; 54 } 55 sum=bfs(a,b); 56 printf("%d\n",sum); 57 } 58 return 0; 59 }
标签:
原文地址:http://www.cnblogs.com/Wing0624/p/4253931.html