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层楼,在每层楼移动的层数,计算从A层到B层最少的移动次数,如果不能到达输出-1;
解题思路:广度优先搜索;
感悟:比较正常的广搜(不用剪枝,^0^),但是用floor,和next命名的时候莫名其妙的CE了
代码:
#include

#include

#include

#include

#include

#include

#define maxn 205



using namespace std;

int A,B,n,flo,a,f;

int k[maxn],t[maxn];



bool visit[maxn];

int check(int a)

{

   
if(a<0||a>n||visit[a])

       
return 1;

    else

       
return 0;

}

int bfs(int A,int B)

{

   
queueQ;

   
Q.push(A);

   
visit[A]=true;

   
while(!Q.empty())

    {

       
flo=Q.front();

       
Q.pop();

       
if(flo==B)

       
{

           
f=1;

           
return t[flo];

       
}

       
//上楼

       
a=flo+k[flo];

       
if(!check(a))

       
{

           
Q.push(a);

           
t[a]=t[flo]+1;

           
visit[a]=true;

       
}

       
//下楼

       
a=flo-k[flo];

       
if(!check(a))

       
{

           
Q.push(a);

           
t[a]=t[flo]+1;

           
visit[a]=true;

       
}

    }

}

int main()

{

   
//freopen("in.txt", "r", stdin);

   
while(~scanf("%d",&n)&&n)

    {

       
memset(visit,false,sizeof(visit));

       
memset(t,0,sizeof(t));

       
f=0;

       
scanf("%d%d",&A,&B);

       
flo=A;

       
for(int i=1;i<=n;i++)

           
scanf("%d",&k[i]);

       
bfs(A,B);

       
if(f)

           
printf("%d\n",t[B]);

       
else

           
printf("-1\n");

    }

}

A strange lift的更多相关文章

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

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

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

  3. A strange lift

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

  4. bfs A strange lift

    题目:http://acm.hdu.edu.cn/showproblem.php?pid=1548 There is a strange lift.The lift can stop can at e ...

  5. hdu 1548 A strange lift

    题目连接 http://acm.hdu.edu.cn/showproblem.php?pid=1548 A strange lift Description There is a strange li ...

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

  7. HDU 1548 A strange lift (Dijkstra)

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

  8. HDU1548——A strange lift(最短路径:dijkstra算法)

    A strange lift DescriptionThere is a strange lift.The lift can stop can at every floor as you want, ...

  9. HDU 1548 A strange lift 搜索

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

  10. 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. jquery自定义banner图滚动插件---(解决最后一张图片倒回第一张图片的bug)

    banner图的滚动效果动画 最近做项目中banner滚动的时候遇到了一个小bug,当banner滚动到最后一张图再跳回第一张图时, 会出现默认的倒回第一张图的过渡效果,看了几个插件都是这样,所以自定 ...

  2. [转载]iOS开发之手势识别

    感觉有必要把iOS开发中的手势识别做一个小小的总结.在上一篇iOS开发之自定义表情键盘(组件封装与自动布局)博客中用到了一个轻击手势,就是在轻击TextView时从表情键盘回到系统键盘,在TextVi ...

  3. Java线程池带图详解

    线程池作为Java中一个重要的知识点,看了很多文章,在此以Java自带的线程池为例,记录分析一下.本文参考了Java并发编程:线程池的使用.Java线程池---addWorker方法解析.线程池.Th ...

  4. sqoop使用的问题

    找不到表 17/05/02 18:15:47 ERROR tool.ImportTool: Imported Failed: There is no column found in the targe ...

  5. 糖果大战 hdu1204

    糖果大战 Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Submis ...

  6. D. Mysterious Present (看到的一个神奇的DP,也可以说是dfs)

    D. Mysterious Present time limit per test 2 seconds memory limit per test 64 megabytes input standar ...

  7. margin:0px auto和text-align:center区别

    (1)margin:0px auto :作用于块级元素,对块级元素进行居中 (2)text-align:center:作用于内联元素,必须放在要居中的内联元素所在的块级元素. 例: (1) <d ...

  8. 再起航,我的学习笔记之JavaScript设计模式30(简单模板模式)

    简单模板模式 概念介绍 简单模板模式(Simple template): 通过格式化字符串拼凑出视图避免创建视图时大量节点操作,优化内存开销. 创建模板 在实际的业务中如果我们需要进行前后台交互,或多 ...

  9. SQL Server 后悔药 delete drop update

    国庆假期终于有时间做点事情 因为平常工作会做些数据库操作 可能会有所操作失误  参考一下 方法一 ApexSql 2016一个软件 http://www.cnblogs.com/gsyifan/p/A ...

  10. 前端基础之JavaScript

    什么是JavaScript? JavaScript,也称ECMAScript,是一种基于对象和事件驱动并具有相对安全性并广泛用于客户端网页开发的脚本语言,同时也是一种广泛用于客户端Web开发的脚本语言 ...