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

  • 相关阅读:
    git代码冲突
    Centos Git1.7.1升级到Git2.2.1
    linux指定某非root用户执行开机启动项的方法(gogs git)
    kvm增加硬盘挂载
    git分支管理策略
    mac命令行配置网络
    svn稀疏目录--通过设置工作目录的深度(depth)实现目录树的部分签出
    svn update解决冲突
    rocketmq单机搭建
    MongoDB数据库未授权访问漏洞及加固
  • 原文地址:https://www.cnblogs.com/lxm940130740/p/3285078.html
Copyright © 2011-2022 走看看