zoukankan      html  css  js  c++  java
  • HDU 1548 A strange lift (广搜)

    题目链接

    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
    

    分析:

    开始一些BFS练习了,先来一道简单的,这是一栋大楼,有一部很奇怪的电梯,电梯只有两个按钮,

    UP和DOWN,每个楼层有一个数字num,如果你在第2层,你按UP,电梯会到达 num+2层,按DOWN亦然,

    到达2-num层,求一个人从一个楼层要去另一个楼层,需要按几步?

    (这个电梯够让人捉急的!)

    当然如果碰到0层或者-1层都不能到达,碰到大于刚开始的n层的也不能到达,

    如果永远到不了需要去的那一层,就输出-1了。

    广搜,队列做,标准的BFS题目呀,很容易就AC了。。

    代码:

    #include<stdio.h>
    #include<queue>
    #include<string.h>
    using namespace std;
    int A,B;
    int a[500];
    int bj[500];
    struct zuobiao
    {
        int x,output;
    } now,nex;
    void bfs(int x)
    {
        queue<zuobiao >s;
        memset(bj,0,sizeof(bj));
        bj[x]=1;
        now.x=x;
        now.output=0;
        s.push(now);
        while(!s.empty())
        {
            now=s.front();
            if(now.x==B)
            {
                printf("%d
    ",now.output);
                return ;
            }
            s.pop();
            for(int i=0; i<2; i++)
            {
                if(i==0)nex.x=now.x+a[now.x];
                if(i==1)nex.x=now.x-a[now.x];
                if(nex.x>=1&&nex.x<=200&&bj[nex.x]==0)
                {
                    bj[nex.x]=1;
                    nex.output=now.output+1;
                    s.push(nex);
                }
            }
        }
        printf("-1
    ");
        return ;
    }
    int main()
    {
        int n;
        while(~scanf("%d",&n))
        {
            if(n==0)break;
            scanf("%d%d",&A,&B);
            memset(a,0,sizeof(a));
            for(int i=1; i<=n; i++)
            {
                scanf("%d",&a[i]);
            }
            bfs(A);
        }
    }
  • 相关阅读:
    带你封装自己的『权限管理』框架
    一夜搞懂 | JVM 线程安全与锁优化
    一夜搞懂 | Java 内存模型与线程
    一夜搞懂 | JVM 字节码执行引擎
    一夜搞懂 | JVM 类加载机制
    一夜搞懂 | JVM GC&内存分配
    一文洞悉JVM内存管理机制
    Redis 的基本数据类型 和 基础应用场景
    MyISAM 和 InnoDB 索引结构及其实现原理
    一次性搞懂 PHP 中面向对象的所有知识点。
  • 原文地址:https://www.cnblogs.com/cmmdc/p/6767691.html
Copyright © 2011-2022 走看看