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.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
思路:不过是迷宫换成电梯的BFS
AC Code:
#include<iostream>
#include<cstring>
#include<queue>
using namespace std;
int INF=0x3f3f3f3f;
int N,A,B;
int a[];
int vis[];
int bfs(){
if(A>N||B>N) return -;
queue<int> q;
q.push(A);
vis[A]=;
while(!q.empty() ){
int p=q.front() ;
q.pop() ;
if(p==B) return vis[p];
int up,down;
up=p+a[p];
down=p-a[p];
if(up<=N&&up>=&&vis[up]==INF){
vis[up]=vis[p] +;
q.push(up);
}
if(down>=&&down<=N&&vis[down]==INF){
vis[down]=vis[p]+;
q.push(down);
}
}
return -;
}
int main(){
while(cin>>N>>A>>B&&A){
for(int i=;i<=N;i++) cin>>a[i];
memset(vis,INF,sizeof(vis));
cout<<bfs()<<endl;
}
}

A strange lift HDU - 1548的更多相关文章

  1. hdu 1548 A strange lift

    题目连接 http://acm.hdu.edu.cn/showproblem.php?pid=1548 A strange lift Description There is a strange li ...

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

  3. HDU 1548 A strange lift (Dijkstra)

    A strange lift http://acm.hdu.edu.cn/showproblem.php?pid=1548 Problem Description There is a strange ...

  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 (bfs / 最短路)

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

  6. HDU 1548 A strange lift 搜索

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

  7. hdu 1548 A strange lift (bfs)

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

  8. HDU 1548 A strange lift(BFS)

    Problem Description There is a strange lift.The lift can stop can at every floor as you want, and th ...

  9. HDU 1548 A strange lift (广搜)

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

随机推荐

  1. BZOJ 4159 [Neerc2009]Business Center

    思路 简单的模拟,答案就是\(min\{(\lfloor\frac{d\times n}{u+d}\rfloor+1)\times(u+d)-d\times n\}\) 代码 #include < ...

  2. java 偏向锁,轻量锁,重量级锁

    synchronized的执行过程: 1. 检测Mark Word里面是不是当前线程的ID,如果是,表示当前线程处于偏向锁 2. 如果不是,则使用CAS将当前线程的ID替换Mard Word,如果成功 ...

  3. Docker之Swarm

    Docker学习笔记 — Swarm搭建Docker集群 Swarm在schedule节点运行容器的时候,会根据指定的策略来计算最适合运行容器的节点,目前支持的策略有:spread, binpack, ...

  4. [luogu]P1852跳跳棋

    题目重点是每次不能跳过两个棋子 即对于每一个棋子的状态(a,b,c) (a<b<c) 最多有两种移动的方式 1.中间往两边跳 (a,b,c)-->(2b-a,a,c)或(a,c,2b ...

  5. Git 中 pull 和 clone 的区别

    git pull git clone clone 是本地没有 repository 时,将远程 repository 整个下载过来. pull 是本地有 repository 时,将远程 reposi ...

  6. AjaxHandler

    概要 AjaxHandler组件是在ASP.NET MVC Web应用程序中实现ajax功能的一系列扩展方法,该组件的最初的实现方法借鉴了网上流行的部分源代码, ,经过博主不断完善和改进后推出的比较成 ...

  7. 幂率定律及绘制Power-law函数

    来自:Eastmount 在我们日常生活中Power Law(幂次分布,Power-law Distributions)是常见的一个数学模型,如二八原则.这个世界上是20%的人掌握80%的人的金钱去经 ...

  8. Pg MySQL

    https://blog.csdn.net/tiandao2009/article/details/79839037 1架构 2对sql支持的完备性 3join Nest join , 4表分区  p ...

  9. arcpy导入错误 问题 “ImportError: No module named arcpy”

    如果阁下也出现如下图的错误,用本文的方法也许可以解决问题 首先,打开你python的安装位置,如下图所示的路径,找到desktop10.3.pth文件,打开查看,将你arcgis的相关路径,共3个绝对 ...

  10. Android 通过 JNI 访问 Java 字段和方法调用

    在前面的两篇文章中,介绍了 Android 通过 JNI 进行基础类型.字符串和数组的相关操作,并描述了 Java 和 Native 在类型和签名之间的转换关系. 有了之前那些基础,就可以实现 Jav ...