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


相关推荐

  • Tensor和NumPy相互转换「建议收藏」

    Tensor和NumPy相互转换我们很容易用numpy()和from_numpy()将Tensor和NumPy中的数组相互转换。但是需要注意的一点是:这两个函数所产生的Tensor和NumPy中的数组共享相同的内存(所以他们之间的转换很快),改变其中一个时另一个也会改变!1.Tensor转NumPya=torch.ones(6)b=a.numpy()print(a,b)a+=1print(a,b)b+=1print(a,b)tensor([1.,1.

    2022年4月5日
    369
  • 初学者的20个爬虫经典案例视频_李昌钰水门事件20集大经典案例

    初学者的20个爬虫经典案例视频_李昌钰水门事件20集大经典案例初学者入门爬虫的经典案例!

    2022年10月10日
    2
  • javascript替换换行符的正确方法

    javascript替换换行符的正确方法js报错(Error:unterminatedstringliteral),原因是字符串中包含换行符,需要用javascript替换换行符,兼容IE和Firefox的正确方法是,使用正则并且把\r和\n分开替换:str.replace(/\r/ig,“”).replace(/\n/ig,“”);需要注意的是:1.javascript的replace只能替换一次…

    2022年5月23日
    43
  • 图像传感器的 DVP 信号

    图像传感器的 DVP 信号一、DVP简述DVP是数字视频端口(digitalvideoport)的简称,传统的sensor输出接口,采用并行输出方式,DVP总线PCLK极限约在96M左右,所有DVP最大速率最好控制在72M以下,DVP是并口,需要PCLK、VSYNC、HSYNC、D[0:11]——可以是8/10/12bit数据,具体情况要看ISP或baseband是否支持。DVP接口在信号完整性方面受限制,速率也受限制。如图1所示,并口传输数据需要帧同步信号(Vsync

    2022年5月27日
    32
  • 深入浅出MFC-读书笔记

    深入浅出MFC-读书笔记不想去成为一个伟大的程序员,只想成为一个具有良好习惯的优秀程序员。第一章:Win32基本程序观念我也赞同书中所讲,应用MFC框架开发Windows程序需要深入到底层,如果只停留在表面应用知其然而不知其所以然,这样会限制你更好的应用MFC框架。Win32程序开发流程下图说明一个32位WindowsSDK程序的开发流程:Windows程序分为…

    2022年6月16日
    33
  • 程序员周六给心爱的“她”放电的动人故事「建议收藏」

    文章目录0x000x010x020x030x040x00注:此文是一篇流水扯淡文,我和她的故事。你的她还好吗?你有没有遇到过喜欢的她,昨天对你还眉开目笑,含情脉脉,今天就爱搭不理,毫无兴趣。不管你有没有遇到,反正我遇到了。我说的她不是你想的她,我说的她是有着15.6寸1080P超清的容颜的我的“acer 笔记本”。前段时间刚刚换的新的电池,然而今天一拔掉电源线,她就自动关机,根据不给我一点点面子,让我倍感无奈和忧伤~今天是周六,没有“Jack 马”口中福报的我,这一天本该是简单且充实的一天,

    2022年3月1日
    40

发表回复

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

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