题目描述

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. 创建透明CEdit控件

    1.在资源编辑器中将Edit控件的Border设为none(当然也可以在代码中修改窗口样式) 2.在重载CEdit的类中响应MSG_OCM_CTLCOLOREDIT消息,在处理函数中将背景模式设为透明 ...

  2. MVC jsonModelBuilder

    /// <summary> /// JsonModelBinderAttribute /// author:BearLee /// 2015/5/20 11:48:40 /// </ ...

  3. mongodb安装 win7版

    首先下载mongdb的zip包,可以到:http://www.mongodb.org/downloads.选择32位或64位的. 在D盘创建mongodb文件夹,d:\mongodb.把解压里面所有的 ...

  4. Python 邮件发送

    python发送各类邮件的主要方法   python中email模块使得处理邮件变得比较简单,今天着重学习了一下发送邮件的具体做法,这里写写自己的的心得,也请高手给些指点.     一.相关模块介绍 ...

  5. B - 瑶瑶带你玩激光坦克

    B - 瑶瑶带你玩激光坦克 Time Limit: 2000/1000MS (Java/Others)    Memory Limit: 256000/128000KB (Java/Others) S ...

  6. 11--Python 备份文件程序

    最近看了下<A Byte of Python>, 看见一个非常有意思的程序,用python进行文件行备份的练习程序, 自己在机器上敲代码运行了一遍,结果出现了一个小问题,路径出错--&qu ...

  7. delete、truncate与drop的区别

    转自:SQL truncate .delete与drop区别 相同点: 1.truncate和不带where子句的delete.以及drop都会删除表内的数据. 2.drop.truncate都是DD ...

  8. html 任意表格

    <!DOCTYPE html><html>    <head>        <meta charset="utf-8" />   ...

  9. svnserve: E000098: 不能绑定服务器套接字: 地址已在使用

    百度一下,所有资料都是 1.查找出目前正在使用的svnserve进程,然后kill掉 ps -aux | grep svnserve kill -9  xxx    // xxx代表svnserve对 ...

  10. IoC容器Autofac正篇之解析获取(六)

    解析获取的方式有如下几种: Resolve class Program { static void Main(string[] args) { var builder = new ContainerB ...