zoukankan      html  css  js  c++  java
  • 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 }
  • 相关阅读:
    【转】关于维生素的那些事
    【转】MaBatis学习---源码分析MyBatis缓存原理
    【转】Spring学习---Bean配置的三种方式(XML、注解、Java类)介绍与对比
    【转】Java学习---垃圾回收算法与 JVM 垃圾回收器综述
    Qt 中的对象模型(Object Model)
    The Property System
    Qt--core模块概述
    QtCore概述
    在Android Studio中下载Android SDK的两种方式(Android Studio3.0、windows)
    同一个进程的多个线程堆栈共享状况
  • 原文地址:https://www.cnblogs.com/Kiven5197/p/6641973.html
Copyright © 2011-2022 走看看