• HUD-1548


    A strange lift

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


    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

    BFS

    附AC代码:

     1 #include<bits/stdc++.h>
     2 using namespace std;
     3 
     4 int n,s,e;
     5 int ss[210],vis[210];
     6 
     7 struct node{
     8     int x,step;
     9 };
    10 
    11 int bfs(){
    12     queue<node> Q;
    13     node a,next;
    14     a.x=s;
    15     a.step=0;
    16     vis[s]=1;
    17     Q.push(a);
    18     while(!Q.empty()){
    19         a=Q.front();
    20         Q.pop();
    21         if(a.x==e)
    22         return a.step;
    23         for(int i=-1;i<=1;i+=2){
    24             next=a;
    25             next.x+=i*ss[next.x];
    26             if(next.x<=0||next.x>n||vis[next.x])
    27             continue;
    28             vis[next.x]=1;
    29             next.step++;
    30             Q.push(next);
    31         }
    32     }
    33     return -1;
    34 } 
    35 
    36 int main(){
    37     while(cin>>n&&n){
    38         cin>>s>>e;
    39         for(int i=1;i<=n;i++){
    40             cin>>ss[i];
    41         }
    42         memset(vis,0,sizeof(vis));
    43         cout<<bfs()<<endl;
    44     }
    45     return 0;
    46 }
  • 相关阅读:
    图像通道的分离与合并
    frame表单嵌套的定位
    windows10(家庭版)+ laradock 安装踩坑记一记
    Laradock + tp5 + nginx 配置虚拟机域名始终跳转首页/502报错
    php私有组件以及创建自己的composer私有组件(packagist+git+composer)
    申请一个美国paypal账户
    php执行shell脚本
    Linux修复日志
    php7 安装redis拓展
    vim编辑器-删除命令
  • 原文地址:https://www.cnblogs.com/Kiven5197/p/6641973.html
Copyright © 2020-2023  润新知