A strange lift

Time Limit : 2000/1000ms (Java/Other)   Memory Limit : 65536/32768K (Java/Other)
Total Submission(s) : 56   Accepted Submission(s) : 15

Font: Times New Roman | Verdana | Georgia

Font Size: ← →

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

用广搜,注意标记

#include<cstdio>
#include<cstring>
#include<queue>
#include<cstdlib>
using namespace std;
int map[205];
int floor[205];
int n;
struct Node
{int x,step;
}info;
bool f(int x)
{if(x<1||x>n){return false;}return true;
}
int bfs(int sx,int sy)
{info.x=sx,info.step=0;queue<Node>q;q.push(info);while(!q.empty()){Node pos=q.front();q.pop();if(pos.x==sy){return pos.step;}info.x=pos.x+floor[pos.x];if(f(info.x)&&!map[info.x]){map[info.x]=1;                //标记info.step=pos.step+1;q.push(info);}info.x=pos.x-floor[pos.x];if(f(info.x)&&!map[info.x]){map[info.x]=1;                          //标记info.step=pos.step+1;q.push(info);}}return -1;
}
int main()
{int A,B;while(scanf("%d",&n)!=EOF&&n){memset(map,0,sizeof(map));scanf("%d%d",&A,&B);map[A]=1;for(int i=1;i<=n;i++){scanf("%d",&floor[i]);}printf("%d\n",bfs(A,B));}return 0;
}

转载于:https://www.cnblogs.com/gantaiyuan/p/3211963.html

hdu 1548 A strange lift相关推荐

  1. hdu 1548 A strange lift (BFS)

    解题思路:BFS 注意点:每次查询结束后,要清空队列 #include <iostream> #include <queue> #include <cstring> ...

  2. A strange lift HDU - 1548(基础广搜)

    There is a strange lift.The lift can stop can at every floor as you want, and there is a number Ki(0 ...

  3. HDU A strange lift

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

  4. SDAU 搜索专题 13 A strange lift

    1:问题描述 Problem Description There is a strange lift.The lift can stop can at every floor as you want, ...

  5. HDU1548:A strange lift(Dijkstra或BFS)

    题目链接 http://acm.hdu.edu.cn/showproblem.php?pid=1548 题意:电梯每层有一个数,例如第n层有个数k, 那么这一层只能上k层或下k层,但是不能低于一层或高 ...

  6. poj 1564 Sum It Up | zoj 1711 | hdu 1548 (dfs + 剪枝 or 判重)

    Sum It Up Time Limit : 2000/1000ms (Java/Other)   Memory Limit : 65536/32768K (Java/Other) Total Sub ...

  7. 【HDU - 2899】 Strange fuction(二分或三分,求导)

    题干: Now, here is a fuction:    F(x) = 6 * x^7+8*x^6+7*x^3+5*x^2-y*x (0 <= x <=100)  Can you fi ...

  8. hdu 1548 升降梯

    题目大意:有一个升降机,它有两个按钮UP和DOWN,给你一些数i表示层数,并且每层对应的Ki,如果按UP按钮,会从第i层升到第i+Ki层:如果按了DOWN则会从第i层降到第i-Ki层:并规定能到的层数 ...

  9. 杭电OJ分类题目(4)-Graph

    原题出处:HDOJ Problem Index by Type,http://acm.hdu.edu.cn/typeclass.php 杭电OJ分类题目(4) HDU Graph Theory - U ...

最新文章

  1. Django Admin 录入中文错误解决办法
  2. 商汤首付56亿!上海建成亚洲最大AI“发电厂”,万亿参数大模型训练无压力
  3. C++ 动态内存管理:c/c++的动态内存管理,new/delete,operator new/delete,placement-new, 内存泄漏
  4. linux 查看内存用量_正确计算linux系统内存使用率
  5. 【新年巨献】计算机类国际英文EI(JA)期刊限量推荐
  6. 一、Ajax跨域访问
  7. hausaufgabe--python 20- usage of Closure
  8. 2.python中的矩阵、多维数组----numpy
  9. linux里用户权限:~$,/$,~#,/#的区别与含义
  10. 已修复的bug: 简书文章长标题换行异常,标题配图异常,首页配图异常
  11. 实践项目2 图书管理系统
  12. 汉王考勤系统服务器IP,汉王人脸识别考勤客户端使用说明
  13. vs2005安装和部署项目
  14. oracle的dual用法
  15. 运动数据采集,医疗数据采集
  16. 重磅推出:2019中国开源年度报告
  17. 【乐理学习】音程 升降调 力度标记
  18. 配音软件永久免费的有哪些?这几款免费的配音软件绝对适合你
  19. 1248 NYOJ 海岛争霸
  20. 1007: 求正方形面积

热门文章

  1. Java实现markdown转html,并且生成TOC目录
  2. php自定义函数出现乱码,php的imagettftext 函数出现乱码的解决方法
  3. 计算机学具制作,一种具有防护功能的计算机编程学具的制作方法
  4. AOP通过开启注解方式自动注入值
  5. nginx添加stream模块1.20
  6. [k8s] 第八章 数据存储
  7. Docker(五):Docker Volume
  8. 获得虚拟服务器相对路径,在web应用中获取相对路径和绝对路径
  9. python矩阵处理库_用于处理非常大的矩阵的Python库
  10. 清华大佬教你一招最便捷搭建 Zookeeper 的方法!