zoukankan      html  css  js  c++  java
  • hdu1548

    A strange lift

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


    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
     
     

    # include <iostream>
    # include <queue>
    # include <stdio.h>
    # include <string.h>
    # include <stdlib.h>
    using namespace std;
    int n,a,b;
    int hash[210];
    int s[210];
    int BFS( )
    {
        int now,next,flag = 0;
        queue<int>q;
        q.push(a);
        memset(hash,0,sizeof(hash));
        hash[a] = 0;
        while(!q.empty())
        {
            now=q.front();
            q.pop();
            if(now==b)
            {
                flag=1;
                break;
            }
            next=now-s[now];
            if(next>0&&!hash[next])
            {
                q.push(next);
                hash[next]=hash[now]+1;
            }
            next=now+s[now];
            if(next<=n&&!hash[next])
            {
                q.push(next);
                hash[next]=hash[now]+1;
            }
        }
        if(flag)
            return(hash[b]);
        else
            return -1;
    }

    int main()
    {
        while(scanf("%d",&n)&&n)
        {
            int flag,i;
            scanf("%d%d",&a,&b);
            for(i=1; i<=n;i++)
                scanf("%d",&s[i]);
            flag=BFS();
            printf("%d ",flag);
        }
        return 0;
    }

    最短路:

    #include<stdio.h>
    #include<string.h>
    #define M 1000000
    int n,a,b,dis[201],map[202][201],vit[210];
    void dijkstra()
    {
    int i,j,k,min;
    for(i=1; i<=n; i++)
    dis[i]=map[a][i];
    memset(vit,0,sizeof(vit));
    dis[a]=0;
    for(i=1; i<n; i++)
    {
    min=M;
    for(j=1; j<=n; j++)
    {
    if(!vit[j] && dis[j]<min)
    {
    min=dis[j];
    k=j;
    }
    }
    if(min==M)
    break;
    vit[k]=1;
    for(j=1; j<=n; j++)
    if(!vit[j] && map[k][j]+dis[k]<dis[j])
    dis[j]=dis[k]+map[k][j];
    }
    }
    int main()
    {
    int i,j,f[201];
    while(scanf("%d",&n),n)
    {
    scanf("%d%d",&a,&b);
    for(i=1; i<=n; i++)
    {
    for(j=1; j<=n; j++)
    map[i][j]=M;
    }
    for(i=1; i<=n; i++)
    {
    scanf("%d",&f[i]);
    if(i+f[i]<=n)
    map[i][i+f[i]]=1;
    if(i-f[i]>=1)
    map[i][i-f[i]]=1;
    }
    dijkstra();
    if(dis[b]<M)
    printf("%d ",dis[b]);
    else
    puts("-1");
    }
    return 0;
    }

  • 相关阅读:
    GC算法 垃圾收集器
    Distinct
    生产者消费者实现
    单例模式(七种实现方法)
    【JUC】JDK1.8源码分析之AbstractQueuedSynchronizer
    【JUC】JDK1.8源码分析之ConcurrentHashMap
    【集合框架】JDK1.8源码分析之HashMap
    一个整数,它加上100后是一个完全平方数, 再加上168又是一个完全平方数,请问该数是多少?
    猴子吃桃问题:猴子第一天摘下若干个桃子, 当即吃了一半,还不过瘾,又多吃了一个; 第二天早上又将剩下的桃子吃掉一半,又多吃了一个。 * 以后每天早上都吃了前一天剩下的一半零一个。 到第10天早上想再吃时,见只剩下一个桃子了。 求第一天共摘了多少。 * 1.程序分析:采取逆向思维的方法,从后往前推断。
    输出9*9口诀 输出9*9乘法表
  • 原文地址:https://www.cnblogs.com/lxm940130740/p/3285078.html
Copyright © 2011-2022 走看看