lego blocks
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的更多相关文章
- hackerrank【Lego Blocks】:计数类dp
题目大意: 修一个层数为n,长度为m的墙,每一层可以由长度为1.2.3.4的砖块构成. 每一层都在同一个长度处出现缝隙是方案非法的,问合法的方案数有多少种 思路: 先求出总方案,再减去所有非法的方案数 ...
- iOS Architecture Patterns
By Bohdan Orlov on 21 Mar 2016 - 0 Comments iOS FYI: Slides from my presentation at NSLondon are ava ...
- (转)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 ...
- (转) Learning Deep Learning with Keras
Learning Deep Learning with Keras Piotr Migdał - blog Projects Articles Publications Resume About Ph ...
- 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 ...
- 从Script到Code Blocks、Code Behind到MVC、MVP、MVVM
刚过去的周五(3-14)例行地主持了技术会议,主题正好是<UI层的设计模式——从Script.Code Behind到MVC.MVP.MVVM>,是前一天晚上才定的,中午花了半小时准备了下 ...
- 【POJ-1390】Blocks 区间DP
Blocks Time Limit: 5000MS Memory Limit: 65536K Total Submissions: 5252 Accepted: 2165 Descriptio ...
- 开发该选择Blocks还是Delegates
前文:网络上找了很多关于delegation和block的使用场景,发现没有很满意的解释,后来无意中在stablekernel找到了这篇文章,文中作者不仅仅是给出了解决方案,更值得我们深思的是作者独特 ...
- poj 1390 Blocks
poj 1390 Blocks 题意 一排带有颜色的砖块,每一个可以消除相同颜色的砖块,,每一次可以到块数k的平方分数.问怎么消能使分数最大.. 题解 此题在徐源盛<对一类动态规划问题的研究&g ...
随机推荐
- dojo 二 AMD模块
可参考官方教程:http://dojotoolkit.org/documentation/tutorials/1.7/hello_dojo/教程里主要定义了两个方法,setText设置文本内容和res ...
- U3D NGUI改变GameObject Activity闪烁的问题
不是关闭再激活GameObject会闪烁,而是再激活时,NGUI渲染步骤不一致导致的闪烁. 并且文字激活后渲染要慢一帧,如果延迟一帧处理,又会导致精灵图片快一帧,图片重叠.这个测试结果不一定准确,先记 ...
- hdu - 1180 诡异的楼梯 (bfs+优先队列)
http://acm.hdu.edu.cn/showproblem.php?pid=1180 注意点就是楼梯是在harry移动完之后才会改变方向,那么只要统计到达这个点时间奇偶性,就可以知道当前楼梯是 ...
- JSON 之 SuperObject(4): 增、删、改
unit Unit1; interface uses Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, For ...
- 推荐一款开源的原型设计软件--pencil
如果觉得内置的元素不够,可以直接用类似屏幕截图软件直接剪切粘贴,并且可以制作自己的元素集合.很好用 http://pencil.evolus.vn/ Easy GUI Prototyping Penc ...
- UVa 1395 Slim Span【最小生成树】
题意:给出n个节点的图,求最大边减最小边尽量小的值的生成树 首先将边排序,然后枚举边的区间,判定在该区间内是否n个点连通,如果已经连通了,则构成一颗生成树, 则此时的苗条度是这个区间内最小的(和kru ...
- 使用C++读写Excel
1.导入Excel类型库 使用Visual C++的扩展指令#import导入Excel类型库: 1 2 3 4 5 6 7 8 9 10 11 12 #import "C:\\Progra ...
- ubuntu下实现openerp 7使用nginx反正代理及绑定域名
这里要记录一个nginx upstream实现反向代理的配置过程. 连接vps的ssh. 先安装nginx sudo apt-get install nginx 修改/etc/nginx/nginx. ...
- Swift 2.0 : 'enumerate' is unavailable: call the 'enumerate()' method on the sequence
Swift 2.0 : 'enumerate' is unavailable: call the 'enumerate()' method on the sequence 如下代码: for (ind ...
- JDK,JRE,JVM区别与联系-理解与概括
我们利用JDK(调用JAVA API)开发了属于我们自己的JAVA程序后,通过JDK中的编译程序(javac)将我们的文本java文件编译成JAVA字节码,在JRE上运行这些JAVA字节码,JVM解析 ...