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. selenium高亮显示操作步骤方法

    package com.allin.pc;import java.util.List;import org.openqa.selenium.WebElement;import org.openqa.s ...

  2. 内核编译选配(VMware篇)

    出现这个错误的原因是相应的驱动程序没有编译进内核,所以在内核启动时,不认识分区. 一.磁盘驱动没编译进内核 VMware5.5.3 的磁盘有两种,一种是IDE的,一种是SCSI的:VMware 你在新 ...

  3. Bootstrap_导航

    一.标签形tab导航 标签形导航,也称为选项卡导航. 标签形导航是通过“.nav-tabs”样式来实现.在制作标签形导航时需要在原导航“.nav”上追加此类名. <ul class=" ...

  4. web服务器工作原理

    Web服务器工作原理概述 转载自http://www.importnew.com/15020.html 很多时候我们都想知道,web容器或web服务器(比如Tomcat或者jboss)是怎样工作的?它 ...

  5. 浅谈用ModelSim+Synplify+Quartus来实现Altera FPGA的仿真

    浅谈用ModelSim+Synplify+Quartus来实现Altera FPGA的仿真 工作内容: Mentor公司的ModelSim是业界最优秀的HDL语言仿真软件,它能提供友好的仿真环境,是业 ...

  6. Python virtualenv安装库报错SSL: CERTIFICATE_VERIFY_FAILED

    Python virtualenv安装库报错SSL: CERTIFICATE_VERIFY_FAILED 问题描述 使用pip按照virtualenv报错,如下: pip install virtua ...

  7. SQL Server 2012 管理新特性:AlwaysOn【转】

    http://jimshu.blog.51cto.com/3171847/871169 见超链接

  8. BZOJ 2600: [Ioi2011]ricehub

    2600: [Ioi2011]ricehub Time Limit: 10 Sec  Memory Limit: 128 MBSubmit: 628  Solved: 325[Submit][Stat ...

  9. 封装对MongoDB数据库的增删改查访问方法(基于MongoDB官方发布的C#驱动)

    本文利用MongoDB官方发布的C#驱动,封装了对MongoDB数据库的增删改查访问方法.先用官方提供的mongo-csharp-driver ,当前版本为1.7.0.4714 编写数据库访问帮助类 ...

  10. sqlAlchemy 按DateTime字段的年或月进行group_by查询

    一.根据”create_date“查询每天的数据 1.查询2016年5月每天的数据 session.query(extract('day', User.create_date).label('day' ...