hdoj 1548 A strange lift

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1548

解题思路:BFS 搜索

  1 ///////////////////////////////////////////////////////////////////////////
  2 //problem_id: hdoj 1548
  3 //user_id: SCNU20102200088
  4 ///////////////////////////////////////////////////////////////////////////
  5 
  6 #include <algorithm>
  7 #include <iostream>
  8 #include <iterator>
  9 #include <iomanip>
 10 #include <cstring>
 11 #include <cstdlib>
 12 #include <string>
 13 #include <vector>
 14 #include <cstdio>
 15 #include <cctype>
 16 #include <cmath>
 17 #include <queue>
 18 #include <stack>
 19 #include <list>
 20 #include <set>
 21 #include <map>
 22 using namespace std;
 23 
 24 ///////////////////////////////////////////////////////////////////////////
 25 typedef long long LL;
 26 const double PI=acos(-1.0);
 27 
 28 const int x4[]={-1,0,1,0};
 29 const int y4[]={0,1,0,-1};
 30 const int x8[]={-1,-1,0,1,1,1,0,-1};
 31 const int y8[]={0,1,1,1,0,-1,-1,-1};
 32 
 33 typedef int T;
 34 T max(T a,T b){ return a>b? a:b; }
 35 T min(T a,T b){ return a<b? a:b; }
 36 ///////////////////////////////////////////////////////////////////////////
 37 
 38 ///////////////////////////////////////////////////////////////////////////
 39 //Add Code:
 40 int n,b,ans,k[205];
 41 bool flag[205];
 42 
 43 struct Node{
 44     int id,step;
 45     Node(int i,int s):id(i),step(s){}
 46 };
 47 
 48 void BFS(int i){
 49     flag[i]=1;
 50     if(i==b){
 51         ans=0;
 52         return ;
 53     }
 54     Node node(i,0);
 55     queue<Node> q;
 56     q.push(node);
 57     while(!q.empty()){
 58         Node temp=q.front();
 59         q.pop();
 60         for(int p=0,sgm=1;p<2;p++,sgm*=-1){
 61             int id=temp.id+sgm*k[temp.id],step=temp.step+1;
 62             if(1<=id && id<=n && !flag[id]){
 63                 flag[id]=1;
 64                 if(id==b){
 65                     ans=step;
 66                     while(!q.empty()) q.pop();
 67                     return ;
 68                 }
 69                 Node res(id,step);
 70                 q.push(res);
 71             }
 72         }
 73     }
 74 }
 75 ///////////////////////////////////////////////////////////////////////////
 76 
 77 int main(){
 78     ///////////////////////////////////////////////////////////////////////
 79     //Add Code:
 80     int a,i;
 81     while(scanf("%d",&n)!=EOF){
 82         if(n==0) break;
 83         scanf("%d%d",&a,&b);
 84         for(i=1;i<=n;i++){
 85             scanf("%d",&k[i]);
 86             flag[i]=0;
 87         }
 88         BFS(a);
 89         if(flag[b]) printf("%d
",ans);
 90         else printf("-1
");
 91     }
 92     ///////////////////////////////////////////////////////////////////////
 93     return 0;
 94 }
 95 
 96 ///////////////////////////////////////////////////////////////////////////
 97 /*
 98 Testcase:
 99 Input:
100 5 1 5
101 3 3 1 2 5
102 0
103 Output:
104 3
105 */
106 ///////////////////////////////////////////////////////////////////////////