Mondriaan's Dream

Problem Description
Squares and rectangles fascinated the famous Dutch painter Piet Mondriaan. One night, after producing the drawings in his 'toilet series' (where he had to use his toilet paper to draw on, for all of his paper was filled with squares and rectangles), he dreamt of filling a large rectangle with small rectangles of width 2 and height 1 in varying ways.

Expert as he was in this material, he saw at a glance that he'll need a computer to calculate the number of ways to fill the large rectangle whose dimensions were integer values, as well. Help him, so that his dream won't turn into a nightmare!

 
Input
The input file contains several test cases. Each test case is made up of two integer numbers: the height h and the width w of the large rectangle. Input is terminated by h=w=0. Otherwise, 1<=h,w<=11. 
 
Output
For each test case, output the number of different ways the given rectangle can be filled with small rectangles of size 2 times 1. Assume the given large rectangle is oriented, i.e. count symmetrical tilings multiple times.
 
Sample Input
1 2
1 3
1 4
2 2
2 3
2 4
2 11
4 11
0 0
 
Sample Output
1
0
1
2
3
5
144
51205
 
题目大意:用长为2,宽为1的矩形填充满长为h,宽为w的矩形有多少种方案?
 
分析:

由于长和宽均小于等于11,故每一行均可用一个2进制数表示其状态。我们用1表示竖放的方格,0表示横放的方格。那么样例中各行的状态分别为:

00100001100

11110011100

11110011001

00111001001

10011000011

10000001111

00001001100

10011100100

10011100111

00001000011

转化为十进制,就是268,1948,1945,457,1219,1039,76,1252,1255,67。

  用dp(i,j)表示第 i 行状态为 j 时,有多少种排法。那么dp(i,j)应该等于前 i-1 行,状态 k 与 j 相符的排法数的和。状态相符,即在 j 二进制数上为1的位,k 一定也为1。在向下递归时,应该把 k 中和 j 二进制状态中同为1的位置为0,即应该求 dp(i-1,k^j),原因是当第 i-1 行与第 i 行相接后,二者同为1的地方消掉了,应该当作0处理。

  所以为了方便,题目描述中那个最大矩阵的第一行就表示为00100001100,第二行就表示为11010010000,这样的话,当上一层j位置为1时,下一层j位置一定为0

当上一层j位置为0时,下一层j位置可以为1,也可以为0,并且当上下都层为0时,零的个数要为偶数,这样上下层的状态相&为0。

  所以dp(i,j)=sum{dp(i-1,k^j),(k&&j)==0&&judge(k^j)}(judge(k)表示k是有效的行状态,即二进制数状态中连续的0个数为偶数,可先生成存入数组中)。所求答案即为dp(n,0).

  初始化dp(0,i)=0,dp(0,0)=1。

  注意结果要用long long或__int64保存,还有要注意位运算的优先级是很低的。

代码如下:

 # include<stdio.h>
__int64 dp[][<<];
int h,w; bool judge(int s){ //判断状态s是否成立
int count;
count = ; //s中0的个数
for(int i=;i<w; i++){
if(s & ){
if(count & ) //1的前面0的个数非偶数,不成立
return false;
}
else
count ++;
s >>= ;
}
if(count & ) //奇数个0,不成立
return false;
else
return true;
} int main(){
int i,j,k;
while(scanf("%d%d",&h,&w),h+w){
if((h*w) & ){ //这种情况下无论如何也不能填满
printf("0\n");
continue;
}
dp[][] = ;
for(i=; i<=h; i++)
for(j=; j<(<<w); j++){
dp[i][j] = ; //初始化
for(k=; k<(<<w); k++)
if((j&k)== && judge(j^k))
dp[i][j] += dp[i-][k];
}
printf("%I64d\n",dp[h][]);
}
return ;
}

HDU 1400 (POJ 2411 ZOJ 1100)Mondriaan's Dream(DP + 状态压缩)的更多相关文章

  1. POJ2411 Mondriaan's Dream(状态压缩)

    Mondriaan's Dream Time Limit: 3000MS   Memory Limit: 65536K Total Submissions: 15295   Accepted: 882 ...

  2. poj 2411 Mondriaan's Dream(状态压缩dp)

    Description Squares and rectangles fascinated the famous Dutch painter Piet Mondriaan. One night, af ...

  3. POJ 2411 Mondriaan's Dream [经典状态压缩dp]

    题意:略. 思路:这一题开始做的时候完全没有思路,便去看了别人的题解. 首先,对于这个题目解法想有一个初步的了解,请看这里:http://www.2cto.com/kf/201208/146894.h ...

  4. POJ 3311 Hie with the Pie(DP状态压缩+最短路径)

    题目链接:http://poj.org/problem?id=3311 题目大意:一个送披萨的,每次送外卖不超过10个地方,给你这些地方之间的时间,求送完外卖回到店里的总时间最小. Sample In ...

  5. ZOJ 3471 Most Powerful(DP + 状态压缩)

    题目链接:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemId=4257 题目大意:有 n(2<=n<=10) 个原子,每两 ...

  6. HDU 4336 Card Collector (期望DP+状态压缩 或者 状态压缩+容斥)

    题意:有N(1<=N<=20)张卡片,每包中含有这些卡片的概率,每包至多一张卡片,可能没有卡片.求需要买多少包才能拿到所以的N张卡片,求次数的期望. 析:期望DP,是很容易看出来的,然后由 ...

  7. HDU 1074 Doing Homework (dp+状态压缩)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1074 题目大意:学生要完成各科作业, 给出各科老师给出交作业的期限和学生完成该科所需时间, 如果逾期一 ...

  8. hdu 4352 数位dp + 状态压缩

    XHXJ's LIS Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total ...

  9. [ACM] HDU 1400 Mondriaan&#39;s Dream (状态压缩,长2宽1长方形铺满)

    Mondriaan's Dream Time Limit: 20000/10000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Othe ...

随机推荐

  1. C#学习笔记10:Try-catch的用法和引用类型、值类型整理

    Try-Catch: 将可能发生异常的代码放到try中,在catch中进行捕获. 如果try中有一行代码发生了异常,那么这行代码后面的代码不会再被执行了. Try写完了以后,紧接着就要写Catch   ...

  2. redhat6.5 配置使用centos的yum源

    新安装了redhat6.5安装后,登录系统,使用yum update 更新系统.提示: This system is not registered to Red Hat Subscription Ma ...

  3. 常用经典SQL语句大全(基础)

    一.基础 1.说明:创建数据库 CREATE DATABASE database-name 2.说明:删除数据库 drop database dbname 3.说明:备份sqlserver --- 创 ...

  4. Meteor错误:TypeError: Meteor.userId is not a function

    问题描述: 浏览器console提示错误TypeError: Meteor.userId is not a function. 原因分析: 通过查看Meteor API文档,可知该函数由包accoun ...

  5. 使用 x3dom 框架及 WebGL 在浏览器上显示 3 维模型

    如果需要在浏览器上显示 3D 画面的话, 现在一般会使用 ​WebGL, 典型的例如 three.js(​http://mrdoob.github.com/three.js/), 但是 WebGL 对 ...

  6. iOS开发——企业证书(免审核 299$)

    (最近准备考试……空闲截图整理成博客)

  7. forever start Error: Cannot find module './daemon.v0.10.26'

    我的技术博客经常被流氓网站恶意爬取转载.请移步原文:http://www.cnblogs.com/hamhog/p/3590158.html,享受整齐的排版.有效的链接.正确的代码缩进.更好的阅读体验 ...

  8. OpenJudge / Poj 1044 Date bugs C++

    链接地址: Poj:http://poj.org/problem?id=1044 OpenJudge:http://bailian.openjudge.cn/practice/1044/ 题目: 总时 ...

  9. 学习S5

                  rztyfx的专栏       目录视图 摘要视图 订阅 [专家问答]阿里陈康贤:探讨大型网站之架构    走进VR开发世界——我们离开发一款VR大作还有多远?     C ...

  10. 关于静态库和动态库的理解(C++)

    库的存在,是软件模块化的基础. 库存在的意义: } 库是别人写好的现有的,成熟的,可以复用的代码,你可以使用但要记得遵守许可协议.      } 现实中每个程序都要依赖很多基础的底层库,不可能每个人的 ...