题目链接

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);
}
}

最新文章

  1. SQL Server 复制系列(文章索引)
  2. 【splay】文艺平衡树 BZOJ 3223
  3. Win7 64位 php+Apache+mysql 配置
  4. 4部门明确软件IC产业企业所得税优惠政策
  5. [MFC] 编辑框 EditControl 输入数字范围限制
  6. Multi-Objective Data Placement for Multi-Cloud Socially Aware Services---INFOCOM 2014
  7. 操作符、语句、函数——Javascript高级程序设计
  8. JS为网页添加文字水印【原创】
  9. angular.js实现省市区三级联动指令
  10. 如何两周达到150行Java程序的能力--part 1
  11. 求最小生成树——Kruskal算法
  12. mac安装postman
  13. Windows下MongoDB设置用户、密码
  14. 显示开机信息-dmesg
  15. HQL-Query接口
  16. idea Connection to SQL Server - 公网8 failed java
  17. tfs团队项目删除原来连接的默认账户
  18. springboot 定制错误页面
  19. android 数据加密——加密的概述
  20. 如何找回Ucenter创始人密码,账号无需修改

热门文章

  1. iOS- 无处不在,详解iOS集成第三方登录(SSO授权登录&lt;无需密码&gt;)
  2. Unity3d学习日记(一)
  3. JS高级 1
  4. 统计VS2013中有效行数
  5. WCF面试精典题汇总
  6. 基于c++和opencv底层的图像旋转
  7. 【Linux】linux中删除指定文件外所有其他文件(夹)的问题
  8. Codeforces Round #522 Div. 1 没打记
  9. Android 通知机制 Toast和Notification
  10. BZOJ4869:[SHOI2017]相逢是问候——题解