标签:
1 /* 2 By:OhYee 3 Github:OhYee 4 Email:oyohyee@oyohyee.com 5 Blog:http://www.cnblogs.com/ohyee/ 6 7 かしこいかわいい? 8 エリーチカ! 9 要写出来Хорошо的代码哦~ 10 */ 11 12 #include <cstdio> 13 #include <algorithm> 14 #include <cstring> 15 #include <cmath> 16 #include <string> 17 #include <iostream> 18 #include <vector> 19 #include <list> 20 #include <queue> 21 #include <stack> 22 using namespace std; 23 24 //DEBUG MODE 25 #define debug 0 26 27 //循环 28 #define REP(n) for(int o=0;o<n;o++) 29 30 const int maxn = 205; 31 int k[maxn]; 32 int N; 33 34 int BFS(int s,int v) { 35 if(s == v) 36 return 0; 37 38 queue<int> Q; 39 bool visited[maxn]; 40 memset(visited,false,sizeof(visited)); 41 int dis[maxn]; 42 memset(dis,0,sizeof(dis)); 43 44 Q.push(s); 45 visited[s] = true; 46 while(!Q.empty()) { 47 int th = Q.front(); 48 Q.pop(); 49 50 //达到终点 51 if(th == v) 52 break; 53 54 //拓展节点 55 int next; 56 for(int i = -1;i == -1 || i == 1;i += 2) { 57 next = th + i * k[th]; 58 if(next > N || next <= 0) 59 continue; 60 if(!visited[next]) { 61 Q.push(next); 62 visited[next] = true; 63 dis[next] = dis[th] + 1; 64 } 65 } 66 67 } 68 69 if(dis[v]) 70 return dis[v]; 71 else 72 return -1; 73 } 74 75 bool Do() { 76 int s,v; 77 if(scanf("%d%d%d",&N,&s,&v),N == 0) 78 return false; 79 REP(N) 80 scanf("%d",&k[o + 1]); 81 printf("%d\n",BFS(s,v)); 82 return true; 83 } 84 85 int main() { 86 while(Do()); 87 return 0; 88 }
标签:
原文地址:http://www.cnblogs.com/ohyee/p/5389459.html