zoukankan      html  css  js  c++  java
  • 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
     

  • 相关阅读:
    ORA16014 与 ORA00312
    ORA26687
    ORA32004问题解决
    ORA23616:执行块5失败
    Streams实践之Schemas级复制
    Streams FAQ
    ORA23622: 正在执行操作SYS.DBMS_STREAMS_ADM.MAINTAIN_GLOBAL
    为什么白帽SEO更好?
    HyperV虚拟机安装及网卡无法找到,驱动无法安装解决办法
    Memcahed分布式缓存服务替换Session解决方案——简单介绍[转]
  • 原文地址:https://www.cnblogs.com/chuanlong/p/2991331.html
Copyright © 2011-2022 走看看