A strange lift

Time Limit : 2000/1000ms (Java/Other)   Memory Limit : 65536/32768K (Java/Other)
Total Submission(s) : 69   Accepted Submission(s) : 34

Font: Times New Roman | Verdana | Georgia

Font Size: ← →

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

______________________________________________________________________________________________________________________________

题意:电梯每层有一个不同的数字,例如第n层有个数字k,那么这一层只能上k层或下k层,但是不能低于一层或高于n层,给定起点与终点,要求出最少要按几次键
思路:BFS
#include<iostream>
#include <cstdio>
#include <cstring>
#include<queue>
#include <algorithm>
using namespace std;
int a[205];
int vis[205];
int n;
struct node
{
int x, cnt;
};
int bfs(int si, int di)
{
queue<node>q;
node d, f;
f.x = si;
f.cnt = 0;
vis[si] = 1;
q.push(f);
while (!q.empty())
{
f = q.front();
q.pop();
if (f.x == di)
return f.cnt;
d.x = f.x - a[f.x];
if (d.x>0&&d.x<=n&&!vis[d.x])
{
vis[d.x] = 1;
d.cnt = f.cnt + 1;
q.push(d);
}
d.x = f.x + a[f.x];
if (d.x>0 && d.x <= n&&!vis[d.x])
{
vis[d.x] = 1;
d.cnt = f.cnt + 1;
q.push(d);
}
}
return -1;
} int main()
{
int st, ed;
while (~scanf("%d", &n)&&n)
{
scanf("%d%d", &st, &ed);
for (int i = 1; i <= n; i++)
scanf("%d", &a[i]);
memset(vis, 0, sizeof(vis));
int ans = bfs(st, ed);
printf("%d\n", ans); }
return 0;
}

Hdu1548 A strange lift 2017-01-17 10:34 35人阅读 评论(0) 收藏的更多相关文章

  1. 山东理工大学第七届ACM校赛-经济节约 分类: 比赛 2015-06-26 10:34 19人阅读 评论(0) 收藏

    经济节约 Time Limit: 1000ms   Memory limit: 65536K  有疑问?点这里^_^ 题目描述 由于经济紧张,某国国王决定减少一部分多余的士兵,这些士兵在边界都有各自的 ...

  2. Hangover 分类: POJ 2015-06-11 10:34 12人阅读 评论(0) 收藏

    Hangover Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 108765   Accepted: 53009 Descr ...

  3. 博弈论入门小结 分类: ACM TYPE 2014-08-31 10:15 73人阅读 评论(0) 收藏

    文章原地址:http://blog.csdn.net/zhangxiang0125/article/details/6174639 博弈论:是二人或多人在平等的对局中各自利用对方的策略变换自己的对抗策 ...

  4. 山东理工大学第七届ACM校赛-最大收益问题 分类: 比赛 2015-06-26 10:25 51人阅读 评论(0) 收藏

    最大收益问题 Time Limit: 2000ms Memory limit: 65536K 有疑问?点这里^_^ 题目描述 铁牌狗最近迷上了一款游戏,但铁牌狗实在是太笨了,他还是要请求你的帮助. 有 ...

  5. Financial Management 分类: POJ 2015-06-11 10:51 12人阅读 评论(0) 收藏

    Financial Management Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 164431   Accepted: ...

  6. winform timespan 两个时间的间隔(差) 分类: WinForm 2014-04-15 10:14 419人阅读 评论(0) 收藏

    TimeSpan 结构  表示一个时间间隔. 先举一个小例子:(计算两个日期相差的天数) 代码如下: DateTime dt = DateTime.Now.ToShortDateString(yyyy ...

  7. iOS开发:创建真机调试证书 分类: ios相关 2015-04-10 10:22 149人阅读 评论(0) 收藏

    关于苹果iOS开发,笔者也是从小白过来的,经历过各种困难和坑,其中就有关于开发证书,生产证书,in_house证书,add_Hoc证书申请过程中的问题,以及上架发布问题.今天就着重说一下关于针对于苹果 ...

  8. Codeforces758D Ability To Convert 2017-01-20 10:29 231人阅读 评论(0) 收藏

    D. Ability To Convert time limit per test 1 second memory limit per test 256 megabytes input standar ...

  9. log4j配置文件及nutch中的日志配置 分类: B1_JAVA 2015-02-17 10:58 483人阅读 评论(0) 收藏

    吐槽几句,log4j的坑啊.... (1)CLASSPATH中不能有多个log4j的版本本,否则有有奇形怪状的NoSuchMethod, NoSuchFiled, NoClassDefineFound ...

随机推荐

  1. Delphi声明Record变量后直接初始化

    TARec = record    A1: string;    A2: string;  end; TBRec = record    A1: string;    A2: string;    A ...

  2. sql查询job

    use msdb go --if object_id('tempdb..#SqlAgentJob') is not null -- drop table #SqlAgentJob --go decla ...

  3. mysql在linux中安装问题和命令

    1. cd /  切换到 根目录. 2. cd /root 切换到根目录下的 root目录. 3. cd .. 切换到当前目录的上级目录. 4. rpm --qa mysql 查询已经安装mysql. ...

  4. 吴裕雄 数据挖掘与分析案例实战(14)——Kmeans聚类分析

    # 导入第三方包import pandas as pdimport numpy as np import matplotlib.pyplot as pltfrom sklearn.cluster im ...

  5. break、continue、pass介绍

    break.continue.pass介绍 break:跳出当前循环 continue:跳出本次循环,进行下一次循环 pass:什么也不做,占位.

  6. docker-composer

    1.安装docker-composer   参考官方 安装1.20.1 sudo curl -L https://github.com/docker/compose/releases/download ...

  7. Eclipse编辑jsp不显示预览效果页面

    转载链接:https://blog.csdn.net/fishsr/article/details/22662787 转载 2014年03月31日 13:35:35 1.Eclipse打开jsp后,在 ...

  8. Loadrunner通过吞吐量计算每个用户需要的带宽

    Loadrunner通过吞吐量计算每个用户需要的带宽 运行一个场景,点击Analysis进行分析,使用分析报告中的Average Throughput(bytes/second)进行计算. 计算公式: ...

  9. url获取参数

    参考http://www.runoob.com/w3cnote/js-get-url-param.html function getQueryVariable(variable) { var quer ...

  10. IllegalArgumentException: Could not resolve resource location pattern [classpath .xml]: class path resource cannot be resolved to URL because it does not exist

    查看编译后的classes文件后,没有mapper.xml文件,所以SQLsessionfactory不能读取成功. 在Maven中加入如下的resources配置: <dependencies ...