[poj P2411] Mondriaan's Dream
Time Limit: 3000MS | Memory Limit: 65536K | |
Total Submissions: 18023 | Accepted: 10327 |
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!
![](https://i-blog.csdnimg.cn/blog_migrate/91cd2a849209448a71d50fa577e5d5ed.jpeg)
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
![](https://i-blog.csdnimg.cn/blog_migrate/9275391b5c2fd4e3cb556e4ab10140b5.jpeg)
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都想不到。。。
先列出总转移方程:f[i][j]=sigma(ok(j,k))f[i-1][k]。
其中j,k是二进制状态,i是第几行。
那么,我们无非就是考虑第i行和第i-1行的状态是否兼容。
如何判断是否兼容?
对于两行k,k+1两个状态,第i位分别为u和d。
我们设状态里面“1”表示当前的格子已经被填了,且是被上一行填入;“0”表示未被填。
如果u==1且d==0,我们可以只能跳到下一列进行判断;否则,
如果u==1且d==1,显然这个状态是不合法的;否则,
如果u==0且d==0,那么上面那一行的下一列必定填入一个横排(且当前为0,表示还没有被填入),我们可以将它当做是被上一行填入的(即更改k行下一列的状态,0->1);否则
如果u==0且d==1,显然正好是当前列放一块竖的。
code:
![](https://i-blog.csdnimg.cn/blog_migrate/8f900a89c6347c561fdf2122f13be562.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/961ddebeb323a10fe0623af514929fc1.gif)
1 #include<cstdio> 2 #include<cstring> 3 #include<algorithm> 4 #include<vector> 5 #define LL long long 6 #define idx(x,i) ((x>>i)&1) 7 using namespace std; 8 const int lim=20; 9 int n,m,S; LL f[lim][1<<lim]; 10 vector <int> o[1<<lim]; 11 bool jug(int su,int sd) { 12 for (int i=0; i<m; i++) { 13 int ku=idx(su,i),kd=idx(sd,i); 14 if (!kd&&ku) continue; 15 if (ku) return 0; else 16 if (!kd) { 17 if (i<m-1&&!idx(su,i+1)) su|=1<<(i+1); else return 0; 18 } 19 } 20 return 1; 21 } 22 int main() { 23 while (scanf("%d%d",&n,&m),n|m) { 24 S=1<<m; 25 for (int i=0; i<S; i++) o[i].clear(); 26 for (int i=0; i<S; i++) 27 for (int j=0; j<S; j++) if (jug(i,j)) o[j].push_back(i); 28 memset(f,0,sizeof f),f[0][0]=1; 29 for (int i=1; i<=n; i++) 30 for (int j=0; j<S; j++) 31 for (int k=0,s=o[j].size(); k<s; k++) 32 f[i][j]+=f[i-1][o[j][k]]; 33 printf("%lld\n",f[n][0]); 34 } 35 return 0; 36 }