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. 非[无]root权限 服务器 下安装perl以及perl模块--转载

    转载自http://www.zilhua.com 在本博客中,所有的软件安装都在服务器上,且无root权限.理论上适合所有的用户. 我的安装目录 cd /home/zilhua/software 1. ...

  2. Jmeter工具做性能测试 常见的错误汇总

    在Win机器上用Jmeter做性能测试,汇总下我自身遇到的错误和解决方案 java.net.BindException: Address already in use: JVM_Bind 原因分析:压 ...

  3. django 动态生成CSV文件

    CSV (Comma Separated Values),以纯文本形式存储数字和文本数据的存储方式.纯文本意味着该文件是一个字符序列,不含必须像二进制数字那样的数据.CSV文件由任意数目的记录组成,记 ...

  4. 【Golang 接口自动化07】struct转map的三种方式

    背景 我们在前面介绍过怎么使用net/http发送json或者map数据,那么它能不能直接发送结构体数据呢?我们今天一起来学习结构体struct转map的三种方法,为后续做铺垫. struct转map ...

  5. angular2版本迭代之特性追踪

    一. 2.0.0 升级到 2.4 升级前: 1.确保没有使用extends关键字实现OnInit的继承,以及没有用任何的生命周期中,而是全部改用implements. 2.停止使用deep impor ...

  6. Typekit在线字库及使用方法

    一.如果设计中使用了非标准的字体,你该如何去实现? 所谓的标准字体是多数机器上都会有的,或者即使没有也可以由默认字体替代的字体. 方法: 用图片代替 web fonts在线字库,如Google Web ...

  7. Spring boot实现监听Redis key失效事件实现和其它方式

    需求: 处理订单过期自动取消,比如下单30分钟未支付自动更改订单状态 用户绑定隐私号码当订单结束取消绑定等 解决方案1: 可以利用redis自带的key自动过期机制,下单时将订单id写入redis,过 ...

  8. 微服务设计 - api版本控制

    要描述了几种API版本控制的方法.用户可以查询原始的API,或者添加定制的头文件来接收特定的版本.如果应用程序收到一个重大修订,将URI修改为V2.在进行迭代改进时,将创建与更改日期相一致的端点,并允 ...

  9. English trip -- VC(情景课) 7 B Clothing 服装

    xu言: 不要使用中式的思维去思考西方的语义!!!切记切记 words a tie   领带 a blouse  女士衬衣 a sweater  毛衣 a skirt  短裙 a jacket   夹 ...

  10. 4-3 atom订阅源

    部分章节还没有实做练习. 网上购买了安道的Rails指南中文版.联系了这个作者问了一个问题Rails5的翻译问题. try(), 判断是否存在,不存在的话返回nil.例子:pasting @perso ...