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 ...
随机推荐
- AbsListView.OnScrollListener 使用注意事项
这个类没什么特别的,但是使用的时候我确出错了 abstract void onScroll(AbsListView view, int firstVisibleItem, int visibleIte ...
- bootstrap table 服务器端分页例子
1,前台引入所需的js 可以从官网上下载 function getTab(){ var url = contextPath+'/fundRetreatVoucher/fundBatchRetreatV ...
- powerdesigner 15 如何导出sql schema
PowerDesigner导出所有SQL脚本 操作:Database=>Generate Database PowerDesigner怎么导出建表sql脚本 1 按照数据库类型,切换数据库. D ...
- ubuntu10.04共享文件夹
ubuntu10.04共享文件夹 参考http://jingyan.baidu.com/album/9989c746084c70f648ecfe99.html,共享了home文件夹,然后把共享文件夹映 ...
- java 的UUID的具体用法
参照JDK public final class UUIDextends Objectimplements Serializable, Comparable<UUID> 表示通用唯一标识符 ...
- 如何将SQLite数据库(dictionary.db文件)与apk文件一起发布
可以将dictionary.db文件复制到Eclipse Android工程中的res\raw目录中,如图1所示.所有在res\raw目录中的文件不会被压缩,这样可以直接提取该目录中的文件.使 用 ...
- POJ 1201 Intervals (差分约束系统)
题意 在区间[0,50000]上有一些整点,并且满足n个约束条件:在区间[ui, vi]上至少有ci个整点,问区间[0, 50000]上至少要有几个整点. 思路 差分约束求最小值.把不等式都转换为&g ...
- 【自动化测试】Selenium 2.0 学习笔记
定位下拉框元素,要记得先把鼠标挪过去再进行定位. 定位元素用的是find_element_by.... python的模块化还要去思考下 unittest框架使用的时候,py文件命名不要用unitte ...
- Heritrix源码分析(七) Heritrix总体介绍(转)
本博客属原创文章,欢迎转载!转载请务必注明出处:http://guoyunsky.iteye.com/blog/642794 本博客已迁移到本人独立博客: http://www.yun ...
- .CO域名快被这帮搞IT的玩坏了……
鉴于近来国内访问Google的服务受阻,greatfire.org于前天推出了其基于亚马逊AWS的Google搜索镜像网站,地址是sinaapp.co.该网站随后因多家海外媒体的报道和众多微博大V的转 ...