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

    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"? 

    InputThe 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.OutputFor 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
    题解:BFS搜索即可;对于走过的楼层打上标记,记录走的步数;
    参考代码:
     1 #include<iostream>
     2 #include<cstdio>
     3 #include<cstring>
     4 #include<cmath>
     5 #include<cstdlib>
     6 #include<algorithm>
     7 #include<queue>
     8 #include<stack>
     9 #include<set>
    10 #include<vector>
    11 #include<map>
    12 using namespace std;
    13 #define PI acos(-1)
    14 #define EPS 1e-8
    15 typedef pair<int,int> PII;
    16 typedef long long LL;
    17 const int INF=0x3f3f3f3f;
    18 const LL inf=0x3f3f3f3f3f3f3f3fLL;
    19 const int maxn=1e5+10;
    20 struct Node{
    21     int Floor;
    22     int step;
    23 } node;
    24 int a[maxn],v[maxn],n,s,e;
    25 int bfs()
    26 {
    27     queue<Node> q;
    28     node.Floor=s,node.step=0;
    29     q.push(node);v[node.Floor]=1;
    30     while(!q.empty())
    31     {
    32         Node tem=q.front(); q.pop();
    33         Node tp;
    34         if(tem.Floor==e) return tem.step;   
    35         for(int i=0; i<2; i++)
    36         {
    37             if(i==0) tp.Floor=tem.Floor+a[tem.Floor-1];
    38             else tp.Floor=tem.Floor-a[tem.Floor-1];
    39             if(tp.Floor<1||tp.Floor>n||v[tp.Floor]) continue;
    40             tp.step=tem.step+1;
    41             q.push(tp);
    42             v[tp.Floor]=1;
    43         }
    44     }
    45     return -1;
    46 }
    47 int main()
    48 {
    49     while(~scanf("%d",&n) && n)
    50     {
    51         scanf("%d %d",&s,&e);
    52         memset(a,0,sizeof(a));
    53         memset(v,0,sizeof(v));
    54         for(int i=0; i<n; i++) scanf("%d",&a[i]);
    55         printf("%d
    ",bfs());
    56     }
    57     return 0;
    58 }
    View Code
  • 相关阅读:
    JS实现日期选择
    php获取ip地址
    CentOS下将php和mysql命令加入到环境变量中简单
    java第二次作业
    新学期新计划
    java第三次作业
    java第四次作业
    申请到博客的第一时间
    Java基础之JDK
    Java中的数据类型
  • 原文地址:https://www.cnblogs.com/csushl/p/9520432.html
Copyright © 2011-2022 走看看