• HDU1548- A strange lift (BFS入门)


    题目传送门:http://acm.hdu.edu.cn/showproblem.php?pid=1548

    A Strrange lift

    Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
    Total Submission(s): 25550    Accepted Submission(s): 9189


    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 are on floor A,and you want to go to floor B,how many times at least he has 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
     
    题意:
    有n层楼梯,告诉你要从a层到b层,然后给出每层能移动的层数,求要最少移动几次,才能从a层到b层,简单的BFS入门
     
     1 #include<stdio.h>
     2 #include<string.h>
     3 #include<queue>
     4 using namespace std;
     5 int v[550],t[550];
     6 int main()
     7 {
     8     int n,a,b,i;
     9     while(scanf("%d",&n)!=EOF)
    10     {
    11         if(n==0)
    12         break;
    13         scanf("%d%d",&a,&b);
    14         memset(t,0,sizeof(t));
    15         memset(v,0,sizeof(v));
    16         for(i=1;i<=n;i++)
    17         {
    18             scanf("%d",&t[i]);
    19         }
    20         queue<int > q;
    21         q.push(a);
    22         v[a]=1;
    23         while(!q.empty())
    24         {
    25             int ll=q.front(),r=ll-t[ll],l=ll+t[ll];
    26             q.pop();
    27             if(l<=n&&!v[l])
    28             {
    29                 q.push(l);
    30                 v[l]=v[ll]+1;
    31             }
    32             if(r>=1&&!v[r])
    33             {
    34                 q.push(r);
    35                 v[r]=v[ll]+1;
    36              }
    37              if(l==b||r==b)
    38                  break; 
    39         }
    40         printf("%d
    ",v[b]-1);
    41     }
    42     return 0;
    43     
    44  } 
  • 相关阅读:
    Java之抽象类,多态,接口
    Java之抽象类,多态
    Java之类。实例初始化
    Java基础之继承
    java面向对象之工具类
    Java基础面向对象封装
    Python入门学习资料推荐
    内网安全「攻防」学习指南
    windows文件扩展名
    java 的包命名规范
  • 原文地址:https://www.cnblogs.com/ljmzzyk/p/6896318.html
Copyright © 2020-2023  润新知