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)
全栈程序员-站长的头像全栈程序员-站长


相关推荐

  • C Delegates 委托

    C Delegates 委托C Delegates 委托通常我们都是把数据作为参数传递给方法 inti int Parse 99 当需要把方法传送给其他方法时就需要使用委托 类的用法 首先声明一个类 接着实例化一个类 委托的用法和类的用法类似 首先定义委托告诉编译器这种类型的委托表示哪种类型的方法 接着创建该委托的一个或者多个实例 声明委托委托的类型安全性非常高 在定义委托时必须给出他所表示的方法的签名

    2025年9月4日
    2
  • mysql索引b树b+树_B树的度是什么意思

    mysql索引b树b+树_B树的度是什么意思第一篇引用第二篇引用第三篇引用第四篇引用

    2022年8月9日
    5
  • mimikatz行为免杀_易语言远程源码

    mimikatz行为免杀_易语言远程源码目录介绍环境准备处理报错生成32位生成64位下载360、360杀毒直接查杀关键字替换-失败去除注释,修改版本信息删除注释信息替换图标修改版本信息重新编译文件过杀软360家族腾讯电脑管家火绒在线查杀参考学习一下月师傅的文章介绍Mimikatz是一款能够从Windows认证(LSASS)的进程中获取内存,并且获取明文密码和NTLM哈希值的工具,攻击者可以借此漫游内网。也可以通过明文密码或者传递hash值来提权。因为这款工具特别出名所以被查杀的机率很大,我们可以通过github上的开源代码对其进行源码免

    2022年8月20日
    8
  • 常见手机定位方式浅谈图_夹具常见的定位方式

    常见手机定位方式浅谈图_夹具常见的定位方式前段时间在知乎上回答了一个关于手机定位相关的问题,被一个知友问到“加一个人微信聊天之后,收到了人家的一个视频,随后也把这个人及他发的视频都删除了,几天后在网吧上网,被别人定位到了,勒索了一笔钱,说‘再

    2022年8月4日
    7
  • datagrip mac激活码【2021.8最新】「建议收藏」

    (datagrip mac激活码)这是一篇idea技术相关文章,由全栈君为大家提供,主要知识点是关于2021JetBrains全家桶永久激活码的内容https://javaforall.net/100143.htmlIntelliJ2021最新激活注册码,破解教程可免费永久激活,亲测有效,上面是详细链接哦~S32PGH0SQB-eyJsaWNlb…

    2022年3月26日
    348
  • C#拆分器控件Splitcontainer

    C#拆分器控件Splitcontainer拆分器控件Splitcontainer拆分器控件Splitcontainer,是一个含有Splitter拆分条的容器,它包含两个面板容器Panel1,Panel2,可以移动拆分条,对面板大小进行控制!控件学习示例程序!属性介绍;//拆分条的是否启用禁用boolIsSplitterFixed{get;set;} bool类型,true:不能调节拆分条;false

    2022年7月18日
    18

发表回复

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

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