A strange lift



Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)

Total Submission(s): 14947    Accepted Submission(s): 5654



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

//这题是电梯up down,up层数i+ki down i-ki 注意边界  求a->b最少操作次数

#include <stdio.h>
#include <string.h>
#include <queue>
using namespace std;
int n,start,stop;
int p[210];
int vis[210];
struct Node
{
int floor;
int step;
}; int bfs(int flo)
{
queue <Node> q;
memset(vis,0,sizeof(vis));
Node a;
a.floor=flo,a.step=0;
q.push(a);
while(!q.empty())
{
Node b=q.front();
q.pop();
vis[b.floor]=1;
if(b.floor==stop) return b.step;
for(int i=0;i<2;i++) //0 up 1 down
{
Node c=b;
if(i==0)
c.floor=c.floor+p[c.floor];
else
c.floor=c.floor-p[c.floor]; if(!vis[c.floor]&&c.floor>=1&&c.floor<=n)
{
c.step++;
q.push(c);
vis[c.floor]=1;
}
}
}
return -1;
} int main()
{
while(~scanf("%d",&n)&&n)
{
scanf("%d%d",&start,&stop);
for(int i=1;i<=n;i++)
scanf("%d",&p[i]);
printf("%d\n",bfs(start));
}
return 0;
}

hdu 1548的更多相关文章

  1. cogs 364. [HDU 1548] 奇怪的电梯 Dijkstra

    364. [HDU 1548] 奇怪的电梯 ★   输入文件:lift.in   输出文件:lift.out   简单对比时间限制:1 s   内存限制:128 MB [问题描述] 呵呵,有一天我做了 ...

  2. hdu 1548 楼梯 bfs或最短路 dijkstra

    http://acm.hdu.edu.cn/showproblem.php?pid=1548 Online Judge Online Exercise Online Teaching Online C ...

  3. hdu 1548 A strange lift

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

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

  5. HDU 1548 A strange lift (Dijkstra)

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

  6. hdu 1548 A strange lift (dijkstra算法)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1548 题目大意:升降电梯,先给出n层楼,然后给出起始的位置,即使输出从A楼道B楼的最短时间. 注意的几 ...

  7. hdu 1548 A strange lift 解题报告

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1548 题目意思:给出 n 个 floor 你,每个floor 有一个数k,按下它可以到达 floor ...

  8. poj 1564 Sum It Up | zoj 1711 | hdu 1548 (dfs + 剪枝 or 判重)

    Sum It Up Time Limit : 2000/1000ms (Java/Other)   Memory Limit : 65536/32768K (Java/Other) Total Sub ...

  9. HDU 1548 A strange lift (最短路/Dijkstra)

    题目链接: 传送门 A strange lift Time Limit: 1000MS     Memory Limit: 32768 K Description There is a strange ...

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

随机推荐

  1. day03_07 变量的重新赋值01

    在cmd中退出python,需要使用quit()命令 name = "Alex Li" name2 = name print(name,name2) name = "Ja ...

  2. JQUERY杂技

    JQUERY小技巧 this.value jsobject.val() #aaa:eq(2) 选择第三个id为aaa的元素 $("input")只选择input标签.如:<i ...

  3. 学习 WebService 第三步:一个简单的实例(RAD+WAS 8.5开发SOAP项目)

    [开发环境] Web Service 服务器端开发工具:RAD(Eclipse内核) Web Service 服务器:IBM WebSphere v8.5 REST/SOAP:SOAP(JAX-WS/ ...

  4. CountDownLatch和CyclicBarrier 的用法

    CountDownLatch是减计数方式,计数==0时释放所有等待的线程:CyclicBarrier是加计数方式,计数达到构造方法中参数指定的值时释放所有等待的线程.CountDownLatch当计数 ...

  5. Method and apparatus for verification of coherence for shared cache components in a system verification environment

    A method and apparatus for verification of coherence for shared cache components in a system verific ...

  6. 诡异的malloc函数和%S的输出

    今天测试代码过程中无意间发现,代码如下: #include <stdio.h> #include <string.h> #include <stdlib.h> #d ...

  7. ASP.NET路由应用及IIS配置(非MVC)

    一.前后台代码: Global.cs: using System.Web.Routing; ... void Application_Start(object sender, EventArgs e) ...

  8. centos 7安装golang1.10

    一.安装&配置 官方下载包(一般需要梯子) https://golang.org/dl/ wget https://dl.google.com/go/go1.10.linux-amd64.ta ...

  9. LeetCode OJ--Construct Binary Tree from Preorder and Inorder Traversal *

    http://oj.leetcode.com/problems/construct-binary-tree-from-preorder-and-inorder-traversal/ 根据二叉树的前序遍 ...

  10. 阿里云oss教程

    OSS是提供非结构化数据存取的服务.对于刚开始使用OSS的用户,非结构数据可以理解为word文档.PDF.PPT.EXCEL表格.MP3.MKV.RMVB.HTML等各种类型文件.OSS提供API去进 ...