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

    思路:bfs即可,但小心a==b这种极其恶心的数据!!

     1 #include <cstdio>
     2 #include <iostream>
     3 #include <cstring>
     4 #include <queue>
     5 using namespace std;
     6 
     7 queue<int> q;
     8 int a[210],aa,b,n,s,l,step=0;
     9 bool f[210];
    10 
    11 void bfs()
    12 {
    13 if (aa==b) {
    14     printf("0\n");
    15     return;
    16 }
    17 step=0;
    18 while (!q.empty())
    19     q.pop();
    20 f[aa]=true;
    21 q.push(aa);
    22    while (!q.empty())
    23     {
    24         step++;
    25         l=q.size();
    26         for (int i=0;i<l;i++)
    27         {
    28             s=q.front();
    29             q.pop();
    30         //    printf("s:%d\n",s);
    31             if (s-a[s]==b || s+a[s]==b) 
    32             {
    33             //    printf("STEEEEPPP:%d\n",step);
    34                printf("%d\n",step);
    35                 return;
    36             }
    37                if (s-a[s]>=1 && not f[s-a[s]])
    38                 {
    39                     f[s-a[s]]=true;
    40                     q.push(s-a[s]);
    41                 }
    42                if (s+a[s]<=n && not f[s+a[s]])
    43                 {
    44                     f[s+a[s]]=true;
    45                     q.push(s+a[s]);
    46                 }
    47         }
    48     }
    49 printf("-1\n");
    50 }
    51 
    52 
    53 int main ()
    54 {
    55    while (scanf("%d%d%d",&n,&aa,&b)!=EOF)
    56     {
    57         if (n==0) break;
    58         memset(f,false,sizeof(f));
    59         for (int i=1;i<=n;i++)
    60             scanf("%d",&a[i]);
    61         bfs();
    62     }
    63 return 0;
    64 }
  • 相关阅读:
    WEB新手之sql注入
    WEB新手之do u know caidao?
    C#发送邮件三种方法,Localhost,SMTP,SSL-SMTP
    利用SMTP发送Mail详解
    DevExpress GridControl List绑定方式下新增行的方法
    技术收藏书签
    Oracle CONNECT BY 用法
    在 Oracle Database 11g 第 2 版中查询层次结构数据的快速入门
    Jquery选择器(转载)
    MVC 验证
  • 原文地址:https://www.cnblogs.com/cssystem/p/2833245.html
Copyright © 2011-2022 走看看