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

思路

状态的转移方式只有2种,要么上,要么下,至于每次移动的距离则是\(k[i]\),根据这个来bfs就对了

代码

#include<bits/stdc++.h>
using namespace std;
struct node
{
int pos;
int step;
}st;
int n,a,b;
int k[201];
bool vis[200010]; bool judge(int x)
{
if(vis[x] || x<1 || x>n)
return false;
return true;
}
int bfs(int a)
{
queue<node> q;
st.pos = a;
st.step = 0;
q.push(st);
node next,now;
memset(vis,false,sizeof(vis));
vis[st.pos] = true;
while(!q.empty())
{
now = q.front();
q.pop();
if(now.pos == b) return now.step;
for(int i=0;i<2;i++)
{
if(i==0)
next.pos = now.pos + k[now.pos];
else
next.pos = now.pos - k[now.pos];
next.step = now.step + 1;
if(judge(next.pos))
{
q.push(next);
vis[next.pos] = true;
}
}
}
return -1;
} int main()
{
while(cin>>n)
{
if(n==0) break;
cin >> a >> b;
for(int i=1;i<=n;i++)
cin >> k[i];
int ans = bfs(a);
cout << ans << endl;
}
return 0;
}

Hdoj 1548.A strange lift 题解的更多相关文章

  1. HDU 1548 A strange lift 题解

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

  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. 杭电 1548 A strange lift(广搜)

    http://acm.hdu.edu.cn/showproblem.php?pid=1548 A strange lift Time Limit: 2000/1000 MS (Java/Others) ...

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

  5. HDU 1548 A strange lift (Dijkstra)

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

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

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

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

  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) T ...

随机推荐

  1. stark组件数据库管理软件的总结

      1.stark - 总结 用到的知识点: 1.单例模式 2.继承 3.反射 4.面向对象 5.modelform 1.注册表 单例模式 site = StarkSite() 2.生成url url ...

  2. IdentityServer4【Topic】之登陆注册

    Sign-in 登陆注册 为了让标识服务器(identity server)代表用户发出令牌,该用户必须登录到标识服务器. Cookie authentication Cookie认证 身份验证是由来 ...

  3. pl/sql实现打印九九乘法表

    学习PL/SQL循环的时候写的,记录一下. declare v_number1 ); -- 外层循环变量 v_number2 ); -- 内层循环变量 begin .. -- 开始外层循环 loop ...

  4. STL中vector、set、list和map

  5. 使用NFS时的一些问题

    当我把nfs服务端共享目录/usr/local/data/test删掉时,在nfs客户端却没办法把之前挂载在这上面的当前从机上的/usr/local/data/test删除,出现 bash: cd: ...

  6. 剑指offer(8)

    题目: 输入一个整数,输出该数二进制表示中1的个数.其中负数用补码表示. 思路: 第一反应想到的是把数右移,每一位与1相与,然后判断个数,但是若输入的为负数,会出现死循环现象. 所以我们设置一个标志量 ...

  7. java 中Excel的导入导出

    部分转发原作者https://www.cnblogs.com/qdhxhz/p/8137282.html雨点的名字  的内容 java代码中的导入导出 首先在d盘创建一个xlsx文件,然后再进行一系列 ...

  8. java中级——集合框架【2】-二叉树

    二叉树 二叉树概念 首先我们要明确,二叉树由各种节点组成:还有就是二叉树特点:(1)每个节点都可以有左子节点,右子节点(2)每一个节点都有一个值 如图所示: 代码所示: package cn.jse. ...

  9. drf实现图片验证码功能

    一.背景 在之前实现过django的图片验证码,有自己实现过的,也有基于django-simple-captcha的,都是基于form表单验证,若自己实现,可以获取相应的标签name便可以获取判断,若 ...

  10. ASP.NET Web.config文件的配置(Configuration API)

    本次我们讨论主要聚焦在以下Web.config配置文件的设置值的读取. 1.<connectionString />连接字符串的读取. 2.<appSettings />应用程 ...