Mondriaan‘s Dream
Time Limit: 3000MS | Memory Limit: 65536K | |
Total Submissions: 12315 | Accepted: 7189 |
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 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
1*2的长方形木块,问组成h*w的大长方形的种数。
从第 i行放一个小木块后,可能会对下一行造成影响,但最多只会影响到下一层。
dp[i][j]代表第当1到i-1行铺满后,第i行为状态j的种数。状态j为每一行的二进制压缩1代表存在了木块,0代表不存在。在第i行遍历所有的j,对每一个状态进行dfs,找到可以填满的一种情况,得到下一行的状态。
#include <cstdio> #include <cstring> #include <algorithm> using namespace std ; #define LL __int64 LL dp[15][2100] ; int h , w ; void dfs(int low_i,int low_j,int k1,int k2,int i) { if( i == w ) { dp[low_i+1][k2] += dp[low_i][low_j] ; return ; } if( (k1 & (1<<i)) ) { dfs(low_i,low_j,k1,k2,i+1) ; return ; } dfs(low_i,low_j,k1|(1<<i),k2|(1<<i),i+1) ; if( i+1 < w && (k1&(1<<(i+1))) == 0 ) dfs(low_i,low_j,(k1|(1<<i))|( 1<<(i+1) ),k2,i+2) ; return ; } int main() { int m , i , j ; while( scanf("%d %d", &h, &w) != EOF ) { if( h == 0 && w == 0 ) break ; memset(dp,0,sizeof(dp)) ; dp[1][0] = 1 ; m = 1 << w ; for(i = 1 ; i <= h ; i++) { for(j = 0 ; j < m ; j++) { dfs(i,j,j,0,0); } } printf("%I64d\n", dp[h+1][0]) ; } return 0; }
poj2411--Mondriaan's Dream(状压dp+dfs)