题目链接

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. lol人物模型提取(三)

      提取出来的lol人物模型能让你知道一些有趣的信息,比如说给英雄量个身高啥的.   经测量,佐伊的身高应大于1m60,比想象中的着实高不少啊.   然后还应该把这个模型镜像对称一下,在3dsmax里 ...

  2. Jenkins系列-Jenkins升级、迁移和备份

    升级Jenkins Jenkins的开发迭代非常快,每周发布一个开发版本,长期支持版每半年更新一次(ps:大版本更新).如此频繁的更新,怎么升级呢? war:下载新版的war文件,替换旧版本war文件 ...

  3. 修改QQ各版本的默认保存位置(聊天记录)

    这几天没少折腾windows,都有点烦了,我是那种有强迫症的,只要知道的自己没有做到的会感觉到浑身不爽的因为系统重装了好几次,QQ也没少安装几次,我使用的是TM的QQ(没有 那么多烦人的广告,娱乐组件 ...

  4. listBox和pictureBox的使用

    重要属性:pictureBox中SizeMode可以更改图像显示的尺寸大小. using System; using System.Collections.Generic; using System. ...

  5. QT分析之消息事件机制

    原文地址:http://blog.163.com/net_worm/blog/static/127702419201001432028526/ 上回我们分析到QPushButton的初始化,知道了Wi ...

  6. /var/redis/run/redis_6379.pid exists, process is already running or crashed的解决办法

    命令:service redis start /var/redis/run/redis_6379.pid exists, process is already running or crashed 引 ...

  7. 在网页中浏览PDF文档

    刚开始找了好多插件,包括pdf.js,但都不理想,后来发现用iframe反而容易: <iframe src="test_pdf.pdf" width="800&qu ...

  8. Android Shimmer 发光微光动画

    这是Facebook提供的一个类库(题外话http://code.facebook.com,这里有很多好玩有趣有用的Facebook开源的类库) 这么炫酷的发光动画效果,想必很多Android码农都会 ...

  9. pascal语言中学版整理

    P1:主菜单File中的Command shell选项,可以暂时退出Pascal,进入DOS提示符状态,但Pascal仍然驻留在内存中.输入命令exit即可返回Pascal. P3:Edit菜单中Un ...

  10. Linq里where出现null的问题

    今天遇到一个问题,怎么在where里判断一个字段是否为null,并且这个字段不是字符串string类型,而是int和GUID类型,折腾了半天终于搞明白了.(由于项目是我半路接手的,问题是前期的同事给我 ...