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;
    }

  • 相关阅读:
    Java内存模型(JMM)是什么?JMM 通过控制主内存与每个线程的本地内存之间的交互,来提供内存可见性保证
    【普及组_在线赛】班级聚会(reuntion)
    面试官:你对Redis缓存了解吗?面对这11道面试题是否有很多问号?
    【华为云技术分享】浅谈产品模型(Profile)在程序设计中的作用
    【华为云技术分享】LiteAI四大绝招,解锁物联网智能设备AI开发难关
    【华为云技术分享】漫谈Huawei LiteOS五大内核模块
    科技感满满,华为云DevCloud推出网页暗黑模式
    赶在520之前,程序员如何用Python送上最特别的“我爱你”表白
    【华为云技术分享】从部署和运维说说DLI(1)
    【2017.11.25普及组模拟】The Farthest House题解
  • 原文地址:https://www.cnblogs.com/lxm940130740/p/3285078.html
Copyright © 2011-2022 走看看