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层,要从s到t,给出每层只能下或者上a[i],问至少经过多少次才能从s到t

【思路】经典BFS

#include<iostream>
#include<stdio.h>
#include<string.h>
#include<queue>
using namespace std;
const int N=;
int a[N];
int vis[N];
int n,s,t;
struct node
{
int x;
int step;
};
bool go(int x)
{
if(x<=||x>n) return false;
return true;
}
int bfs()
{
node now,next;
queue<node>qu;
now.x=s;
now.step=;
qu.push(now);
while(!qu.empty())
{
now=qu.front();
qu.pop();
if(now.x==t)
return now.step;
for(int i=-;i<=;i+=)
{
next=now;
next.x+=a[next.x]*i;
if(go(next.x)&&vis[next.x]==)
{
vis[next.x]=;
next.step++;
qu.push(next); }
}
}
return -;
}
int main()
{
while(~scanf("%d",&n),n)
{
scanf("%d%d",&s,&t);
for(int i=;i<=n;i++)
scanf("%d",&a[i]);
memset(vis,,sizeof(vis));
int ans=bfs();
printf("%d\n",ans);
}
return ;
}

A strange lift_BFS的更多相关文章

  1. timus 1175. Strange Sequence 解题报告

    1.题目描述: 1175. Strange Sequence Time limit: 1.0 secondMemory limit: 2 MB You have been asked to disco ...

  2. CF719C. Efim and Strange Grade[DP]

    C. Efim and Strange Grade time limit per test 1 second memory limit per test 256 megabytes input sta ...

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

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

  4. ACM : HDU 2899 Strange fuction 解题报告 -二分、三分

    Strange fuction Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Tot ...

  5. POJ 2891 Strange Way to Express Integers(拓展欧几里得)

    Description Elina is reading a book written by Rujia Liu, which introduces a strange way to express ...

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

  7. poj 2891 Strange Way to Express Integers (非互质的中国剩余定理)

    Strange Way to Express Integers Time Limit: 1000MS   Memory Limit: 131072K Total Submissions: 9472   ...

  8. A strange lift

    Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Submission( ...

  9. strange error encountered today in ROS

    I reinstalled my ubuntu system and also ROS. I tested slam_karto package when some strange error cam ...

随机推荐

  1. 有向图的强连通分量——Tarjan

    在同一个DFS树中分离不同的强连通分量SCC; 考虑一个强连通分量C,设第一个被发现的点是 x,希望在 x 访问完时立刻输出 C,这样就可以实现 在同一个DFS树中分离不同的强连通分量了. 问题就转换 ...

  2. hadoop运维经验

    0.优化:http://dongxicheng.org/mapreduce/hadoop-optimization-0/ http://dongxicheng.org/mapreduce/hadoop ...

  3. 嵌入资源的方式让Winform使用系统没有的字体,无需安装字体

    原文: How to embed a True Type font http://bobpowell.net/embedfonts.aspx 测试项目下载: http://files.cnblogs. ...

  4. JavaIO总结

    Java IO流分为字节流和字符流 下面首先介绍一下字节流 /** * 字节流测试 * @author hc * */ public class Test { public static void m ...

  5. 使用bootstrap框架的模态框与ckeditor产生冲突,ckeditor的弹出窗不可用时的解决方法

    这样可以解决冲突 $.fn.modal.Constructor.prototype.enforceFocus = function () { modal_this = this $(document) ...

  6. python ConfigParser配置读写

    一.ConfigParser简介 ConfigParser 是用来读取配置文件的包.配置文件的格式如下:中括号"[ ]"内包含的为section.section 下面为类似于key ...

  7. Java精确计算小数

    Java在计算浮点数的时候,由于二进制无法精确表示0.1的值(就好比十进制无法精确表示1/3一样),所以一般会对小数格式化处理. 但是如果涉及到金钱的项目,一点点误差都不能有,必须使用精确运算的时候, ...

  8. linux应用程序开发-文件编程-系统调用方式

    在看韦东山视频linux驱动方面有一些吃力,究其原因,虽然接触过linux应用程序编程,但是没有深入去理解,相关函数用法不清楚,正好看到国嵌视频对这一方面讲的比较透彻, 所以把学习过程记录下来,也作为 ...

  9. Delphi 使用之连接数据库

    DELPHI 中的数据库开发有很多种类的,可以连接Access数据库.MS SQL Server 数据库.Oracle 数据库.MySQL数据库等等,一般连接有两种方式:BDE和ADO两种方式, 都是 ...

  10. myeclipse/eclipse没有Project Facets的解决方法

    http://www.cnblogs.com/jerome-rong/archive/2012/12/18/2822783.html 经常在eclipse中导入web项目时,出现转不了项目类型的问题, ...