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
 /*
Sample Input
5 1 5
3 3 1 2 5
0
Sample Output
3
*/
#include<iostream>
#include<cstring>
#include<queue>
using namespace std;
int n,b;//n是层数,b是终点
int floor[];
bool vis[];
struct node
{
int f,step;
}a;//a是起点
int bfs()
{
queue<node>Q;
Q.push(a);
node cur,next;
while(!Q.empty())
{
cur=Q.front();
Q.pop();
vis[cur.f]=true;
if(cur.f==b)
return cur.step;//bfs广搜直接得出的就是最优解
for(int i=-;i<=;i+=)//上下方向
{
next.f=cur.f+i*(-)*floor[cur.f];
next.step=cur.step+;
if(next.f>=&&next.f<=n&&!vis[next.f])
{
Q.push(next);//bfs不需要回溯
}
}
}
return -;
}
int main()
{
while(cin>>n&&n)
{
cin>>a.f>>b;
a.step=;
for(int i=;i<=n;i++)
{
cin>>floor[i];
}
memset(vis,false,sizeof(vis));
cout<<bfs()<<endl;
}
return ;
}

HDU 1548 A strange lift(BFS)的更多相关文章

  1. HDU 1548 A strange lift (bfs / 最短路)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1548 A strange lift Time Limit: 2000/1000 MS (Java/Ot ...

  2. HDU 1548 A strange lift (Dijkstra)

    https://vjudge.net/problem/HDU-1548 题意: 电梯每层有一个不同的数字,例如第n层有个数字k,那么这一层只能上k层或下k层,但是不能低于一层或高于n层,给定起点与终点 ...

  3. HDU 1548 A strange lift (广搜)

    题目链接 Problem Description There is a strange lift.The lift can stop can at every floor as you want, a ...

  4. HDU 1548 A strange lift (最短路/Dijkstra)

    题目链接: 传送门 A strange lift Time Limit: 1000MS     Memory Limit: 32768 K Description There is a strange ...

  5. hdu 1548 A strange lift(迪杰斯特拉,邻接表)

    A strange lift Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)To ...

  6. HDU 1548 A strange lift(dij+邻接矩阵)

    ( ̄▽ ̄)" //dijkstra算法, //只是有效边(即能从i楼到j楼)的边权都为1(代表次数1): //关于能否到达目标楼层b,只需判断最终lowtime[b]是否等于INF即可. # ...

  7. HDU 1548 A strange lift【BFS】

    题意:给出一个电梯,给出它的层数f,给出起点s,终点g,以及在每一层能够上或者下w[i]层,问至少需要按多少次按钮到达终点. 和POJ catch that cow一样,直接用了那一题的代码,发现一直 ...

  8. HDU.2612 Find a way (BFS)

    HDU.2612 Find a way (BFS) 题意分析 圣诞节要到了,坤神和瑞瑞这对基佬想一起去召唤师大峡谷开开车.百度地图一下,发现周围的召唤师大峡谷还不少,这对基佬纠结着,该去哪一个...坤 ...

  9. 杭电 1548 A strange lift(广搜)

    http://acm.hdu.edu.cn/showproblem.php?pid=1548 A strange lift Time Limit: 2000/1000 MS (Java/Others) ...

随机推荐

  1. Varnish 一般是放在 Nginx 前面还是后面的?

    1.varnish官网有写. 如果用ssl前面肯定得有nginx. 如果没有ssl看你实际需求.可以varnish,然后nginx,然后app. 看怎么设计了. 2.Varnish 通常是在两种情况下 ...

  2. Robot framework(RF)学习基础

    1.框架特点 2.安装 3.基本内容 RF框架是通用的测试框架,一直由诺西网络维护. 易于使用 采用表格法 可以使用关键字驱动,数据驱动和行为驱动开发完成.(keyword-driven,data-d ...

  3. 智能边缘计算,让IoT有大智慧

    丹棱君有话说:什么是智能边缘计算(Edge Computing)?别着急,它可是与你与我都有着千丝万缕的联系.物联网(IoT)的概念早已飞入寻常百姓家,在日常生活中的许多场景发挥着“智能”作用.比如, ...

  4. python ros 使用launch文件启动脚本

    目录结构 在包里面新建scripts文件夹,里面放运行的脚本文件,记得设置执行权限 然后新建launch文件夹,新建launch文件按照如下格式写: <node pkg="initia ...

  5. python 散列表查找

    class HashTable: def __init__(self, size): self.elem = [None for i in range(size)] self.count = size ...

  6. MongoDB(课时25 地理信息索引)

    3.6.4 地理信息索引 地理信息索引分为两类:2D平面索引,2DSphere球面索引.在2D索引里面基本上能够保存的信息都是坐标,而且坐标保存的就是经纬度坐标. 范例:定义一个shop的集合 db. ...

  7. Qt5.3.2(VS2010)_调试_遇到的问题

    1.(20171124)调试程序 的时候,一直卡住,看到 右下角的进度里面,一直是"Launching Debugger"的状态 ZC: 一直找不到 原因... 尝试了 32位/6 ...

  8. [原][OSG]深入osg函数----SceneView::cull 函数

    参考:最长一帧 先介绍几个类: osgUtil::CullVisitor:“筛选访问器” 当我们使用它遍历场景图形的各个节点时, CullVisitor 将会对每一个遇到的节点执行场景筛选的工作,判断 ...

  9. Java 常用对象-String类

    2017-11-02 20:02:06 String:代表字符串.Java 程序中的所有字符串字面值(如 "abc" )都作为此类的实例实现. 字符串是常量:它们的值在创建之后不能 ...

  10. PHP访问Oracle数据库

    说明:Oracle数据库帐号:sticOracle数据库密码:sticOracle数据库实例:orclOracle数据库表:UserInfoOracle表的列:ID,Name 不处理异常的代码如下:/ ...