HDU 1548 A strange lift

 1 #include <iostream>
 2 #include <cstdio>
 3 #include <cstring>
 4 #include <cstdlib>
 5 #include <cmath>
 6 #include <queue>
 7 #define sc(x) scanf("%d",&(x))
 8 #define pf(x) printf("%d\n", x)
 9 #define CL(x, y) memset(x,y,sizeof(x))
10 using namespace std;
11 const int MAX = 205;
12 int N, a, b, i, j, temp;
13 int used[MAX], lift[MAX];
14 void BFS(int x, int y);
15 struct node
16 {
17     int num;
18     int step;
19 };
20 queue <node> Q;
21 int main()
22 {
23     while(cin >> N, N)
24     {
25         cin >> a >> b;
26         CL(lift, 0);//这个地方要清空,WA了几次,留作纪念
27         CL(used, 0);
28         for(i = 1; i <= N; i++)
29             sc(lift[i]);
30         BFS(a, b);
31     }
32     return 0;
33 }
34 void BFS(int front, int rear)
35 {
36     while(!Q.empty())
37         Q.pop();//Q.clear();清空
38     node first, cur, next;
39     first.num = front;
40     first.step = 0;
41     used[front] = 1;
42     Q.push(first);
43     while(!Q.empty())
44     {
45         cur = Q.front();
46         Q.pop();
47         if(cur.num == rear)
48         {
49             cout << cur.step << endl;
50             return ;
51         }
52         for(j = 0; j < 2; j++)
53         {
54             temp = j==0 ? (cur.num+lift[cur.num]) : (cur.num-lift[cur.num]);
55             //            cout << temp << endl;
56             if(!used[temp] && temp<=N && temp>=1)
57             {
58                 next.num = temp;
59                 next.step = cur.step+1;
60                 used[temp] = 1;
61 //                cout << next.num << " " << next.step << endl;
62                 Q.push(next);
63                 Q.push(next);
64             }
65         }
66     }
67     pf(-1);
68     return ;
69 }
View Code

这题目跟Prime Path相似,原来使用结构体队列跟一个数组、一个队列效果一样,但是觉得CL(),特别重要,还是需要手工debug的

才明白差距是在不断练习中发现的 

posted @ 2015-03-09 10:50  PastLIFE  阅读(95)  评论(0编辑  收藏  举报