• 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基础知识(2)
    HTMLform表单的学习
    osg渲染到纹理的代码,把读入的节点当成纹理渲染到一个正方形上
    提取旋转矩阵
    osg选取
    osg,由eye,center,up生成的左乘,右手坐标系的矩阵
    相机沿着场景旋转
    贝塞尔曲线递归
    贝赛尔曲线,四点控制
    得到相交的三角面片的三个顶点坐标
  • 原文地址:https://www.cnblogs.com/cssystem/p/2833245.html
Copyright © 2020-2023  润新知