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

    A strange lift

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


    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

    题解:从当前位置开始每次都向上下两个方向扩展,知道走到目标位置为止。DFS也可得出答案,但是会超时,可能是水平不够吧,这里就不贴出来了。

    代码:

    #include <iostream>
    #include <cstdio>
    #include <cstring>
    #include <cmath>
    #include <algorithm>
    #include <vector>
    #include <set>
    #include <map>
    #include <queue>
    #define INF 99999999
    using namespace std;
    const int maxn=205;
    int a[maxn];
    int vis[maxn];
    int minx=INF;
    int dir[2][1]={-1,1};//上下两个方向
    int n,sx,ex;
    typedef struct Node{
        int x;
        int step=0;
    }node;
    void bfs(int x){
        node p,t;
        queue<node> q;
        p.x=x;
        p.step=0;
        q.push(p);
        while(!q.empty()){
            p=q.front();
            q.pop();
            if(p.x==ex){
                if(p.step<minx)//走到目标位置之后还要判断是否当前路径是最短距离
                    minx=p.step;
            }
            for(int i=0;i<2;i++){//向上下两个方向扩展
                t.x=p.x+a[p.x]*dir[i][0];
                t.step=p.step+1;
                if(!vis[t.x]&&t.x>0&&t.x<=n){//判断是否已经到过或者是否越界
                    vis[t.x]=1;
                    q.push(t);
                }
            }
        }
        return ;
    }
    int main()
    {
    
        while(scanf("%d",&n)&&n){
            minx=INF;
            scanf("%d %d",&sx,&ex);
            for(int i=1;i<=n;i++){
                scanf("%d",&a[i]);
            }
            memset(vis,0,sizeof vis);
            vis[sx]=1;
            bfs(sx);
            if(minx==INF) printf("-1
    ");
            else printf("%d
    ",minx);
        }
        return 0;
    }

  • 相关阅读:
    个人管理:提高你的搜商
    敏捷个人:提供更多文档下载,并转载一篇敏捷个人读书笔记
    个人管理: 激励你的一句话
    敏捷个人 从Scrum实践来思考如何导入价值观
    信息系统开发平台OpenExpressApp 如何解决ComboBox.TextProperty绑定带来问题的来龙去脉
    敏捷个人 敏捷个人价值观,欢迎提出你的意见和你的价值观
    使用VS2010的CodedUI来做自己的自动化测试框架
    .Net4下的MEF(Managed Extensibility Framework) 架构简介
    IronRuby - 快速在半小时学习Ruby基础知识
    敏捷个人 项目网站文档页签增加blog链接
  • 原文地址:https://www.cnblogs.com/kzbin/p/9205230.html
Copyright © 2011-2022 走看看