zoukankan      html  css  js  c++  java
  • hdu 1548 A strange lift

    http://acm.hdu.edu.cn/showproblem.php?pid=1548

    A strange lift

    Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
    Total Submission(s): 8249    Accepted Submission(s): 3136


    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
     
    Recommend
    8600
     比较水,bfs,和前面catch that cow 差不多,直接上代码
     1 #include<stdio.h>
     2 #include<queue>
     3 #include<string.h>
     4 using namespace std;
     5 struct node
     6 {
     7      int x;
     8      int step;
     9 
    10 }info;
    11 int n,a,b,ans[1000],vis[1000],flag,cnt;
    12 void bfs(int x)
    13 {
    14   info.x= x; 
    15   int i;
    16   info.step=0;
    17   vis[x]=1;
    18   queue<node>q;
    19   q.push(info) ;
    20   while(!q.empty())
    21   {
    22      
    23        node pos;
    24        pos=q.front();
    25        q.pop();
    26        if(pos.x==b)
    27        {
    28             flag=1;
    29             cnt=pos.step;
    30         //    return;
    31        }
    32        for(i=0;i<2;i++)
    33        {
    34             int xx,step;
    35             if(i==0)
    36             {
    37                  xx=pos.x+ans[pos.x];
    38                  step=pos.step+1;
    39 
    40             }
    41             else
    42             {
    43                  xx=pos.x-ans[pos.x];
    44                  step=pos.step+1;
    45             }
    46             if(xx>=1&&xx<=b&&!vis[xx])
    47             {
    48             //    printf("xx=%d,step=%d
    ",xx,step);
    49                  vis[xx]=1;
    50                  info.x=xx;
    51                  info.step=step;
    52                  q.push(info);
    53             }
    54        }
    55   }
    56   //return;
    57 }
    58 int main()
    59 {
    60      while(scanf("%d",&n)!=EOF&&n)
    61      {
    62          scanf("%d %d",&a,&b);
    63          
    64           memset(vis,0,sizeof(vis));
    65           int i;
    66           flag=0;
    67           cnt=0;
    68           for(i=1;i<=n;i++)
    69           scanf("%d",&ans[i]);
    70           bfs(a);
    71           if(flag)
    72           printf("%d
    ",cnt);
    73           else
    74           printf("-1
    ");
    75           }
    76      return 0;
    77 }
    View Code
  • 相关阅读:
    OpenSSL生成rsa密钥对
    RabbitMQ工作模式
    加密解密
    MongDB优化
    java线程进程
    MongoDB数据类型
    获取指针指向的内存大小方法
    [教程] 让Mac OS 10.x.x安装在Vmware虚拟机上!
    安装好的苹果系统部分截图
    VC中MFC程序手动控制最近文件列表
  • 原文地址:https://www.cnblogs.com/llei1573/p/3198717.html
Copyright © 2011-2022 走看看