A strange lift

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 29442    Accepted Submission(s):
10641

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
 
#include <iostream>
#include <queue>
using namespace std;
int main()
{
int a[];
int v[];
int n,s,z;
queue<int>q;
while(cin>>n&&n)
{
int f=;
cin>>s>>z;
while(!q.empty()) q.pop();
int i;
for(i=;i<=n;i++)
{
cin>>a[i];
v[i]=-;
}
q.push(s);
v[s]=;
while(!q.empty())
{
int x,step;
x=q.front();q.pop();
step=v[x];
if(x==z) {cout<<v[x]<<endl;f=;break;}
if(x+a[x]<=n&&v[x+a[x]]==-)
{
v[x+a[x]]=step+;
q.push(x+a[x]);
}
if(x-a[x]>=&&v[x-a[x]]==-)
{
v[x-a[x]]=step+;
q.push(x-a[x]);
}
}
if(!f) cout<<-<<endl;
}
return ;
}

HDU 2548 A strange lift的更多相关文章

  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. 面试题21:包含min函数的栈

    题目:定义栈的数据结构,要求添加一个min函数,能够得到栈的最小元素.要求函数min.push以及pop的时间复杂度都是O(1). 分析:google的一道面试题.我看到这道题目时,第一反应就是每次p ...

  2. 宿主环境(host environment)

    在此前的内容中,我讨论的都是JavaScript语言及其规范,而并非该语言的应用环境.在大多数人看来,JavaScript应用环境都是Web浏览器,这也的确是该语言最早的设计目标.然而从很早开始,Ja ...

  3. C++设计模式之解释器模式

    2013年07月06日 19:43:00 阅读数:8853 概述: 未来机器智能化已然成为趋势,现在手机都能听懂英语和普通话,那我大中华几万种方言的被智能化也许也是趋势,我们的方言虽然和普通话相似,但 ...

  4. webbench源码学习-->命令行选项解析函数getopt和getopt_long函数

    对于webbench这个网站压力测试工具网上介绍的很多,有深度详解剖析的,对于背景就不在提了, 听说最多可以模拟3万个并发连接去测试网站的负载能力,这里主要是学习了一下它的源码,做点 笔记. 官方介绍 ...

  5. ARM裸板开发:07_IIC 通过IIC总线接口读写时钟芯片时间参数实现的总结

    问题一:程序直接在iRAM内部可正常执行,而程序搬移(Nand ->SDRAM)之后,就不能正常运行了 #define NAND_SECTOR_SIZE 2048 /* 读函数 */ void ...

  6. 【Android 】查看被测应用程序package和launchable-activity

    [appt] appt 工具所在位置..\sdk\build-tools\23.0.1\aapt.exe #查看Package>aapt dump badging C:\QQemail.apk ...

  7. Apache和iis的冲突处理

    http://wenku.baidu.com/link?url=N4GYFpkQyr8G0kVEy3AR2Q5FBho8EOle-_5inEfEq6QSxlyzB3xSbcpeugRdExkSU-tw ...

  8. Linux删除Screen

    screen screen命令是用来解决远程运行服务器中程序时无法退出的尴尬问题. 介绍 有详细的一篇文章 linux screen 命令详解 问题 文章较老,难免有问题. 比如某用户评论: Ctrl ...

  9. 安卓手机文件管理器简单横向评比 - imsoft.cnblogs

      X-plore文件管理器 个人评价:安卓手机上管理文件的神器,所有文件一览无余,加上自己对软件常用功能的配置,管理文件无比方便.(本人一直使用)   Solid文件管理器 个人评价:用户体验真的很 ...

  10. IntelliJ IDEA 2017激活

    最新更新: 在激活Jetbrains旗下任意产品的时候选择激活服务器 填入以下地址便可成功激活 http://idea.liyang.io 点击help→Register→License sever ...