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. web app开发中 iPhone、iPad默认按钮样式问题

    webapp开发过程中,用html5+css3很方便,而且可以很方便的编译到Android ios等不同平台,但是ios需要单独处理一下,不然会出现一些想象不到的问题.下面就介绍一下各种问题的解决方法 ...

  2. HW--漂亮度2(测试通过)

    总结:几个函数的使用 (1)  int num=Integer.parseInt(str[0]); //将第一个字符串转成整形数,表示名字个数 (2) String string1=str[i].to ...

  3. JEditorPane中html文档中文乱码解决

    Culturally dependent information in some documents is handled through a mechanism called character e ...

  4. 鼠标点击input时,placeholder中的提示信息消失

    html代码: <input type="text" placeholder="多个关键词空格隔开"> 鼠标点击input时,placeholder ...

  5. 09_httpclient测试SOAP协议

    [工程截图]注意:无需使用Wsimport生成客户端代码 [HttpClient.java] import java.io.ByteArrayOutputStream; import java.io. ...

  6. 修改虚机IP

    同网段的话,直接修改,不同网段的话,使用以下方法: 步骤一:nova list --all-tenant 找到相应虚拟机+--------------------------------------+ ...

  7. 7zip self-extracted executable: To extract file to specific directory

    1) Install 7-zip (7zS.sfx will be installed to C:\Program Files\7-Zip): http://7zsfx.solta.ru/en/ 2) ...

  8. 网址、URL

    手册网:http://www.shouce.ren/ in_array().array_search().array_key_exists() http://www.shangxueba.com/ji ...

  9. htmlt中的块状元素与内联元素

    块元素(block element) address - 地址 blockquote - 块引用 center - 举中对齐块 dir - 目录列表 div - 常用块级容易,也是CSS layout ...

  10. 51nod动态规划-----矩阵取数

    一个N*N矩阵中有不同的正整数,经过这个格子,就能获得相应价值的奖励,从左上走到右下,只能向下向右走,求能够获得的最大价值. 例如:3 * 3的方格. 1 3 3 2 1 3 2 2 1 能够获得的最 ...