zoukankan      html  css  js  c++  java
  • HDU 1548 A strange lift (bfs / 最短路)

    A strange lift

    Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)


    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
     
     
    题目大意:
          有一个电梯,在不同的楼层,可以上升或下降的层数是不同的
           (例如第一层有一个整数3,说明他可以上升3层或者下降3层),
          现在给你一个起始位置和最终位置,问至少需要几步能够到达最终位置。
     
    解题思路:
          该题可以用最短路,也可以用广搜来写
           最短路:需要注意的是这题是单向边
           bfs:每次只需要搜索两个方向
    最短路算法:
     1 #include <stdio.h>  
     2 #include <string.h>  
     3 #include <algorithm>  
     4 using namespace std;  
     5   
     6 const int inf = 1<<30;  
     7   
     8 int n;  
     9 int map[205][205];  
    10 int a[205],cnt;  
    11 int vis[205],cast[205];  
    12   
    13 void Dijkstra(int s,int e)   //迪杰斯特拉 
    14 {  
    15     int i,j,min,pos;  
    16     memset(vis,0,sizeof(vis));  
    17     for(i = 0; i<n; i++)  
    18         cast[i] = map[s][i];  
    19     cast[s] = 0;  
    20     vis[s] = 1;  
    21     for(i = 1; i<n; i++)  
    22     {  
    23         min = inf;  
    24         for(j = 0; j<n; j++)  
    25         {  
    26             if(cast[j]<min && !vis[j])  
    27             {  
    28                 pos = j;  
    29                 min = cast[j];  
    30             }  
    31         }  
    32         if(min == inf)  
    33             break;  
    34         vis[pos] = 1;  
    35         for(j = 0; j<n; j++)  
    36         {  
    37             if(cast[pos]+map[pos][j]<cast[j] && !vis[j])  
    38                 cast[j] = cast[pos]+map[pos][j];  
    39         }  
    40     }  
    41 }  
    42   
    43 int main()  
    44 {  
    45     int i,j,s,e,x,y;  
    46     while(~scanf("%d",&n),n)  
    47     {  
    48         scanf("%d%d",&s,&e);  
    49         s--,e--;  
    50         for(i = 0; i<n; i++)      
    51             for(j = 0; j<n; j++)  
    52                 map[i][j] = inf;  
    53         for(i = 0; i<n; i++)  //单向边 
    54         {  
    55             scanf("%d",&a[i]);  
    56             if(i+a[i]<n)      //上升
    57                 map[i][i+a[i]] = 1;   
    58             if(i-a[i]>=0)    //下降 
    59                 map[i][i-a[i]] = 1;  
    60         }  
    61         Dijkstra(s,e);  
    62         printf("%d
    ",cast[e]==inf?-1:cast[e]);  
    63     }  
    64   
    65     return 0;  
    66 }

    广搜 bfs:

     1 #include <stdio.h>
     2 #include <string.h>
     3 #include <algorithm>
     4 #include <queue>
     5 using namespace std;
     6 
     7 int a[210];  // 在第i层可以上升和下降的层数
     8 int f[210];  // 记录步数
     9 int n,x,y;
    10 void bfs()
    11 {
    12     int b;
    13     memset(f,0,sizeof(f));
    14     queue<int > q;
    15     q.push(x);
    16     f[x] = 1;
    17     if (x == y)
    18         return ;
    19     while (!q.empty())
    20     {
    21         b = q.front();
    22         if (b+a[b] <= n && !f[b+a[b]])  // 判断上升之后是否满足条件
    23         {
    24             q.push(b+a[b]);
    25             f[b+a[b]] = f[b] + 1;
    26             if (b+a[b] == y)     // 到达之后直接跳出
    27                 return ;
    28         }
    29         if (b-a[b] > 0 && !f[b-a[b]])   // 判断下降之后是否满足条件
    30         {
    31             q.push(b-a[b]);
    32             f[b-a[b]] = f[b] + 1;
    33             if (b-a[b] == y)     // 到达之后直接跳出
    34                 return ;
    35         }
    36         q.pop();
    37     }
    38     return ;
    39 }
    40 int main ()
    41 {
    42     int i,j;
    43     while (scanf("%d",&n),n)
    44     {
    45         scanf("%d%d",&x,&y);
    46         for (i = 1; i <= n; i ++)
    47             scanf("%d",&a[i]);
    48             
    49         bfs();
    50         if (f[y] != 0)
    51             printf("%d
    ",f[y]-1);
    52         else
    53             printf("-1
    ");
    54     }
    55     return 0;
    56 }
  • 相关阅读:
    低代码能做什么?这家服务商用钉钉宜搭打造了智慧医院管理应用
    【深度】阿里巴巴万级规模 K8s 集群全局高可用体系之美
    如何做规划?分享2种思维和4个方法
    配置审计(Config)配合开启OSS防盗链功能
    被解救的代码
    物联网海量时序数据存储有哪些挑战?
    Serverless:这真的是未来吗?(一)
    数据库学习之MySQL进阶
    网页三剑客之CSS
    网页三剑客之HTML
  • 原文地址:https://www.cnblogs.com/yoke/p/5929539.html
Copyright © 2011-2022 走看看