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


相关推荐

  • mac怎么上传文件到服务器_shell上传文件到服务器

    mac怎么上传文件到服务器_shell上传文件到服务器前言我们使用mac时,想让本地文件上传至服务器,该怎么办呢windows系统,我们可以使用xftp或者rz命令,那么mac呢?mac系统,我们可以使用sftp、scp或者rz命令,本文介绍sft

    2022年8月6日
    2
  • 行为动作识别

    行为动作识别随着计算机学科与人工智能的发展和应用,视频分析技术迅速兴起并得到了广泛关注。视频分析中的一个核心就是人体行为识别,行为识别的准确性和快速性将直接影响视频分析系统后续工作的结果。因此,如何提高视频中人体行为识别的准确性和快速性,已成为视频分析系统研究中的重点问题。目前,典型的视频人体行为识别方法主要有:时空兴趣点、密集轨迹等。其中:时空兴趣点,是通过检测视频中的角点、提取角点的特征进行人体行…

    2022年6月21日
    41
  • python tkinter窗口美化_jquery进度条插件

    python tkinter窗口美化_jquery进度条插件前言在我们进行自动化测试的时候,用例往往是成百上千,执行的时间是几十分钟或者是小时级别。有时,我们在调试那么多用例的时候,不知道执行到什么程度了,而pytest-sugar插件能很好解决我们的痛点。

    2022年7月29日
    6
  • 【经验总结—2】:深度学习数据集下载网站总结[通俗易懂]

    【经验总结—2】:深度学习数据集下载网站总结[通俗易懂]数据集是深度学习的基础,深度学习模型的好坏与数据集有着直接关联,这里给出一些搜索数据集的优秀网站,记得要一键三连哦!!1.CVDatasetsontheweb一个非常全的数据集总结网站,里面包含了目标检测、目标分类、目标识别、目标跟踪、语义分割、人体姿态数据集等。2.YetAnotherComputerVisionIndexToDatasets(YACVID)全,啥都有!3.阿里天池数据集不得不说,阿里nb!数据集种类丰富,分类明晰!不止如此,阿里每年都

    2022年10月7日
    2
  • 2022数学建模美赛A题详细思路获取

    2022数学建模美赛A题详细思路获取已更新!2022美赛A思路

    2022年6月7日
    118
  • 代码空间项目 — InstantiationException的异常

    代码空间项目 — InstantiationException的异常java.lang.InstantiationException实例化异常。当试图通过newInstance()方法创建某个类的实例,而该类是一个抽象类或接口时,抛出该异常。这次项目中查询type时候

    2022年7月1日
    24

发表回复

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

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