zoukankan      html  css  js  c++  java
  • acm课程练习2--1013(同1014)

    题目描述

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




  • 相关阅读:
    VS2019基于windows类库创建单元测试报错解决方法
    scp 跨机远程拷贝
    java递归查询部门
    使用jOrgChart插件生成树形图
    让你页面上所有的非http请求强制转成https请求
    js对金额格式化————脑子不好使总忘
    去除相邻的重复元素 122345556 -> 123456
    打包时无法引入外部jar
    计算list里连续出现的值
    VirtualBox安装Centos双网卡(访问外网+固定IP)
  • 原文地址:https://www.cnblogs.com/liuzhanshan/p/5427972.html
Copyright © 2011-2022 走看看