题目链接

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. Scala快速入门-基础

    HelloWorld 从HelloWorld开始,使用scala IDE编辑器. 新建scala project 新建scala object 编写HelloWorld run as scala ap ...

  2. Zigbee安全基础篇Part.1

    原文地址: https://www.4hou.com/wireless/14211.html 导语:ZigBee是一种开源无线技术,用于低功耗嵌入式设备(无线电系统).本文探讨了ZigBee协议的可用 ...

  3. 【转载】Windows下Mysql5.7开启binlog步骤及注意事项

    转自:https://www.cnblogs.com/wangwust/p/6433453.html 1.查看是否开启了binlog:show binary logs; 默认情况下是不开启的. 2.开 ...

  4. Linux命令之查看cpu个数_核数_内存总数

    http://blog.csdn.net/cgwcgw_/article/details/10000053 cpu个数 cat /proc/cpuinfo | grep "physical ...

  5. DELPHI enablecontrols,disablecontrols函数

    DisableControls方法是在程序修改或后台有刷新记录的时候切断数据组件,如TTABLE.ADOQUERY等等与组件数据源的联系.如果没有切断,数据源中只要一有数据的改动,尤其是批量改动的话, ...

  6. C#操作access练习

    ORM框架使用dapper,dapper不仅能操作sqlserver,也能操作access,下面为基本代码: OleDbConnection strConnection = new OleDbConn ...

  7. 【bzoj2272】[Usaco2011 Feb]Cowlphabet 奶牛文字 dp

    题目描述 Like all bovines, Farmer John's cows speak the peculiar 'Cow'language. Like so many languages, ...

  8. 【bzoj1717】[Usaco2006 Dec]Milk Patterns 产奶的模式 后缀数组+离散化

    题目描述 农夫John发现他的奶牛产奶的质量一直在变动.经过细致的调查,他发现:虽然他不能预见明天产奶的质量,但连续的若干天的质量有很多重叠.我们称之为一个“模式”. John的牛奶按质量可以被赋予一 ...

  9. OI入门

    学习顺序: 1.高精度计算: 高精度计算(一) 高精度计算练习1 高精度计算(二) 高精度计算练习2 2.递推 3.递归 递归算法 递归练习 4.搜索与回溯 搜索与回溯算法(一) 搜索与回溯练习(一) ...

  10. [NOI2017]蔬菜 贪心

    题面: [NOI2017]蔬菜 题解: 首先每天蔬菜会变质这点并不好处理,我们考虑让时间倒流,从后向前处理,这样的话就相当于每天都会得到一定量的蔬菜. 这样做有什么好处呢? 我们可以发现一个性质:如果 ...