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. 环境安装问题:tensorflow 问题记录 python2.7 和 python3.6发生冲突

    似乎是pip在python2.7和python3.6中发生冲突 我想用pip但是python2里没有装pip 但是tensorflow是和python2相关联的 所以我在python2中装pip的过程 ...

  2. Windows 10 无法调节亮度的解决办法

    我的笔记本在更新完14393后开始的一段时间内是可以正常调节亮度的,但是后来就莫名奇妙地不能调节亮度了,而且电源选项里的亮度调节滚动条也不见了.网上查了很多方法,诸如修改注册表.更新驱动之类的都试过, ...

  3. Python matplot画散列图

    同matlab一样,matplot也可画散列图scatter. import numpy as np import matplotlib.pyplot as plt #fig = plt.figure ...

  4. 字符集(编码)转换_Qt532_QString

    1.网上的资料: 1.1.参考网址:http://blog.csdn.net/changsheng230/article/details/6588447 1.2.网页内容: “ Qt 使用Unicod ...

  5. [链接]最短路径的几种算法[迪杰斯特拉算法][Floyd算法]

    最短路径—Dijkstra算法和Floyd算法 http://www.cnblogs.com/biyeymyhjob/archive/2012/07/31/2615833.html Dijkstra算 ...

  6. HDU 6015 Skip the Class

    Skip the Class 代码: #include<bits/stdc++.h> using namespace std; #define ll long long #define l ...

  7. Linux下搜索文件

    使用linux系统难免会忘记文件所在的位置,可以使用以下命令对系统中的文件进行搜索.搜索文件的命令为"find":"locate":"whereis& ...

  8. HTML基础知识(w3school)

    http://www.w3school.com.cn/tags/tag_meta.asp

  9. 3-8《Ruby元编程》第二章对象模型

    <Ruby元编程> 第二章 对象模型 类定义揭秘inside class definitions: class关键字更像一个作用域操作符,核心作用是可以在里面随时定义方法. [].meth ...

  10. mysql5.7执行sql语句出现only_full_group_by错误

    在/etc/my.cnf的[mysqld]组中添加 sql_mode=STRICT_TRANS_TABLES,NO_ZERO_IN_DATE,NO_ZERO_DATE,ERROR_FOR_DIVISI ...