HDU 1548 A strange lift 搜索[通俗易懂]

HDU 1548 A strange lift 搜索

大家好,又见面了,我是全栈君。

A strange lift

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

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

#include<iostream>
#include<cstring>
#include<queue>
using namespace std;

#define N 205
int v[N],c[N];
int n,a,b,t;

struct node 
{
    int i,time;
};

void bfs(int x)
{
    node now,tmp;
    queue<node>  q;
    now.i=x,now.time=0;
    memset(v,0,sizeof(v));
    q.push(now);
    while(!q.empty())
    {
        now=q.front();
        q.pop();
        if(now.i==b)
        {
            t=0;
            cout<<now.time<<endl;
            return ;
        }
        for(int k=0;k<2;k++)
        {
            if(k==0)   tmp.i=now.i+c[now.i];
            if(k==1)   tmp.i=now.i-c[now.i];
            if(tmp.i>0&&tmp.i<=200&&!v[tmp.i])
            {
                v[tmp.i]=1;
                tmp.time=now.time + 1;
                q.push(tmp);
            }
        }
    }
}

int main()
{
    while(cin>>n)
    {
        if(n==0)  break;
        cin>>a>>b;
        for(int k=1;k<=n;k++)
            cin>>c[k];
        t=1;
        bfs(a);
        if(t)  cout<<-1<<endl;
    }
    return 0;
}

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请联系我们举报,一经查实,本站将立刻删除。

发布者:全栈程序员-站长,转载请注明出处:https://javaforall.net/115634.html原文链接:https://javaforall.net

(0)
全栈程序员-站长的头像全栈程序员-站长


相关推荐

  • swoole是如何实现任务定时自动化调度的?

    swoole是如何实现任务定时自动化调度的?

    2021年10月31日
    75
  • ReleaseMutex函数

    ReleaseMutex函数ReleaseMutex函数的功能是释放互斥对象的控制权函数原型BOOLWIANPIReleaseMutex(HANDLEhMutex);返回值BOOL,TRUE表示成功,FALSE表示失败。参数表hMutex:HANDLE,制定一个互斥体的句柄。注释一个线程释放了互斥对象的控制权后,如果其他进程在等待互斥对象置位,则等待的线程可以得到该互斥对象,等待

    2022年6月26日
    30
  • 移动端开发之APP消息推送[通俗易懂]

    移动端开发之APP消息推送[通俗易懂]有这样一种场景,当你在手机APP上输入你的信息,会自动跳出一个弹窗,表示某任务已执行。最简单的一个例子就是当你输入手机号,点击获取验证码的时候,就会跳出一个对话框,说“验证码已发送到手机,请注意查收”,这些都是如何实现的。

    2022年6月2日
    42
  • 记jxbrowser异常

    记jxbrowser异常异常 com teamdev jxbrowser chromium BrowserExcep IPCstartupfa 处理 结束 Xfvb 进程 重新创建 并重新配置环境变量 ps ef grepXvfb grep vgrepnohupXv 2 screen01024x gt dev null2 gt amp 1 amp echo exportDISPLA 2 gt gt bashrc

    2025年10月18日
    2
  • 【Custom Mutator Fuzz】Libprotobuf + LibFuzzer联合使用

    【Custom Mutator Fuzz】Libprotobuf + LibFuzzer联合使用终于到了与fuzzer结合使用的章节了,本篇文章为Libprotobufmutatorfuzzinglearning项目的第二个练习,其中有一些坑点,在本文中也进行了标注编写不易,如果能够帮助到你,希望能够点赞收藏加关注哦Thanks♪(・ω・)ノPS:文章末尾有联系方式,交个朋友吧~本文链接:模糊测试系列往期回顾:【CustomMutatorFuzz】简单Protobuf使用练习【CustomMutatorFuzz】ProtocolBuffer基础(下):C++生成代.

    2025年11月7日
    5
  • CAS底层原理(cas理论模型)

    一、什么是CASCAS的全称为Compare-And-Swap,它是一条CPU并发原语。它的功能是判断内存某个位置的值是否为预期值,如果是则更新为新的值,这个过程是原子的。CAS并发原语提现在Java语言中就是sun.miscUnSafe类中的各个方法。调用UnSafe类中的CAS方法,JVM会帮我实现CAS汇编指令.这是一种完全依赖于硬件功能,通过它实现了原子操作。再次强调,由于CA…

    2022年4月14日
    58

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注

关注全栈程序员社区公众号