Mondriaan's Dream

Time Limit: 20000/10000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)

Total Submission(s): 783    Accepted Submission(s): 506

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. 1 2
  2. 1 3
  3. 1 4
  4. 2 2
  5. 2 3
  6. 2 4
  7. 2 11
  8. 4 11
  9. 0 0
 
Sample Output
  1. 1
  2. 0
  3. 1
  4. 2
  5. 3
  6. 5
  7. 144
  8. 51205
 
Source

解题思路:

如图:问铺满大举行一共同拥有多少种方法。

由于长宽最大11,能够状态压缩.

从第一行開始铺砖。

dp[ i ]  [ j ] 定义为 第i行的状态为 j 一共同拥有多少种方法 .

把小矩形用01状态表示,小矩形由两个正方形组成。 对于横着放的小矩形,左右两个正方形用11表示,对于竖着的小矩形,上下两个正方形用分别01表示。

第i行的状态s2与第i-1行的状态s1有关。

s1和s2满足两个条件:

1.   s1  |  s2  得到的数二进制每一位都是1 ,由于对于竖着放的 ,0|1肯定是1,横着放的都是11,相或也是11.

2.   s1  & s2  得到的数连续的1是偶数个,注意0也是偶数。这个看图观察就能够了。

本题犯的错误:

1.

获取一个数x二进制的第i位是0或者1。用 if( x&(1<<i) ==1) 是不正确的, 这句话的意思是,把x的二进制数除了第i位都设为0,第i位通过 &1,来推断是0或者1,可是得到的数不一定是1,是2的倍数,比方 0010  或者 0100.

2.

推断一个数x二进制的每一位是否等于1 ,如果有m位 , 直接用 if( x==1<<m)-1),不用每一位的推断。前者效率更高。

代码:

  1. #include <iostream>
  2. #include <stdlib.h>
  3. #include <stdio.h>
  4. #include <vector>
  5. #include <algorithm>
  6. #include <string.h>
  7. using namespace std;
  8. #define ll long long
  9. ll dp[12][1<<12];//dp[i][j]表示第i行状态为j有多少种方法
  10. int n,m;
  11.  
  12. bool ok(int s1,int s2)
  13. {
  14. int temp=s1|s2;//两个状态或运算每一位都必须是1
  15. if(temp!=(1<<m)-1)
  16. return false;
  17. int cnt=0;
  18. temp=s1&s2;//两个状态且运算,必须连续的1都是偶数个
  19. for(int i=0;i<m;i++)
  20. {
  21. if((temp&(1<<i)))//第i位是1
  22. cnt++;
  23. else
  24. {
  25. if(cnt&1)
  26. return false;
  27. }
  28. }
  29. if(cnt&1)
  30. return false;
  31. return true;
  32. }
  33.  
  34. void solve()
  35. {
  36. memset(dp,0,sizeof(dp));
  37. int maxd=1<<m;
  38. for(int i=0;i<maxd;i++)//铺第一行
  39. if(ok(maxd-1,i))
  40. dp[1][i]++;
  41. for(int i=2;i<=n;i++)//铺第i行
  42. {
  43. for(int j=0;j<maxd;j++)
  44. {
  45. for(int k=0;k<maxd;k++)
  46. if(ok(j,k))
  47. dp[i][j]+=dp[i-1][k];
  48. }
  49. }
  50. ll ans=0;
  51. ans+=dp[n][maxd-1];//最后一行肯定都是1
  52. printf("%I64d\n",ans);
  53. }
  54.  
  55. int main()
  56. {
  57. while(scanf("%d%d",&n,&m)!=EOF)
  58. {
  59. if(!n||!m)
  60. break;
  61. if(n*m&1)//小方块的个数为奇数个,肯定不能铺满
  62. {
  63. printf("0\n");
  64. continue;
  65. }
  66. if(n<m)
  67. n=n^m,m=n^m,n=n^m;
  68. solve();
  69. }
  70. return 0;
  71. }

[ACM] HDU 1400 Mondriaan&#39;s Dream (状态压缩,长2宽1长方形铺满)的更多相关文章

  1. [ACM] HDU 5025 Saving Tang Monk (状态压缩,BFS)

    Saving Tang Monk Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others) ...

  2. hdu 4057 AC自己主动机+状态压缩dp

    http://acm.hdu.edu.cn/showproblem.php?pid=4057 Problem Description Dr. X is a biologist, who likes r ...

  3. hdu 1400 Mondriaan's Dream 解题报告

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1400 题目意思:给出一个h * w的 大 矩形,需要用 1 * 2 的砖块去填充这个大矩形,问填充的方 ...

  4. HDU - 1400 Mondriaan's Dream

    HDU - 1400 思路: 轮廓线dp入门题 #include<bits/stdc++.h> using namespace std; #define fi first #define ...

  5. POJ 2411 Mondriaan&#39;s Dream

    状压DP Mondriaan's Dream Time Limit: 3000MS Memory Limit: 65536K Total Submissions: 9938 Accepted: 575 ...

  6. poj 2411 Mondriaan&#39;s Dream 【dp】

    题目:id=2411" target="_blank">poj 2411 Mondriaan's Dream 题意:给出一个n*m的矩阵,让你用1*2的矩阵铺满,然 ...

  7. POJ2411 - Mondriaan's Dream(状态压缩DP)

    题目大意 给定一个N*M大小的地板,要求你用1*2大小的砖块把地板铺满,问你有多少种方案? 题解 刚开始时看的是挑战程序设计竞赛上的关于铺砖块问题的讲解,研究一两天楞是没明白它代码是怎么写的,智商捉急 ...

  8. POJ 2411 Mondriaan&#39;s Dream (dp + 减少国家)

    链接:http://poj.org/problem?id=2411 题意:题目描写叙述:用1*2 的矩形通过组合拼成大矩形.求拼成指定的大矩形有几种拼法. 參考博客:http://blog.csdn. ...

  9. zoj 1100 - Mondriaan&#39;s Dream

    题目:在m*n的地板上铺上同样的1*2的地板砖,问有多少种铺法. 分析:dp,组合,计数.经典dp问题,状态压缩. 状态:设f(i,j)为前i-1行铺满,第i行铺的状态的位表示为j时的铺砖种类数: 转 ...

随机推荐

  1. Java 中关于default 访问权限的讨论

    Java中关于成员变量访问权限问题一般书中会给出如下表格: 简单地描述一下表中的内容:用private 修饰的成员变量只能在类内部访问:用default修饰的成员变量可以再内部访问,也可以被同个包(同 ...

  2. Tomcat样例安全漏洞

    [漏洞描述] Tomcat在使用的时候一般都直接官网下载源代码包直接使用. 默认情况下,Tomcat源码包里面包含了   examples   这个目录,这个目录主要实现一些样例页面的访问. 比如: ...

  3. js本地对象——Date()

    Date()是JavaScript的本地对象,用于获取当前的时间,包括年.月.日.时.分.秒,可以精确到毫秒级:该对象返回的是UTC 协调世界时(Coordinated Universal Time) ...

  4. jQuery调用WebService ( 同源调用)

    转自原文 jQuery调用WebService 1.编写4种WebService方法     [WebService(Namespace = "http://tempuri.org/&quo ...

  5. hdu Swipe Bo(bfs+状态压缩)错了多次的题

    Swipe Bo Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total S ...

  6. 第二次phython作业

    第一题:编写程序,生成一个包含50个随机整数的列表,然后删除其中所有奇数.(注意保证删除操作的效率) import random x=[random.randint(0,100)for i in ra ...

  7. ubuntu中taglist和ctags安装,简单明了

    1.使用命令安装ctags: sudo apt-get install ctags 2.安装taglist 下载: http://vim.sourceforge.net/scripts/downloa ...

  8. Linux内核编译測试

    内核编译: Step 1:配置内核编译选项. make menuconfig Optional Step :排除编译结果文件(.o)等之间的依赖性. make mrproper Optional St ...

  9. iOS开发—在@interface,@implementation和@property中变量的定义

    一直搞不懂在OC中变量在@interface和@implementation中有什么区别,定义@property又有什么不同,查了很多资料,总结如下: //ViewController.h @inte ...

  10. SSH Key的生成和使用(for git)

    SSH Key的生成和使用 一.总结 1.用git base生成ssh,会生成id_rsa.pub文件,还有一个私钥文件.     $ ssh-keygen -t rsa -C “youremailn ...