A strange lift



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

Total Submission(s): 14947    Accepted Submission(s): 5654



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

//这题是电梯up down,up层数i+ki down i-ki 注意边界  求a->b最少操作次数

  1. #include <stdio.h>
  2. #include <string.h>
  3. #include <queue>
  4. using namespace std;
  5. int n,start,stop;
  6. int p[210];
  7. int vis[210];
  8. struct Node
  9. {
  10. int floor;
  11. int step;
  12. };
  13.  
  14. int bfs(int flo)
  15. {
  16. queue <Node> q;
  17. memset(vis,0,sizeof(vis));
  18. Node a;
  19. a.floor=flo,a.step=0;
  20. q.push(a);
  21. while(!q.empty())
  22. {
  23. Node b=q.front();
  24. q.pop();
  25. vis[b.floor]=1;
  26. if(b.floor==stop) return b.step;
  27. for(int i=0;i<2;i++) //0 up 1 down
  28. {
  29. Node c=b;
  30. if(i==0)
  31. c.floor=c.floor+p[c.floor];
  32. else
  33. c.floor=c.floor-p[c.floor];
  34.  
  35. if(!vis[c.floor]&&c.floor>=1&&c.floor<=n)
  36. {
  37. c.step++;
  38. q.push(c);
  39. vis[c.floor]=1;
  40. }
  41. }
  42. }
  43. return -1;
  44. }
  45.  
  46. int main()
  47. {
  48. while(~scanf("%d",&n)&&n)
  49. {
  50. scanf("%d%d",&start,&stop);
  51. for(int i=1;i<=n;i++)
  52. scanf("%d",&p[i]);
  53. printf("%d\n",bfs(start));
  54. }
  55. return 0;
  56. }

hdu 1548的更多相关文章

  1. cogs 364. [HDU 1548] 奇怪的电梯 Dijkstra

    364. [HDU 1548] 奇怪的电梯 ★   输入文件:lift.in   输出文件:lift.out   简单对比时间限制:1 s   内存限制:128 MB [问题描述] 呵呵,有一天我做了 ...

  2. hdu 1548 楼梯 bfs或最短路 dijkstra

    http://acm.hdu.edu.cn/showproblem.php?pid=1548 Online Judge Online Exercise Online Teaching Online C ...

  3. hdu 1548 A strange lift

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

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

  5. HDU 1548 A strange lift (Dijkstra)

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

  6. hdu 1548 A strange lift (dijkstra算法)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1548 题目大意:升降电梯,先给出n层楼,然后给出起始的位置,即使输出从A楼道B楼的最短时间. 注意的几 ...

  7. hdu 1548 A strange lift 解题报告

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1548 题目意思:给出 n 个 floor 你,每个floor 有一个数k,按下它可以到达 floor ...

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

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

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

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

随机推荐

  1. hnust 不爱学习的小w

    问题 C: 不爱学习的小W 时间限制: 2 Sec  内存限制: 64 MB提交: 1431  解决: 102[提交][状态][讨论版] 题目描述 “叮铃铃”上课了,同学们都及时到了教室坐到了座位上, ...

  2. [git 学习篇]版本回退

    再次修改readme.txt ,并将其提交成功 $ git add readme.txt $ git commit -m "append GPL" [master ] append ...

  3. HashSet源码分析 jdk1.6

    Set的特点:Set元素无顺序,且元素不可以重复. 1.定义 public class HashSet<E> extends AbstractSet<E> implements ...

  4. 【Luogu】P2709小B的询问(莫队算法)

    题目链接 md,1A率等于0. 烦死. 终于搞到一道莫队了qwq. 先对区间分块再按照块编号为第一关键字,右端点为第二关键字排序,然后每次端点移动1乱搞. 然后……就wa了. 然后有很多细节需要注意q ...

  5. LibreOJ2044 - 「CQOI2016」手机号码

    Portal Description 给出两个十一位数\(L,R\),求\([L,R]\)内所有满足以下两个条件的数的个数. 出现至少\(3\)个相邻的相同数字: 不能同时出现\(4\)和\(8\). ...

  6. THUWC2018爆0记

    Day-2 心里想到要明天就要出发,去长沙膜拜各省dalao,心里挺激动,, 细细整理着行囊 Day-1 一觉睡到天明,正好是星期一,大家都要上课,也没怎么听物理老师讲什么. 到了9:30,就背着包拖 ...

  7. Eclipse + Jersey 发布RESTful WebService(一)了解Maven和Jersey,创建一个WS项目(成功!)

    一.下文中需要的资源地址汇总 Maven Apache Maven网站 http://maven.apache.org/ Maven下载地址: http://maven.apache.org/down ...

  8. [USACO12DEC]第一!First! (Trie树,拓扑排序)

    题目链接 Solution 感觉比较巧的题啊... 考虑几点: 可以交换无数次字母表,即字母表可以为任意形态. 对于以其他字符串为前缀的字符串,我们可以直接舍去. 因为此时它所包含的前缀的字典序绝对比 ...

  9. 【BZOJ1579】Revamping Trails(分层图,最短路,堆)

    题意:每天,农夫John需要经过一些道路去检查牛棚N里面的牛. 农场上有M(1<=M<=50,000)条双向泥土道路,编号为1..M. 道路i连接牛棚P1_i和P2_i (1 <= ...

  10. 转 Python爬虫入门七之正则表达式

    静觅 » Python爬虫入门七之正则表达式 1.了解正则表达式 正则表达式是对字符串操作的一种逻辑公式,就是用事先定义好的一些特定字符.及这些特定字符的组合,组成一个“规则字符串”,这个“规则字符串 ...