zoukankan      html  css  js  c++  java
  • HDU-1548 A strange lift(单源最短路 或 BFS)

    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
     
     
    题意:一个n层的奇怪的电梯,每层有两个键,上、下。在第i层有一个数字啊a[i],表示能从这一层到第i-a[i]层和第i+a[i]层。问从指定层到指定层的最少按键次数。
    解析:两种方法。BFS和单源最短路。
     
    代码如下:
     
    BFS:
     1 # include<iostream>
     2 # include<cstdio>
     3 # include<queue>
     4 # include<cstring>
     5 # include<algorithm>
     6 using namespace std;
     7 struct node
     8 {
     9     int pos,time;
    10 };
    11 int n,a,b;
    12 int f[205];
    13 int mark[205];
    14 void BFS()
    15 {
    16     queue<node>q;
    17     memset(mark,0,sizeof(mark));
    18     node now;
    19     now.pos=a,now.time=0;
    20     q.push(now);
    21     mark[a]=1;
    22     while(!q.empty())
    23     {
    24         now=q.front();
    25         q.pop();
    26         if(now.pos==b){
    27             printf("%d
    ",now.time);
    28             return ;
    29         }
    30         node nxt;
    31         nxt.pos=now.pos+f[now.pos];
    32         nxt.time=now.time+1;
    33         if(nxt.pos<=n&&!mark[nxt.pos]){
    34             q.push(nxt);
    35             mark[nxt.pos]=1;
    36         }
    37         nxt.pos=now.pos-f[now.pos];
    38         nxt.time=now.time+1;
    39         if(nxt.pos>=1&&!mark[nxt.pos]){
    40             q.push(nxt);
    41             mark[nxt.pos]=1;
    42         }
    43     }
    44     printf("-1
    ");
    45 }
    46 int main()
    47 {
    48     while(scanf("%d",&n)&&n)
    49     {
    50         scanf("%d%d",&a,&b);
    51         for(int i=1;i<=n;++i)
    52             scanf("%d",&f[i]);
    53         BFS();
    54     }
    55 }
    View Code

    spfa:

     1 # include<iostream>
     2 # include<cstdio>
     3 # include<queue>
     4 # include<cstring>
     5 # include<algorithm>
     6 using namespace std;
     7 const int INF=1<<29;
     8 int mp[205][205];
     9 int f[205],a,b,n,dis[205];
    10 void init()
    11 {
    12     for(int i=1;i<=n;++i)
    13         for(int j=1;j<=n;++j)
    14             mp[i][j]=(i==j)?0:INF;
    15     for(int i=1;i<=n;++i){
    16         if(i+f[i]<=n)
    17             mp[i][i+f[i]]=1;
    18         if(i-f[i]>=1)
    19             mp[i][i-f[i]]=1;
    20     }
    21 }
    22 void spfa()
    23 {
    24     init();
    25     fill(dis+1,dis+n+1,INF);
    26     queue<int>q;
    27     q.push(a);
    28     dis[a]=0;
    29     while(!q.empty()){
    30         int u=q.front();
    31         q.pop();
    32         for(int i=1;i<=n;++i){
    33             if(dis[i]>dis[u]+mp[u][i]){
    34                 dis[i]=dis[u]+mp[u][i];
    35                 q.push(i);
    36             }
    37         }
    38     }
    39     if(dis[b]==INF)
    40         printf("-1
    ");
    41     else
    42         printf("%d
    ",dis[b]);
    43 }
    44 int main()
    45 {
    46     while(scanf("%d",&n)&&n)
    47     {
    48         scanf("%d%d",&a,&b);
    49         for(int i=1;i<=n;++i)
    50             scanf("%d",f+i);
    51         spfa();
    52     }
    53     return 0;
    54 }
    View Code
     
  • 相关阅读:
    全面了解HTTP和HTTPS(开发人员必备)
    这几款前端必备构建工具合辑,我们帮你整理好了!
    扎心!程序员泪奔的8个瞬间
    Centos7 自定义systemctl服务脚本
    nginx配置优化+负载均衡+动静分离详解
    nginx负载均衡配置
    keepalived高可用反向代理的nginx
    Tomcat相关目录及配置文件
    tomcat快速入门
    基于keepalived双主模型的高可用LVS
  • 原文地址:https://www.cnblogs.com/20143605--pcx/p/4678532.html
Copyright © 2011-2022 走看看