标签:des style blog http color os java io ar
5 1 5 3 3 1 2 5 0
3
解题:bfs...
1 #include <iostream> 2 #include <cstdio> 3 #include <cstring> 4 #include <cmath> 5 #include <algorithm> 6 #include <climits> 7 #include <vector> 8 #include <queue> 9 #include <cstdlib> 10 #include <string> 11 #include <set> 12 #include <stack> 13 #define LL long long 14 #define pii pair<int,int> 15 #define INF 0x3f3f3f3f 16 using namespace std; 17 struct node{ 18 int p,step; 19 node(int x = 0,int y = 0):p(x),step(y){} 20 }; 21 int N,A,B,k[210]; 22 bool vis[210]; 23 queue<node>q; 24 int bfs(){ 25 while(!q.empty()) q.pop(); 26 memset(vis,false,sizeof(vis)); 27 vis[A] = true; 28 node temp(A,0); 29 q.push(temp); 30 int to; 31 while(!q.empty()){ 32 node now = q.front(); 33 q.pop(); 34 if(now.p == B) return now.step; 35 to = now.p + k[now.p]; 36 if(to <= N && !vis[to]){ 37 vis[to] = true; 38 q.push(node(to,now.step+1)); 39 } 40 to = now.p - k[now.p]; 41 if(to > 0 && !vis[to]){ 42 vis[to] = true; 43 q.push(node(to,now.step+1)); 44 } 45 } 46 return -1; 47 } 48 int main() { 49 while(scanf("%d",&N),N){ 50 scanf("%d %d",&A,&B); 51 for(int i = 1; i <= N; i++) 52 scanf("%d",k+i); 53 printf("%d\n",bfs()); 54 } 55 return 0; 56 }
标签:des style blog http color os java io ar
原文地址:http://www.cnblogs.com/crackpotisback/p/3945732.html