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 and C++ : Partial initialization of automatic structure

    Refer to: http://stackoverflow.com/questions/10828294/c-and-c-partial-initialization-of-automatic-st ...

  2. hdu - 1180 诡异的楼梯 (bfs+优先队列)

    http://acm.hdu.edu.cn/showproblem.php?pid=1180 注意点就是楼梯是在harry移动完之后才会改变方向,那么只要统计到达这个点时间奇偶性,就可以知道当前楼梯是 ...

  3. oracle教程:PLSQL常用方法汇总

    oracle教程:PLSQL常用方法汇总 在SQLPLUS下,实现中-英字符集转换alter session set nls_language='AMERICAN';alter session set ...

  4. hdu1054(二分图匹配)

    题意很简单,在一颗树上找最小点覆盖. 将树染成黑白两色,构成一张二分图,然后最大匹配==最小点覆盖即可,所以一次匈牙利就可以求出来了 hdu1054 #include <iostream> ...

  5. 维护没有源代码,float改成double

    float f= 931340.31f; Console.WriteLine(f.ToString("#,###,##0.00")); 返回 931,340.30 ,float 1 ...

  6. UVa 340 Master-Mind Hints

    蛋疼的题目描述,看了好长好长时间才看懂,题目本身是很简单的. Designer给出一串长度为N的Code,Breaker用Guess来破译. 对于两串数字,如果有同一列相等的数字,那么叫做strong ...

  7. 51nod1379 索函数

    果断打表找规律.然后看得出来是2^k-1之后又不知道怎么求出k有什么卵用... http://blog.csdn.net/guhaiteng/article/details/52094210 %%%% ...

  8. spring tx:advice 和 aop:config 配置事务

    <?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.sp ...

  9. Http进行网络通信

    http使用get的方式进行网络通信: package com.testGet; import java.io.BufferedReader; import java.io.IOException; ...

  10. I.MX6 Linux、Jni ioctl 差异

    /*********************************************************************** * I.MX6 Linux.Jni ioctl 差异 ...