POJ2411 Mondriaan's Dream (广场铺砖问题 状压dp)
传送门:http://poj.org/problem?id=2411
Mondriaan's Dream
Time Limit: 3000MS | Memory Limit: 65536K | |
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.
![](http://poj.org/images/2411_1.jpg)
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!
![](http://poj.org/images/2411_1.jpg)
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 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
![](http://poj.org/images/2411_2.jpg)
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
Source
第一次写状压dp。。在网上看了半天没看懂,后来自己模拟了一下就懂了。。
这题把每一行压成一个二进制状态 ,
那么如果对于第i行的第j位,如果是0,则上一位必须是1 //竖放,不为空
如果是1,则上一位可能是0,也可能是1
如果是0,竖放,不为空
如果是1,需要检测是否是横放
对于第一行的状态,只需要check(i,(1<<m)-1)即可,因为第一行只能横放或不放
代码纯个人风格。。如果哪里写的不好,希望有人可以给我指正。。
Codes:
1 #include<set> 2 #include<queue> 3 #include<cstdio> 4 #include<cstring> 5 #include<cstdlib> 6 #include<iostream> 7 #include<algorithm> 8 using namespace std; 9 #define For(i,n) for(int i=1;i<=n;i++) 10 #define Rep(i,l,r) for(int i=l;i<=r;i++) 11 12 long long opt[14][1<<14]; 13 int n,m; 14 15 bool Check(int s1,int s2){ 16 if((s1|s2)!=(1<<m)-1) return false; 17 for(int i=0;i<m;) 18 if((s1 & 1<<i)!=(s2 & 1<<i)) i++; 19 else{ 20 if(i==m-1) return false;else 21 if((s1 & 1<<(i+1))!= (s2 & 1<<(i+1))) return false; 22 i+=2; 23 } 24 return true; 25 } 26 27 int main(){ 28 while(scanf("%d%d",&n,&m),n+m){ 29 memset(opt,0,sizeof(opt)); 30 if((n*m)%2){ 31 puts("0"); 32 continue; 33 } 34 if(n<m) swap(n,m); 35 if(m==1){ 36 puts("1"); 37 continue; 38 } 39 Rep(i,0,(1<<m)-1) 40 if(Check(i,(1<<m)-1)) opt[1][i] = 1; 41 For(i,n) 42 Rep(j,0,(1<<m)-1) 43 Rep(k,0,(1<<m)-1) 44 if(Check(j,k)) 45 opt[i+1][k] += opt[i][j]; 46 printf("%I64d\n",opt[n+1][0]); 47 } 48 return 0; 49 }