zoukankan      html  css  js  c++  java
  • P1135 奇怪的电梯 dp

      

    题目描述

    呵呵,有一天我做了一个梦,梦见了一种很奇怪的电梯。大楼的每一层楼都可以停电梯,而且第ii层楼(1 le i le N)(1iN)上有一个数字K_i(0 le K_i le N)Ki(0KiN)。电梯只有四个按钮:开,关,上,下。上下的层数等于当前楼层上的那个数字。当然,如果不能满足要求,相应的按钮就会失灵。例如:3, 3 ,1 ,2 ,53,3,1,2,5代表了K_i(K_1=3,K_2=3,…)Ki(K1=3,K2=3,),从11楼开始。在11楼,按“上”可以到44楼,按“下”是不起作用的,因为没有-22楼。那么,从AA楼到BB楼至少要按几次按钮呢?

    输入输出格式

    输入格式:

    共二行。

    第一行为33个用空格隔开的正整数,表示N,A,B(1≤N≤200, 1≤A,B≤N)N,A,B(1N200,1A,BN)。

    第二行为NN个用空格隔开的非负整数,表示K_iKi

    输出格式:

    一行,即最少按键次数,若无法到达,则输出-11。

    输入输出样例

    输入样例#1: 复制
    5 1 5
    3 3 1 2 5
    
    输出样例#1: 复制
    3


    这题做法多样
    很明显最短路 深搜广搜都可以做

    dp方法类似之前的多米诺骨牌 不过效率并不高 n2
    #include<bits/stdc++.h>
    using namespace std;
    //input by bxd
    #define rep(i,a,b) for(int i=(a);i<=(b);i++)
    #define repp(i,a,b) for(int i=(a);i>=(b);i--)
    #define RI(n) scanf("%d",&(n))
    #define RII(n,m) scanf("%d%d",&n,&m)
    #define RIII(n,m,k) scanf("%d%d%d",&n,&m,&k)
    #define RS(s) scanf("%s",s);
    #define LL long long
    #define REP(i,N)  for(int i=0;i<(N);i++)
    #define CLR(A,v)  memset(A,v,sizeof A)
    //////////////////////////////////
    #define inf 214748347
    #define N 15005
    int dp[N];
    int a[N];
    int main()
    {
        int n,s,e;
        RIII(n,s,e);
        rep(i,1,n)
        RI(a[i]),dp[i]=inf;
        dp[s]=0;
    
        rep(i,1,n)
        rep(j,1,n)
        {
            if(j-a[j]>=1)dp[j-a[j]]=min(dp[j-a[j]],dp[j]+1);
            if(j+a[j]<=n)dp[j+a[j]]=min(dp[j+a[j]],dp[j]+1);
        }
        if(dp[e]!=inf)
        printf("%d",dp[e]);
        else printf("-1");
    }
    View Code










  • 相关阅读:
    [Jsp] JSP的九个内置对象
    [Linux] 完整删除用户帐号
    Title:it has been a long time.
    Problem and Solution : Unable to resolve target 'android9'
    Click Button to change the color of TextView
    Bundle使用(不同的Activity之间传递数据)
    实例方法和类方法
    Use DIffent Font and Size.
    子类继承的成员方法
    StartActivityForResutl,Bundle(Return data to exActivity)
  • 原文地址:https://www.cnblogs.com/bxd123/p/10691431.html
Copyright © 2011-2022 走看看