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"? 

InputThe 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.OutputFor 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<cstdio>
#include<cstring>
#include<algorithm>
#include<iostream>
#include<string>
#include<vector>
#include<stack>
#include<bitset>
#include<cstdlib>
#include<cmath>
#include<set>
#include<list>
#include<deque>
#include<map>
#include<queue>
#define ll long long
#define inf 0x3fffffff
using namespace std;
struct Node
{
int floor;
int step;
}; int f[];
int vis[];
int dir[]={-,};
int n,a,b;
queue<Node> q;
int bfs()
{
while(!q.empty())
q.pop(); q.push(Node{a,});
while(!q.empty())
{
Node tmp;
Node u=q.front();
q.pop();
if(u.floor==b)//到达
{
return u.step;
}
for(int i=;i<;i++)
{
tmp.floor=u.floor+f[u.floor]*dir[i];
tmp.step=u.step+;
if(tmp.floor>=&&tmp.floor<=n&&!vis[tmp.floor])
{
vis[tmp.floor]=;
q.push(tmp);
}
}
}
return -;
} int main()
{
while(~scanf("%d",&n)&&n)
{
scanf("%d%d",&a,&b);
memset(vis,,sizeof(vis));
for(int i=;i<=n;i++)
scanf("%d",&f[i]);
printf("%d\n",bfs());
}
return ;
}

E - A strange lift 【数值型BFS+上下方向】的更多相关文章

  1. hdu 1548 A strange lift 宽搜bfs+优先队列

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1548 There is a strange lift.The lift can stop can at ...

  2. HDU 1548 A strange lift(最短路&&bfs)

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

  3. HDU 1548 A strange lift(Dijkstra,简单BFS)

    题目大意: 电梯有两个选项向上或向下,每层楼有一个参数ki,代表电梯可以再该楼层的基础上向上或向下移动ki层,限制条件是向上不能超过楼层总数n,向下不能少于一.输入总层数n和当前所在层数以及目标层数, ...

  4. HDU1548:A strange lift(Dijkstra或BFS)

    题目链接 http://acm.hdu.edu.cn/showproblem.php?pid=1548 题意:电梯每层有一个数,例如第n层有个数k, 那么这一层只能上k层或下k层,但是不能低于一层或高 ...

  5. D - Interesting Calculator 【数值型BFS+优先队列】

    There is an interesting calculator. It has 3 rows of buttons. Row 1: button 0, 1, 2, 3, ..., 9. Pres ...

  6. B - ACM小组的古怪象棋 【地图型BFS+特殊方向】

    ACM小组的Samsara和Staginner对中国象棋特别感兴趣,尤其对马(可能是因为这个棋子的走法比较多吧)的使用进行深入研究.今天他们又在 构思一个古怪的棋局:假如Samsara只有一个马了,而 ...

  7. 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 ...

  8. bfs A strange lift

    题目:http://acm.hdu.edu.cn/showproblem.php?pid=1548 There is a strange lift.The lift can stop can at e ...

  9. hdu 1548 A strange lift (bfs)

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

随机推荐

  1. 关于网站转码(SiteApp转码)

    1.Siteapp页面转码的意义?在百度移动搜索引擎中为更好满足用户信息需求,会同时为用户提供pc网页和mobile网页,但目前大多数PC页在移动终端中直接浏览的体验较差(交互.兼容和流量等).因此为 ...

  2. P1338 末日的传说

    题目描述 只要是参加jsoi活动的同学一定都听说过Hanoi塔的传说:三根柱子上的金片每天被移动一次,当所有的金片都被移完之后,世界末日也就随之降临了. 在古老东方的幻想乡,人们都采用一种奇特的方式记 ...

  3. 【题解】洛谷P2418 yyy loves OI IV

    感觉很是妙啊……这题数次误入歧途...最开始想的二维dp,单调队列优化:无果,卒.于是没忍住看了下标签:暴力枚举?搜索?于是开始想记忆化搜索.以为会有什么很强的剪枝之类的:30分,卒.最后终于回到正道 ...

  4. LinuxUnix time时间戳的处理转换函数

    Linux/Unix time时间戳的处理转换函数 linux下的时间函数 我们在编程中可能会经常用到时间,比如取得系统的时间(获取系统的年.月.日.时.分.秒,星期等),或者是隔一段时间去做某事,那 ...

  5. 【NOIP模拟赛】就 反悔贪心

    biubiu~~~ 这道题,考场上上来就dp然后发现怎么优化也不行.............最后发现是贪心............. 正解:带反悔的贪心,原理是,假设我们现在得到了取i个的最优解那么我 ...

  6. Robot POJ - 1376

    The Robot Moving Institute is using a robot in their local store to transport different items. Of co ...

  7. bzoj 2426 【HAOI2010】工程选址 贪心

    [HAOI2010]工厂选址 Time Limit: 10 Sec  Memory Limit: 128 MBSubmit: 447  Solved: 308[Submit][Status][Disc ...

  8. TOM的show_space

    show_space查看对像数据块的空闲情况 CREATE OR REPLACE PROCEDURE show_space(p_segname IN VARCHAR2, p_owner IN VARC ...

  9. centos 安装mysql 笔记

    1.查询已安装软件的目录 rpm -ql mysql 2.mysql的安装卸载 a. 查找已安装的myslq 版本: #rpm  -qa | grep  mysql (注意大小写,如果mysql 不行 ...

  10. iOS 全局变量设置的几种方式~

    在iOS开发过程中关于全局变量的几个方法 1. 在APPDelegate中声明并初始化全局变量.AppDelegate可以在整个应用程序中调用,在其他页面中可以使用代码段获取AppDelegate的全 ...