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. Linux安装Scala

    下载Scala地址http://downloads.typesafe.com/scala/2.10.6/scala-2.10.6.tgz然后解压Scala到指定目录 tar -zxvf scala-2 ...

  2. django demo --blog

    详情,请看虫师博客http://www.cnblogs.com/fnng/p/3737964.html 和https://my.oschina.net/matrixchan/blog/184445  ...

  3. JS数组的下标如果是字符串的排序

    var test = []; test['0'] = 0; test['1'] = 1; test['2'] = 2; 这样一个数组的排序方式是字符为‘1’的数组元素排第一,为‘0’的排在最后

  4. Map容器——HashMap及常用API,及put,get方法解析,哈希码的产生和使用

    Map接口 ①   映射(map)是一个存储键/值对的对象.给定一个键,可以查询到它的值,键和值都是对象; ②   键必须是唯一的,值可以重复; ③   有些映射可以接收null键和null值,而有的 ...

  5. eclipse Java EE安装和web项目的创建

    一.根据http://www.itnose.net/detail/6139800.html基本安装成功二.根据http://www.cnblogs.com/freebsd-pann/archive/2 ...

  6. LibreOJ2042 - 「CQOI2016」不同的最小割

    Portal Description 给出一个给出一个\(n(n\leq850)\)个点\(m(m\leq8500)\)条边的无向图.定义\(cut(s,t)\)等于\(s,t\)的最小割的容量,求在 ...

  7. py-faster-rcnn 的makefile.config 注意事项

    在配置py-faster-rcnn的过程中,我遇到一些问题,记录如下 py-faster-rcnn文件夹下面有一个caffe-fast-rcnn文件夹 这个过程中,我们需要编译caffe, rbgir ...

  8. linux maven安装(三)

    1.下载maven http://maven.apache.org/download.cgi 我下载的是:apache-maven-3.3.9-bin.tar.gz 解压:tar -zxvf apac ...

  9. 创建SVN 本地服务器

    svnserve具体配置如下,主要是将 password-db 前的#号去掉,即去掉注释使其生效 passwd具体配置如下,主要是新增自己需要的账号和密码,也可以将原有的账号去掉注释使用 authz ...

  10. LFYZOJ 104 Counting Swaps

    题解 #include <iostream> #include <cstdio> #include <algorithm> #include <cmath&g ...