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. JS(二)

    上周给大家介绍了一下JS基础中一点东西,今天给大家介绍一下JS基础中一个重要部分,循环和函数. 04-JS中的循环结构 一.[循环结构的步骤] 1.首先要先声明循环变量. 2.判断循环条件 3.执行循 ...

  2. 基于NIO和BIO的两种服务器对比

    基于BIO的服务器,服务端可能要同时保持几百万个HTTP连接,而这些连接并不是每时每刻都在传输数据,所以这种情况不适合使用BIO的服务器:而且需要保证共享资源的同步与安全,这个实现起来相对复杂.这时候 ...

  3. Highway Networks

    一 .Highway Networks 与 Deep Networks 的关系 深层神经网络相比于浅层神经网络具有更好的效果,在很多方面都已经取得了很好的效果,特别是在图像处理方面已经取得了很大的突破 ...

  4. DLL生成与使用的全过程

    由dll导出的lib文件: 包含了每一个dll导出函数的符号名和可选择的标识号以及dll文件名,不含有实际的代码(这里的lib文件和静态库是不一样的),其中的导出导入函数都 是跳转指令,直接跳转到DL ...

  5. H5页面解决IOS进入不自动播放问题(微信内)

    废话少说,直接上代码. 主要还是调用微信的jdk做兼容处理.,安卓可自动播放. ($(function(){ $(function(){ /* ** 复选框*/ $('.ul-radio').on(' ...

  6. .Net 内存对象分析

    在生产环境中,通过运行日志我们会发现一些异常问题,此时,我们不能直接拿VS远程到服务器上调试,同时日志输出的信息无法百分百反映内存中对象的状态,比如说我们想查看进程中所有的Socket连接状态.服务路 ...

  7. FastDFS 分布式文件系统的安装与使用

    跟踪服务器:192.168.152.129 (centos1) 存储服务器:192.168.152.130 (centos2) 环境:CentOS 6.6 用户:root 数据目录:/fastdfs ...

  8. SQLserver2008r2安装过程

    首先,下载SQLserver2008的安装包,下载完成打开是以下界面 点击开始安装,随着安装进程,点下一步 . 接着来到设置角色的过程,点击SQL功能安装 然后按下一步,来到功能选择,点击" ...

  9. MyEclipse的JQuery.min.js报错红叉解决办法

    MyEclipse的JQuery.min.js报错红叉解决办法 1.选中报错的jquery文件"jquery-1.2.6.min.js".2.右键选择 MyEclipse--> ...

  10. Python自学笔记-生成器(来自廖雪峰的官网Python3)

    感觉廖雪峰的官网http://www.liaoxuefeng.com/里面的教程不错,所以学习一下,把需要复习的摘抄一下. 以下内容主要为了自己复习用,详细内容请登录廖雪峰的官网查看. 生成器 通过列 ...