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,和长度为n的k数组。共有n层电梯,求从a层到b层至少按几次电梯。电梯只能按向上或者向下,在i层按上则上升ki层,按下则下降ki层,电梯不能到达1层以下,也不能到达n层以上。
思路:这道算是广搜的基础题了,深搜我试了试,超时。已经走过的楼层就不用走了,如果没有可以走的楼层了还没有到达目标点,那么就是没有办法到达。
代码如下:

#include<bits/stdc++.h>
#define ll long long
#define Pll pair<int,int>
using namespace std;const int maxx=2e2+10;
int a[maxx],vis[maxx];
int n,s,e;inline int bfs()
{vis[s]=1;queue<Pll> q;q.push(make_pair(s,0));while(q.size()){Pll u=q.front();q.pop();if(u.first==e) return u.second;int rr=u.first+a[u.first];if(rr<=n&&vis[rr]==0) vis[rr]=1,q.push(make_pair(rr,u.second+1));rr=u.first-a[u.first];if(rr>=1&&vis[rr]==0) vis[rr]=1,q.push(make_pair(rr,u.second+1));}return -1;
}
int main()
{while(scanf("%d",&n),n){scanf("%d%d",&s,&e);memset(vis,0,sizeof(vis));for(int i=1;i<=n;i++) scanf("%d",&a[i]);printf("%d\n",bfs());}return 0;
}

努力加油a啊,(o)/~

A strange lift HDU - 1548(基础广搜)相关推荐

  1. Hdu 1072 【广搜】.cpp

    题意: 给出一个n*m的矩阵, 0 表示不可走 1 表示可走 2 表示起点 3 表示终点 4 表示可走且走到这一步可以满血 某人一开始有6滴血,走一步少一滴..到0就死了.. 可以走到4的位置满血再走 ...

  2. hdu 1548 A strange lift

    A strange lift Time Limit : 2000/1000ms (Java/Other)   Memory Limit : 65536/32768K (Java/Other) Tota ...

  3. [HDU] 2612 Find a way - 用单源最短论经模拟的简单广搜

    题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=2612 方法:其实就是从两个点分别探寻单源最短路径,两个点到同一个目标位置的最短路径都求出来,相加,然 ...

  4. [HDU] 1181 变形课-简单建模后广搜

    题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=1181 方法:以所有咒语看做一条起点为首字母,终点为末字母的有向边来建立有向图.然后在图中判断目标路径 ...

  5. 提高篇 第一部分 基础算法 第4章 广搜的优化技巧

    LOJ 一本通一句话题解系列: - ~victorique~ - 博客园 LOJ 一本通一句话题解系列: - ~victorique~ - 博客园 「一本通 1.4 例 1」电路维修 电路维修 (广搜 ...

  6. POJ 3278 / hdu 2717 Catch That Cow (广搜)

    POJ 3278 HDU 2717 广搜题,用一个数组标记就可以过,不标记的话会超内存. 另外,poj的数据要比hdu强一些,比如0 100,这种数据.不特判的话会RE.不过如果不特判,在poj上用C ...

  7. hdu 2612 Find a way (广搜)

    Problem Description Pass a year learning in Hangzhou, yifenfei arrival hometown Ningbo at finally. L ...

  8. hdu 1253 胜利大逃亡 (广搜)

    题目链接 Problem Description Ignatius被魔王抓走了,有一天魔王出差去了,这可是Ignatius逃亡的好机会. 魔王住在一个城堡里,城堡是一个ABC的立方体,可以被表示成A个 ...

  9. HDU A strange lift

    A strange lift Time Limit : 2000/1000ms (Java/Other)   Memory Limit : 65536/32768K (Java/Other) Tota ...

最新文章

  1. .NET 框架中的 WMI 命名空间
  2. python工程师收入-Python工程师薪水居然这么高
  3. [转帖]鲍鹏山:我们培养了很多高学历的野蛮人
  4. STM32CubeMx官网下载HAL库文档资料
  5. 安装mysql 5.7.19 winx64解压缩版
  6. android端与windows端通信中文乱码问题
  7. qt_2_QT5.6+VS2015配置
  8. Zip 文件覆盖漏洞曝光,Java、.NET、Go 生态集体中枪
  9. VMware网络连接模式——桥接模式、NAT模式以及仅主机模式的介绍和区别
  10. 网管工具使用与技巧大全
  11. GB28181 PTZCmd控制指令笔记
  12. Oracle下载账户
  13. 护理教育学考试复习题
  14. 【MatLab fdatool】Butterworth 低通 带阻 滤波器 (MatLab C++)
  15. 谷歌浏览器所有页面都崩溃,设置都崩溃
  16. snapchat忘记账户_如何删除您的Snapchat帐户
  17. 山西大学计算机与信息技术学院张超,张超 - 山西大学 - 计算机与信息技术学院...
  18. AI绘图:chatGpt,midjourney, discord
  19. 消息中间件合集:MQ(ActiveMQ/RabbitMQ/RocketMQ)+Kafka+笔记
  20. 进阶Django(一):Django与Ajax

热门文章

  1. C++中重写与覆写(虚函数virtual)的区别
  2. uni-app 实现递增递减(js)
  3. ios14.3开发之使用纯代码创建UITabbarController
  4. r语言 回归分析 分类变量_R语言 | 回归分析(二)
  5. python点名代码_基于python tkinter的点名小程序功能的实例代码
  6. 导入第三方依赖库slidingmenu
  7. QStyleOptionGraphicsItem实现自绘按钮悬浮按下状态
  8. Android开发之Retrofit常见错误@FieldMap parameters can only be used with form encoding. (parameter #1)
  9. 高一计算机旅游计划作业,作业:旅行计划
  10. 韩国防部长会见美驻韩大使 或谈韩日舰机矛盾