A strange lift

2024-02-03 03:58
文章标签 lift strange

本文主要是介绍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

题意:

给出楼层数和起点终点,接下来给出每一层可以改变的层数,当然要在1-n范围内

很显然是一个最短路问题,将楼层当做结点,各个楼层可以到达则把距离设为1

代码:

//一个人的旅行
/*#include<stdio.h>
#include<string.h>
#include<iostream>
#include<queue>
#include<bits/stdc++.h>
using namespace std;
const int inf=0x3f3f3f3f;
const int maxn=1005;//段点数
bool book[maxn];
int dis[maxn],first[maxn],cnt;
struct node
{
    int u;//next
    int v,w;
    node(int uu=0,int vv=0,int ww=0):u(uu),v(vv),w(ww){}
}e[maxn*maxn];
inline void intt()
{
    cnt=0;
    memset(first,-1,sizeof(first));
    memset(book,0,sizeof(book));
    //memset(dis,0x3f,sizeof(book));
}
inline void add(int u,int v,int w)
{
    e[++cnt]=node(first[u],v,w);
    first[u]=cnt;
}
void spfa()
{
    for(int i=0;i<1005;i++)
        dis[i]=inf;
    queue<int>q;
    q.push(0);
    dis[0]=0;
    book[0]=1;
    while(!q.empty())
    {
        //printf("12\n");
        int u=q.front();
        q.pop();
        book[u]=0;
        for(int i=first[u];~i;i=e[i].u)
        {
            int v=e[i].v;
            int w=e[i].w;
            if(dis[v]>dis[u]+w)
            {
                dis[v]=dis[u]+w;
                if(book[v]==0)
                {
                    q.push(v);
                    book[v]=1;
                }
            }

        }

    }
}
int main()
{
    int t,s,d;
    int u,v,w;
    while(scanf("%d %d %d",&t,&s,&d)!=EOF)
    {
        intt();
        for(int i=0;i<t;i++)
        {
            scanf("%d %d %d",&u,&v,&w);
            add(u,v,w);
            add(v,u,w);
        }
        int a;
        for(int i=0;i<s;i++)
        {
            scanf("%d",&a);
            add(a,0,0);
            add(0,a,0);
        }
        spfa();
        //printf("123\n");
        int minn=0x3f3f3f3f;
        for(int i=0;i<d;i++)
        {
            scanf("%d",&a);
            minn=min(minn,dis[a]);
        }
        printf("%d\n",minn);
    }
    return 0;
}*/
//B
/*#include<stdio.h>
#include<string.h>
#include<algorithm>
using namespace std;
const int inf=0x3f3f3f3f;
int dis[205];
int e[205][205];
bool book[205];
int n,m;
void intt()
{
    memset(book,0,sizeof(book));
    for(int i=0;i<n;i++)
        dis[i]=inf;
    for(int i=0;i<n;i++)
        for(int j=0;j<n;j++)
    {
        e[i][j]=inf;
    }
}
int main()
{
    int t1,t2,t3;
    while(scanf("%d %d",&n,&m)!=EOF)
    {
        intt();
        for(int i=0;i<m;i++)
        {
            scanf("%d %d %d",&t1,&t2,&t3);
            e[t1][t2]=min(e[t1][t2],t3);
            e[t2][t1]=e[t1][t2];
        }
        int st,en;
        scanf("%d %d",&st,&en);
        for(int i=0;i<n;i++)
            dis[i]=e[st][i];
        book[st]=1;
        dis[st]=0;
        for(int i=0;i<n;i++)
        {
            int minn=inf;
            int u;
            for(int j=0;j<n;j++)
            {
                if(book[j]==0&&dis[j]<minn)
                {
                    u=j;
                    minn=dis[j];
                }
            }
            book[u]=1;
            for(int v=0;v<n;v++)
            {
                if(e[u][v]<inf)
                {
                    if(dis[v]>dis[u]+e[u][v])
                        dis[v]=dis[u]+e[u][v];
                }
            }
        }
        if(dis[en]!=inf)
            printf("%d\n",dis[en]);
        else
            printf("-1\n");
    }
    return 0;
}
*/
//c
#include<stdio.h>
#include<string.h>
#include<algorithm>
using namespace std;
const int inf=0x3f3f3f3f;
int dis[205];
int e[205][205];
bool book[205];
int n,m;
void intt()
{
    memset(book,0,sizeof(book));
    for(int i=0;i<n;i++)
        dis[i]=inf;
    for(int i=0;i<n;i++)
        for(int j=0;j<n;j++)
    {
        e[i][j]=inf;
    }
}
int main()
{
    int t1,t2,t3;
    int st,en;
    while(scanf("%d",&n)!=EOF)
    {
        if(n==0)
            break;
        scanf("%d %d",&st,&en);
        st--;
        en--;
        intt();
        int a;
        for(int i=0;i<n;i++)
        {
            scanf("%d",&a);
            if(i+a<n)
                e[i][i+a]=1;
            if(i-a>=0)
                e[i][i-a]=1;
        }
        for(int i=0;i<n;i++)
            dis[i]=e[st][i];
        book[st]=1;
        dis[st]=0;
        for(int i=0;i<n;i++)
        {
            int minn=inf;
            int u;
            for(int j=0;j<n;j++)
            {
                if(book[j]==0&&dis[j]<minn)
                {
                    u=j;
                    minn=dis[j];
                }
            }
            book[u]=1;
            for(int v=0;v<n;v++)
            {
                if(e[u][v]<inf)
                {
                    if(dis[v]>dis[u]+e[u][v])
                        dis[v]=dis[u]+e[u][v];
                }
            }
        }
        if(dis[en]!=inf)
            printf("%d\n",dis[en]);
        else
            printf("-1\n");
    }
    return 0;
}
 

这篇关于A strange lift的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/672918

相关文章

hdu a strang lift

按得最短路做的,DP 搜索也能搞 #include <cstdio>#include <cstring>#include <algorithm>#include <vector>#include <queue>using namespace std;#define MAX_N 10000#define INF 0xfffftypedef pair<int, int> P;in

Light OJ 1318 Strange Game 组合数+快速幂+分解因子

长度为l的用k种字符组成的字符串有k^l中 其中m个字符要不相同 那就是k^l*C(l, m)*(k-1)^m 有重复 要除以2 但是你mod n了 不能直接除 n不一定是素数 所以不能乘以逆元 所以我都mod 2倍的n 最后的结果再除以2 特判l = 1 和 m = 0的情况 #include <cstdio>#include <cstring>#include <cmath>us

Codeforces 479E Riding in a Lift(dp)

题目链接:Codeforces 479E Riding in a Lift 题目大意:有一栋高N层的楼,有个无聊的人在A层,他喜欢玩电梯,每次会做电梯到另外一层。但是这栋楼里有个秘 密实验室在B层,所以每次他移动的时候就有了一个限制,x为当前所在层,y为目标层,|x - y| < |x - b|。问说移动K次 后,有多少不同的路径。 解题思路:dp[i][j]表示在第i步到达j层

HDU 2899 Strange fuction(二分||三分)

题目链接~~> 做题感悟:这题和 2199 那题差不多。 解题思路:一、题目让求函数的最小值,首先应该分析函数图象。将函数求导得  f(x) ’ =  42 * x^6 + 48 * x^5 + 21 * x^2 + 10*x - y,因为 y 大于 0 所以假设存在 k 使f(x)'= 0,所以当0<=x<k 时f(x)’小于0,原函数在0<=x<k单调递减。在 k<x<=100 单调递增。

HDU2899 Strange fuction(牛顿迭代法)

最近刚学完数值分析上的方程求根——牛顿法,所以做几题练习一下。 Problem Description Now, here is a fuction:   F(x) = 6 * x^7+8*x^6+7*x^3+5*x^2-y*x (0 <= x <=100) Can you find the minimum value when x is between 0 and 100.

LSS (Lift, Splat, Shoot)代码解析

文章目录 论文研究背景算法实现过程梳理一、相关参数设置二、模型相关参数三、算法前向过程 论文研究背景 LSS是一篇发表在ECCV 2020上有关自动驾驶感知方向的论文,具体子任务为object segmentation and map segmentation。论文和官方repo如下: 论文:https://link.zhihu.com/?target=https%3A//ar

很妙的思维题,CF 1270D. Strange Device

目录 一、题目 1、题目描述 2、输入/交互 2.1输入 2.2交互 3、原题链接 二、解题报告 1、思路分析 2、复杂度 3、代码详解 一、题目 1、题目描述 2、输入/交互 2.1输入 2.2交互 3、原题链接 Problem - 1270D - Codeforces 二、解题报告 1、思路分析 我们只考

HDU 2899 Strange fuction 水三分

<strong><span style="font-family:Comic Sans MS;font-size:24px;">题意不说了,思想就是三分很朴素</span></strong> #include<iostream>#include<stdio.h>#include<string.h>#define eps 1e-9using namespace std;double a;

Codeforces Contest 1191 F Tokitsukaze and Strange Rectangle —— sorting+线段树

This way 题意: 二维平面上有一些点,你现在有一个没有顶边的矩形,问你有多少种包含点的情况(每个点视为不同) 题解: 将每个点视为矩形下底边上的点,查找这个点左边有多少点,右边有多少点,这个点做完之后将其删除,相同高度的点从左到右做,对于右边的点要注意左端点位左边的点+1: 这张图就表示了相同高度右边点的可查询区间。 (刚多校结束发现2200真的是比赛中的简单题了) #incl

Codeforces Round#769(Div.2) C. Strange Test

题意 给定两个数,A-B,一共有三种操作 1.A=A+1; 2.B=B+1; 3.A=A|B; 问使得A=B的最小操作步数。 题解: 由于第一种操作和第二种操作不可能同时使用,所以要么是A++=B,要么是A++然后或为B,不然就是,用第三种操作使得A>=B,然后B++使得A=B(此时B为B变化后的值) #include<bits/stdc++.h>using namespace std;