1.题目描述

https://www.hackerrank.com/challenges/lego-blocks

2.解法分析

这题乍看一下觉得应该可以用动态规划来做,但是却死活想不到最优子结构,在网上搜了一下,找到一个英文的描述,整理如下。

首先需要明白一点,稍微复杂一点的动态规划不一定能直接找到最优子结构,可能内嵌一些其他的最优子结构,比如说这题,我们需要得到如下的知识:

  • layerCom[w] : 表示高度为 1 ,宽度为 w 的墙有多少种,先暂且忽略solid structure这个约束
    • layerCom[1] = 1  layerCom[2] = 2  layerCom[3] = 4 layerCom[4] = 8 这个是可以直接枚举获得的
    • 当w > 4 时,一直layerCom[w] = layerCom[w-1] +layerCom[w-2] + layerCom[w-3]+ layerCom[w-4] ,其中,layerCom[w-i]表示最左一块砖是宽度为 i 的情况。
  • wholeCom[w][h] : 表示宽度为 w,高度为h的墙总共有多少种,也是先忽略solid structrue这个约束

    • 很显然,wholeCom[w][h] = power(layerCom,h)
  • retCom[ w][h] : 表示施加了solid structure这个约束时,宽度为w,高度为h的墙的种类,递推公式如下,其中

    表示以从右至左第i块条垂面作为切割面,左边是solid structure,右边是随意形状的种类

3.代码

不考虑modula的条件的代码如下:

#include <stdio.h>

#include <string.h>

#include <cmath>

#include <stdlib.h>

#include <iostream>

#include <vector>

using namespace std;

int getCom(int w,int h);

int main() {

 

    /* Enter your code here. Read input from STDIN. Print output to STDOUT */   

    int T;

    cin>>T;

    int i = 0;

    while(i<T)

    {

        int N,M;

        cin>>N>>M;

        cout<<getCom(M,N)<<endl;

        i++;

    }

    return 0;

}

int getCom(int w,int h)

{

    vector<int> layerCom((w<4 ? 4:w),0);

    layerCom[0] = 1;

    layerCom[1] = 2;

    layerCom[2] = 4;

    layerCom[3] = 8;

    

    if(w > 4)

    {

        for(int i = 4;i<w;++i) layerCom[w-1]=layerCom[w-2] + layerCom[w-3] + layerCom[w-4] + layerCom[w-5];

    }

 

    vector<int>wholeCom(w,0);

    for(int i=0;i<w;++i)wholeCom[i]=(int)pow((double)layerCom[i],h);

 

    vector<int> retCom(w,0);

    retCom[0] = 1;

 

    for(int i = 1;i<w;++i)

    {

        retCom[i] = wholeCom[i];

        for(int j = 1;j<=i;++j)

        {

            retCom[i] -= retCom[i-j]*wholeCom[j-1]; 

        }

    }

 

    return retCom[w-1];

}

lego blocks的更多相关文章

  1. hackerrank【Lego Blocks】:计数类dp

    题目大意: 修一个层数为n,长度为m的墙,每一层可以由长度为1.2.3.4的砖块构成. 每一层都在同一个长度处出现缝隙是方案非法的,问合法的方案数有多少种 思路: 先求出总方案,再减去所有非法的方案数 ...

  2. iOS Architecture Patterns

    By Bohdan Orlov on 21 Mar 2016 - 0 Comments iOS FYI: Slides from my presentation at NSLondon are ava ...

  3. (转)A Recipe for Training Neural Networks

    A Recipe for Training Neural Networks Andrej Karpathy blog  2019-04-27 09:37:05 This blog is copied ...

  4. (转) Learning Deep Learning with Keras

    Learning Deep Learning with Keras Piotr Migdał - blog Projects Articles Publications Resume About Ph ...

  5. The Unix Tools Are Your Friends

    The Unix Tools Are Your Friends Diomidis Spinellis IF, ON MY WAY TO EXILE ON A DESERT ISLAND, I had ...

  6. 从Script到Code Blocks、Code Behind到MVC、MVP、MVVM

    刚过去的周五(3-14)例行地主持了技术会议,主题正好是<UI层的设计模式——从Script.Code Behind到MVC.MVP.MVVM>,是前一天晚上才定的,中午花了半小时准备了下 ...

  7. 【POJ-1390】Blocks 区间DP

    Blocks Time Limit: 5000MS   Memory Limit: 65536K Total Submissions: 5252   Accepted: 2165 Descriptio ...

  8. 开发该选择Blocks还是Delegates

    前文:网络上找了很多关于delegation和block的使用场景,发现没有很满意的解释,后来无意中在stablekernel找到了这篇文章,文中作者不仅仅是给出了解决方案,更值得我们深思的是作者独特 ...

  9. poj 1390 Blocks

    poj 1390 Blocks 题意 一排带有颜色的砖块,每一个可以消除相同颜色的砖块,,每一次可以到块数k的平方分数.问怎么消能使分数最大.. 题解 此题在徐源盛<对一类动态规划问题的研究&g ...

随机推荐

  1. C# 将字符串转化成流,将流转换成字符串

    using System; using System.IO; using System.Text; namespace CSharpConvertString2Stream { class Progr ...

  2. [原]素数筛法【Sieve Of Eratosthenes + Sieve Of Euler】

    拖了有段时间,今天来总结下两个常用的素数筛法: 1.sieve of Eratosthenes[埃氏筛法] 这是最简单朴素的素数筛法了,根据wikipedia,时间复杂度为 ,空间复杂度为O(n). ...

  3. ccnu-线段树-简单的区间更新(三题)

    题目一:http://poj.org/problem?id=3468 Description You have N integers, A1, A2, ... , AN. You need to de ...

  4. python3字符串格式化

    print('Hello World')print('%s',55)print('%6.2f' % 1.235)print('%06.2f' % 1.235)print('-%06.2f' % 1.2 ...

  5. python下载图片

    import re import  urllib.request   def getHtml(url): page = urllib.request.urlopen(url) html = page. ...

  6. 红黑树、B(+)树、跳表、AVL等数据结构,应用场景及分析,以及一些英文缩写

    在网上学习了一些材料. 这一篇:https://www.zhihu.com/question/30527705 AVL树:最早的平衡二叉树之一.应用相对其他数据结构比较少.windows对进程地址空间 ...

  7. django - 修改 自增长id,起始值

    常常你会遇到这样的情况,需要自增长的起始值是 0,再次从 0开始. 两个选择: 1. drop table_name; django重新建表. 2. ALTER TABLE table_name AU ...

  8. 免费的WebService

    天气预报Web服务,数据来源于中国气象局 Endpoint :     http://www.webxml.com.cn/WebServices/WeatherWebService.asmx Disc ...

  9. Hdu 1521 排列组合

    a1 n1 a2 n2 ... ak nkn=n1+n2+...+nk从n个数中选r个排列(不是组合噢)// 指数型母函数// 模板#include <iostream> #include ...

  10. python基础之使用os.system来执行系统命令

    今天我们来尝试使用python 的os.system来执行系统命令 可以使用如下方法: import osprint os.system('ping www.baidu.com') 输出的结果是:64 ...