题目描述

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 is on floor A,and you want to go to floor B,how many times at least he havt 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

大意

有一个电梯,一共有N层,每一层有两个按钮:上升k[i]层,下降k[i]层。

现在要求求从A到B层最少需要按多少次按钮?

思路

很明显的广搜题目(第一个搜索到的可行结果即为最佳解)

但在具体实现的时候遇到了问题,第一遍写的时候我的入列条件是只要要到达的楼层在1~N范围之内就入列,可这样提交之后提示内存超出限制。这是因为重复计算了许多遍,由于一个楼层的两个状态是固定的,因此只需要计算一遍就可。因此入队的条件又加了一个,此楼层未经过。然后就AC了

AC代码

  1. #include<iostream>
  2. #include<queue>
  3. #include<stdio.h>
  4. using namespace std;
  5. struct floor
  6. {
  7. int x;
  8. int y;
  9. }n1,n2,tem;
  10. int n[201],jilu[201];
  11. int main(){
  12. //freopen("date.in","r",stdin);
  13. //freopen("date.out","w",stdout);
  14. int N,A,B,flag;
  15. while(cin>>N&&N!=0){
  16. flag=0;
  17. cin>>A>>B;
  18. for(int i=1;i<=N;i++){
  19. cin>>n[i];
  20. jilu[i]=0;
  21. }
  22. n1.x=A;n1.y=0;
  23. queue<floor>f;
  24. f.push(n1);
  25. while(!f.empty()){
  26. tem=f.front();
  27. f.pop();
  28. if(tem.x==B){
  29. flag=1;
  30. break;
  31. }
  32. n1.x=tem.x+n[tem.x];
  33. n2.x=tem.x-n[tem.x];
  34. n1.y=tem.y+1;
  35. n2.y=tem.y+1;
  36. if(n1.x>0&&n1.x<=N&&!jilu[n1.x]){
  37. f.push(n1);
  38. jilu[n1.x]=1;
  39. }
  40. if(n2.x>0&&n2.x<=N&&!jilu[n2.x]){
  41. f.push(n2);
  42. jilu[n2.x]=1;
  43. }
  44. }
  45. if(flag)
  46. cout<<tem.y<<endl;
  47. else cout<<-1<<endl;
  48. }
  49. }

acm课程练习2--1013(同1014)的更多相关文章

  1. ACM课程学习总结

    ACM课程学习总结报告 通过一个学期的ACM课程的学习,我学习了到了许多算法方面的知识,感受到了算法知识的精彩与博大,以及算法在解决问题时的巨大作用.此篇ACM课程学习总结报告将从以下方面展开: 学习 ...

  2. ACM课程总结

    当我还是一个被P哥哥忽悠来的无知少年时,以为编程只有C语言那么点东西,半个学期学完C语言的我以为天下无敌了,谁知自从有了杭电练习题之后,才发现自己简直就是渣渣--咳咳进入正题: STL篇: 成长为一名 ...

  3. acm课程练习2--1005

    题目描述 Mr. West bought a new car! So he is travelling around the city.One day he comes to a vertical c ...

  4. acm课程练习2--1003

    题目描述 My birthday is coming up and traditionally I'm serving pie. Not just one pie, no, I have a numb ...

  5. acm课程练习2--1002

    题目描述 Now, here is a fuction:  F(x) = 6 * x^7+8x^6+7x^3+5x^2-yx (0 <= x <=100)Can you find the ...

  6. acm课程练习2--1001

    题目描述 Now,given the equation 8x^4 + 7x^3 + 2x^2 + 3x + 6 == Y,can you find its solution between 0 and ...

  7. HDU ACM 题目分类

    模拟题, 枚举1002 1004 1013 1015 1017 1020 1022 1029 1031 1033 1034 1035 1036 1037 1039 1042 1047 1048 104 ...

  8. 杭电ACM题单

    杭电acm题目分类版本1 1002 简单的大数 1003 DP经典问题,最大连续子段和 1004 简单题 1005 找规律(循环点) 1006 感觉有点BT的题,我到现在还没过 1007 经典问题,最 ...

  9. 杭电acm习题分类

    专注于C语言编程 C Programming Practice Problems (Programming Challenges) 杭电ACM题目分类 基础题:1000.1001.1004.1005. ...

随机推荐

  1. PHP下用正则表达式分割preg_split、替换reg_replace、匹配preg_match_all等出现乱码的解决方法

    操作前声明操作字符的编码: mb_regex_encoding('utf-8'); $arr = preg_split('/[\n,]/u',$data['name'] ,0, PREG_SPLIT_ ...

  2. JQ N级导航

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...

  3. java 内部类(摘抄自网络)

    Java内部类 1.内部类分为成员内部类.静态嵌套类.方法内部类.匿名内部类. 几种内部类的共性: A.内部类仍然是一个独立的类,在编译之后会内部类会被编译成独立的.class文件,但是前面冠以外部类 ...

  4. Chapter 15_2 编写模块的基本方法

    在Lua中创建一个模块最简单的方法是:创建一个table. 并将所有需要导出的函数放入其中,最后返回这个table. 下例中的inv声明为程序块的局部变量,就是将其定义成一个私有的名称: local ...

  5. Chapter 15_0 模块和包

    通常,Lua不会设置规则,相反会提供很多强有力的机制来使开发者有能力实现出最适应的规则. 然而,这种方法对于模块就不行了.模块系统的一个主要目标是允许以不同的形式来共享代码. 但若没有一项公共的规则就 ...

  6. java反射机制(2)

    首先,我们在开始前提出一个问题: 1.在运行时,对于一个java类,能否知道属性和方法:能否去调用它的任意方法? 答案是肯定的. 本节所有目录如下: 什么是JAVA的反射机制 JDK中提供的Refle ...

  7. 与导航栏下控件的frame相关的edgesForExtendedLayout、translucent、extendedLayoutIncludesOpaqueBars、automaticallyAdjustsScrollViewInsets等几个属性的详解

    在引入了导航控制器UINavigationController和分栏控制器UITabBarController之后,我们在设置控件的frame的时候就需要注意避开导航栏UINavigationBar ...

  8. Android 中OKHttp请求数据get和post

    1:在Android Studio 的 build.gradle下  添加 然后再同步一下 compile 'com.squareup.okhttp:okhttp:2.4.0'compile 'com ...

  9. ios 基础数据类型

    1:NSString http://blog.csdn.net/likendsl/article/details/7417878 http://xys289187120.blog.51cto.com/ ...

  10. Java编译成功,用java 运行class时出现错误解决方法

    java -classpath class file's address; filename