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. [LeetCode] 344 Reverse String && 541 Reverse String II

    原题地址: 344 Reverse String: https://leetcode.com/problems/reverse-string/description/ 541 Reverse Stri ...

  2. Java基础——集合源码解析 List List 接口

    今天我们来学习集合的第一大体系 List. List 是一个接口,定义了一组元素是有序的.可重复的集合. List 继承自 Collection,较之 Collection,List 还添加了以下操作 ...

  3. PyTorch教程之Neural Networks

    我们可以通过torch.nn package构建神经网络. 现在我们已经了解了autograd,nn基于autograd来定义模型并对他们有所区分. 一个 nn.Module模块由如下部分构成:若干层 ...

  4. 在 JavaScript 中使用构造器函数模拟类

    今天,我们要讲的是在 JavaScript 中使用构造器函数(construcor function)模拟类. 构造器函数简介 你可以使用 ES6 的 class 关键字来实现类,不过我建议你使用传统 ...

  5. hdu3018欧拉回路题

    Ant Trip Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total S ...

  6. FastDFS的安装步骤

    1.安装相关环境 yum install -y gcc-c++ yum -y install libevent yum install -y pcre pcre-devel yum install - ...

  7. 上传文件没有写权限Access to the path is denied

    Access to the path is denied. asp.net程序目录放在系统盘,ntfs格式. 程序中对cfg.xml有写入操作. 运行的时候出现了这个问题. 在我自己的机器上没有问题 ...

  8. 浅析php curl_multi_*系列函数进行批量http请求

    何起: 一系列 数量很大 数据不热 还希望被蜘蛛大量抓取的页面,在蜘蛛抓取高峰时,响应时间会被拉得很高. 前人做了这样一个事儿:页面分3块,用3个内部接口提供,入口文件用curl_multi_*系列函 ...

  9. C#综合揭秘——细说多线程(二)

    /* 异步写入 FileStream中包含BeginWrite.EndWrite 方法可以启动I/O线程进行异步写入. public override IAsyncResult BeginWrite ...

  10. oracle开启一个用户

    我的工具,PL/SQL Developer(其他工具大同小意) 1.用系统管理员账号登入数据库    账号是:sys,   connect as:sysdba  登入进入如下页面. 2.查看所有用户( ...