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. 冒泡排序(Bubble Sort),比较次数优化改进

    body, table{font-family: 微软雅黑; font-size: 13.5pt} table{border-collapse: collapse; border: solid gra ...

  2. 需求改进&系统设计

    1.需求&原型改进 针对课堂讨论环节老师和其他组的问题及建议,对修改选题及需求进行修改,发现在原有需求下,无需进行过大修改. 2.系统设计 3.Alpha任务分配计划 待定 4.测试计划 测试 ...

  3. MyEclipse CI 2018.8.0正式发布(附下载)

    MyEclipse线上特惠,在线立享专属折扣!火热开启中>> MyEclipse 2018最终版日前正式发布,新版本通过构建Eclipse Photo.支持Java 10和Java EE ...

  4. DevExpress v17.2新版亮点—DevExtreme篇(二)

    用户界面套包DevExpress DevExtreme v17.2终于正式发布,本站将以连载的形式为大家介绍各版本新增内容.本文将介绍了DevExtreme v17.2 的New Color Sche ...

  5. js中的转换方法

    1.var colors =['red','blue','green']; console.log(colors.toString()); // red,blue,green 总结:toString( ...

  6. jsx介绍

    jsx与传统的html的区别: 1. jsx:使用的“元素”不局限与html中的元素,可以是任何一个React组件,传统的html是做不到的 (区分是否是组件的原则:看第一个字母是否是大写) 2.js ...

  7. ThreadLocal讲解

    一.前言 ThreadLocal这个对象就是为多线程而生的,没有了多线程ThreadLocal就没有存在的必要了.可以将任何你想在每个线程独享的对象放置其中,并在任何时候取出来. 二.基本用法 Thr ...

  8. 关于XCode 的agvtool命令行

    简介:用agvtool如何来自动更新版本号和bulid version   agvtool是一个命令行工具,允许你自动递增到下一个最高的数量或具体的数字这些数字.本文档提供了更新您的构建和版本号码使用 ...

  9. Robust Tracking via Weakly Supervised Ranking SVM

    参考文献:Yancheng Bai and Ming Tang. Robust Tracking via Weakly Supervised Ranking SVM Abstract 通常的算法:ut ...

  10. [LeetCode&Python] Problem 136. Single Number

    Given a non-empty array of integers, every element appears twice except for one. Find that single on ...