题目链接

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

分析:

开始一些BFS练习了,先来一道简单的,这是一栋大楼,有一部很奇怪的电梯,电梯只有两个按钮,

UP和DOWN,每个楼层有一个数字num,如果你在第2层,你按UP,电梯会到达 num+2层,按DOWN亦然,

到达2-num层,求一个人从一个楼层要去另一个楼层,需要按几步?

(这个电梯够让人捉急的!)

当然如果碰到0层或者-1层都不能到达,碰到大于刚开始的n层的也不能到达,

如果永远到不了需要去的那一层,就输出-1了。

广搜,队列做,标准的BFS题目呀,很容易就AC了。。

代码:

#include<stdio.h>
#include<queue>
#include<string.h>
using namespace std;
int A,B;
int a[500];
int bj[500];
struct zuobiao
{
int x,output;
} now,nex;
void bfs(int x)
{
queue<zuobiao >s;
memset(bj,0,sizeof(bj));
bj[x]=1;
now.x=x;
now.output=0;
s.push(now);
while(!s.empty())
{
now=s.front();
if(now.x==B)
{
printf("%d\n",now.output);
return ;
}
s.pop();
for(int i=0; i<2; i++)
{
if(i==0)nex.x=now.x+a[now.x];
if(i==1)nex.x=now.x-a[now.x];
if(nex.x>=1&&nex.x<=200&&bj[nex.x]==0)
{
bj[nex.x]=1;
nex.output=now.output+1;
s.push(nex);
}
}
}
printf("-1\n");
return ;
}
int main()
{
int n;
while(~scanf("%d",&n))
{
if(n==0)break;
scanf("%d%d",&A,&B);
memset(a,0,sizeof(a));
for(int i=1; i<=n; i++)
{
scanf("%d",&a[i]);
}
bfs(A);
}
}

HDU 1548 A strange lift (广搜)的更多相关文章

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

  2. hdu 1548 A strange lift

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

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

  4. HDU 1548 A strange lift (Dijkstra)

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

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

  6. hdu 1548 A strange lift (bfs)

    A strange lift Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) T ...

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

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

  8. HDU 1548 A strange lift 搜索

    A strange lift Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) T ...

  9. HDU 1548 A strange lift(最短路&&bfs)

    A strange lift Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)To ...

随机推荐

  1. Jmeter系列-webdriver插件

    1.下载地址    JMeterPlugins-WebDriver-1.1.2 2.将JMeterPlugins-WebDriver-1.1.2\lib\ext中的*.jar拷贝到D:\apache- ...

  2. C# 中的语法糖

    1.   using 代替了 try-catch-finally 因为之前是学 Java 的,在连接数据库或者进行文件读写操作时很自然的就使用了 try-catch-finally-,在 C# 中这样 ...

  3. Oracle 物化视图创建以及常见问题

    create materialized view MV_XXXXrefresh fast on commitwith rowidenable query rewriteasselect * from ...

  4. 【SPOJ - GSS2】Can you answer these queries II(线段树)

    区间连续不重复子段最大值,要维护历史的最大值和当前的最大值,打两个lazy,离线 #include<cstdio> #include<cstring> #include< ...

  5. 2018牛客多校第五场 E.room

    题意: 一共有n个宿舍,每个宿舍有4个人.给出第一年的人员分布和第二年的人员分布,问至少有多少人需要移动. 题解: 对于第一年的每个宿舍,向今年的每种组合连边.流量为1,费用为(4 - 组合中已在该宿 ...

  6. POJ2187:Beauty Contest——题解

    http://poj.org/problem?id=2187 题目大意:给n个点,求点对最大距离的平方. ———————————————————— 很容易证明最大距离的点对在最大凸包上. 那么就是旋转 ...

  7. BZOJ3675 & 洛谷3648 & UOJ104:[Apio2014]序列分割——题解

    https://www.lydsy.com/JudgeOnline/problem.php?id=3675 https://www.luogu.org/problemnew/show/P3648 ht ...

  8. 史上最全Linux提权后获取敏感信息方法

    http://www.freebuf.com/articles/system/23993.html 在本文开始之前,我想指出我不是专家.据我所知,在这个庞大的区域,没有一个“神奇”的答案.分享,共享( ...

  9. 最近经历的一些大数据(Spark/Hadoop)面试题

    1.简答说一下hadoop的map-reduce编程模型 首先map task会从本地文件系统读取数据,转换成key-value形式的键值对集合 使用的是hadoop内置的数据类型,比如longwri ...

  10. 使用Faster-Rcnn进行目标检测(实践篇)转载

    原 文链接:http://blog.csdn.net/gavin__zhou/article/details/52052915 原理 上一篇文章,已经说过了,大家可以参考一下,Faster-Rcnn进 ...