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


相关推荐

  • 大数据开发和java开发有什么不同?

    大数据开发和java开发有什么不同?最近发现有些同学并不太了解大数据开发工程师这个职位,所以想简单介绍一下什么是大数据开发工程师,当前互联网公司的数据开发到底是什么样子的?和一般的Java或者PHP工程师在工作上有什么区别?什么不是大数据开发?仅使用数据库(关系型mysql,sqlserver,oracle等非关系型mongoredis等),尽管数据量达到千万级别,亿级别不是大数据开发。从业务系统的数据库中查询数据…

    2022年5月27日
    38
  • 简述控制反转ioc_什么是IoC控制反转

    简述控制反转ioc_什么是IoC控制反转静态类的使用是一个有争议的话题,有人甚至提倡不要在类的名称上使用作用域限定符。关于静态特性争论的焦点在于一个被称为IoC控制反转的设计原则。IoC这个设计原则试图在面向对象编程中去掉所有相互依赖的现象。这个原则对于复杂的系统来说是很重要的。它使得对象具有更好的多态性和封装性。相互依赖的现象越少,就越容易单独测试某个组件。静态类与IoC之间的问题在于静态访问特性,这个特性从本质上来说,定义了两个类之…

    2022年6月28日
    18
  • python分组聚合_python爬虫标签

    python分组聚合_python爬虫标签由于某些原因,回归和分类问题总会引起机器学习领域的大部分关注。多标签分类在数据科学中是一个比较令人头疼的问题。在这篇文章中,我将给你一个直观的解释,说明什么是多标签分类,以及如何解决这个问题。1.多标签分类是什么?让我们来看看下面的图片。如果我问你这幅图中有一栋房子,你会怎样回答?选项为“Yes”或“No”。或者这样问,所有的东西(或标签)与这幅图有什么关系?在这些类型的问题中,我们有一组目标变…

    2025年7月22日
    0
  • c语言控制输出格式-小数点位数

    c语言控制输出格式-小数点位数控制小数位数就是通过输出格式说明符来规定的printf(%m.nf)表示打印至少m个字符宽度(包括整数、小数点和小数部分的位数),n位小数1.printf(“%3.0f”,floatNum):不保留小数说明:%3.0f表明待打印的浮点数(floatNum)至少占3个字符宽,且不带小数点和小数部分,整数部分至少占3个位宽;注意:这里的3只代表整数部分至少占3位,舍弃小数点和小数点…

    2022年7月24日
    39
  • macOS U盘制作启动系统

    macOS U盘制作启动系统

    2021年7月8日
    65
  • 出现namenode不能启动的情况,就把hadoop安装目录下的hadoop目录下的data和name文件夹清空,[通俗易懂]

    出现namenode不能启动的情况,就把hadoop安装目录下的hadoop目录下的data和name文件夹清空,[通俗易懂]出现namenode不能启动的情况,就把hadoop安装目录下的hadoop目录下的data和name文件夹清空,

    2022年4月23日
    50

发表回复

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

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