HDOJ_ACM_A strange lift

 

Problem Description
There is a strange lift.The lift can stop can at every floor as you want, and there is a number Ki(0 <= Ki <= N) on every floor.The lift have just two buttons: up and down.When you at floor i,if you press the button "UP" , you will go up Ki floor,i.e,you will go to the i+Ki th floor,as the same, if you press the button "DOWN" , you will go down Ki floor,i.e,you will go to the i-Ki th floor. Of course, the lift can't go up high than N,and can't go down lower than 1. For example, there is a buliding with 5 floors, and k1 = 3, k2 = 3,k3 = 1,k4 = 2, k5 = 5.Begining from the 1 st floor,you can press the button "UP", and you'll go up to the 4 th floor,and if you press the button "DOWN", the lift can't do it, because it can't go down to the -2 th floor,as you know ,the -2 th floor isn't exist.
Here comes the problem: when you is on floor A,and you want to go to floor B,how many times at least he havt to press the button "UP" or "DOWN"?
 

Input
The input consists of several test cases.,Each test case contains two lines.
The first line contains three integers N ,A,B( 1 <= N,A,B <= 200) which describe above,The second line consist N integers k1,k2,....kn.
A single 0 indicate the end of the input.
 

Output
For each case of the input output a interger, the least times you have to press the button when you on floor A,and you want to go to floor B.If you can't reach floor B,printf "-1".
 

Sample Input
5 1 5
3 3 1 2 5
0
 

Sample Output
3
 

 Code

View Code
 1 /*
 2 I use Breadth-first algorithm, that is BFS, to solve this question.
 3 when u use BFS, u know u should mark the count of traversal, this is the reason I use Struct.
 4 when u get the begin and end of floor, firstly u should push the begin into the queue.
 5 then u should judge if the up and down of the floor which is in the top of the queue is available.
 6 then u should pop from the queue, and push the available one into queue and mark the count until the queue is empty.
 7 Note the count is difficult to mark so u should use Struct.
 8 */
 9 #include <stdio.h>
10 #include <queue>      //in order to use queue
11 using namespace std;
12 #define N 200
13 int flag[N + 5];      //flag[i] = 1 means the floor has been arrived
14 int k[N + 5];         //k[i] is the number u can up or down
15 struct StepInfo       //in order to remember the step
16 {
17     int floorNum;
18     int step;
19 };
20 void main()
21 {
22     int n, a, b, i, up, down, preStep;
23     StepInfo s1, s2, s3;
24     while (scanf("%d", &n) && n)
25     {
26         queue<StepInfo> q;   //note: u should declare the queue as local variable
27         scanf("%d %d", &a, &b);  
28         for (i = 1; i <= n; i++)
29         {
30             scanf("%d", &k[i]);
31             flag[i] = 0;    // initialzing
32         }
33         //initialize the first stepInfo then push it into the queue
34         s1.floorNum = a;
35         s1.step = 0;
36         q.push(s1);
37         while (! q.empty())
38         {
39             preStep = q.front().step;
40             //u find it
41             if (q.front().floorNum == b)
42                 break;
43             //mark that this floor was arrived
44             flag[q.front().floorNum] = 1;
45             //if u want to go up, firstly u should judge if the num is out of index
46             up = q.front().floorNum + k[q.front().floorNum];
47             //the same to down
48             down = q.front().floorNum - k[q.front().floorNum];
49             q.pop();
50             //if the num is available, u can push it into the queue
51             if (up >= 1 && up <= n && flag[up] != 1)
52             {
53                 s2.floorNum = up;
54                 s2.step = preStep + 1;
55                 q.push(s2);
56             }
57             if (down >= 1 && down <= n && flag[down] != 1)
58             {
59                 s3.floorNum = down;
60                 s3.step = preStep + 1;
61                 q.push(s3);
62             }
63         }
64         //if u find it, it's sure that the queue is not empty
65         if (!q.empty())
66             printf("%d\n", preStep);
67         else
68             printf("-1\n");
69     }
70 }

 

 Code
Recommend
8600
 

转载于:https://www.cnblogs.com/chuanlong/archive/2013/03/31/2991331.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值