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

最新文章

  1. Android Activity的4种启动模式详解(示例)
  2. 多个supplemental logging的说明与删除
  3. HTML5 屏蔽触屏滚动
  4. java连接hbase报错
  5. MVVM: 通过 Binding 或 x:Bind 结合 Command 实现,通过 ButtonBase 触发命令
  6. Storm集群的安装配置
  7. 根据backup-my.cnf来生成新的事务日志文件
  8. struct内存对齐1:gcc与VC的差别
  9. 如何为Linux生成和打上patch
  10. Keepalived安装及初步使用
  11. Having the Result Set of a Stored Proc Sent to You by RSS Feed.
  12. 【大数加法】POJ-1503、NYOJ-103
  13. C语言的ANSI/ISO标准
  14. PHP计算2点经纬度之间的距离
  15. Java8新特性
  16. JAVA控制台版斗地主
  17. 删除Win10资源管理器中的3D对象/音乐/视频文件夹
  18. Winform 窗体实现圆角展示
  19. java.lang.NoClassDefFoundError错误
  20. DSU on Tree浅谈

热门文章

  1. 2017.1.9版给信息源新增:max_len、max_db字段
  2. %s %r 区别 转
  3. django中怎么使用mysql数据库的事务
  4. shell 一次移动很多个命名相似的文件
  5. CentOS 7安装配置Redis数据库
  6. Windows内置系统账户:Local system/Network service/Local Service 区别
  7. 第八章&#160;高级搜索树 (xa3)红黑树:插入
  8. ECMAScript5新特性之isFrozen、freeze
  9. .NET中的Request
  10. 数据库日志文件——数据库“xxx”的事务日志已满,原因为“LOG_BACKUP”