zoukankan      html  css  js  c++  java
  • A strange lift

    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求最短次数.
    #include <bits/stdc++.h>
    const int N=2e2+5;
    using namespace std;
    int a[N];
    int mp[N];//记录是否已经到过第I层楼
    int n;
    int bfs(int now,int end){
        queue<int> q;
        q.push(now);
        while(!q.empty()){
            now= q.front();q.pop();
            if(now==end) return mp[now];//如果到达目标楼层,则返回次数
            if(now+a[now]<=n&&mp[now+a[now]]==-1) q.push(now+a[now]),mp[now+a[now]]=mp[now]+1;//要去的楼层没有超过限制且之前没去过,则上去
            if(now-a[now]>=1&&mp[now-a[now]]==-1) q.push(now-a[now]),mp[now-a[now]]=mp[now]+1;//同上
        }
        return -1;
    }
    int main()
    {
        int now,b;
        while(cin>>n&&n!=0){
            memset(mp,-1,sizeof(mp));
            cin>>now>>b;
            for(int i=1;i<=n;i++) cin>>a[i];
            mp[now]=0;
            cout<<bfs(now,b)<<endl;
        }
        //cout << "Hello world!" << endl;
        return 0;
    }
    
    更新一波:DFS求最短路
    用DFS来搞,弄了个假算法,wa到自闭.看大佬的blog学的.
    #include <bits/stdc++.h>
    const int N=2e2+5;
    using namespace std;
    int a[N];//存储第I层的最短次数
    int mp[N];
    int n,minn;
    void dfs(int now,int step,int end){
        if(now<1||now>n) return;
        if(now==end){
            minn=min(step,minn);
            return;
        }
        if(step>=mp[now]) return;//如果当前所在的楼层的最短次数比我现在的大,那就返回,否则保存最小值.
        mp[now]=step;
        dfs(now+a[now],step+1,end);
        dfs(now-a[now],step+1,end);
    }
    int main()
    {
        int now,b;
        while(cin>>n&&n!=0){
            minn=0x7fffffff;
            cin>>now>>b;
            for(int i=1;i<=n;i++){cin>>a[i];mp[i]=0x7fffffff;}
            dfs(now,0,b);
            if(minn!=0x7fffffff) cout<<minn<<endl;
            else cout<<"-1"<<endl;
        }
        //cout << "Hello world!" << endl;
        return 0;
    }
    
  • 相关阅读:
    Coursera机器学习week11 单元测试
    关于 TypeReference 的解释
    getModifiers 方法解释。
    instanceof isInstance isAssignableFrom 比较
    elasticsearch 基础 语法总结
    kibana 启动 关闭 和进程查找
    MD5 SHA1 SHA256 SHA512 SHA1WithRSA 的区别
    spring boot 项目 热启动
    java zip 压缩文件
    Packet for query is too large (1660 > 1024). You can change this value on the server by setting the max_allowed_packet' variable.
  • 原文地址:https://www.cnblogs.com/-yjun/p/10674586.html
Copyright © 2011-2022 走看看