首页 > 代码库 > POJ 2411 状压dp

POJ 2411 状压dp

F - Mondriaan‘s Dream
Time Limit:3000MS     Memory Limit:65536KB     64bit IO Format:%I64d & %I64u
Submit Status
Appoint description: 

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 21 31 42 22 32 42 114 110 0

Sample Output

10123514451205

压缩时,0是横放或竖放的下半部分,1是竖放的上半部分。
注意:
要用long long;

 

 

  1 #include<iostream>  2 #include<cstring>  3 #include<cstdlib>  4 #include<cstdio>  5 #include<algorithm>  6 #include<cmath>  7 #include<queue>  8 #include<map>  9 #include<vector> 10 #include<set> 11  12 #define N 1130 13 #define M 15 14 #define inf 1000000007 15 #define mod 1000000007 16 #define mod2 100000000 17 #define ll long long 18 #define maxi(a,b) (a)>(b)? (a) : (b) 19 #define mini(a,b) (a)<(b)? (a) : (b) 20  21 using namespace std; 22  23 int h,w; 24 ll dp[15][ (1<<11)+10 ]; 25 int s[15]; 26 ll ans; 27  28 void ini() 29 { 30     memset(dp,0,sizeof(dp)); 31     ans=0; 32 } 33  34 int check(int o) 35 { 36     int cou=0; 37     //int flag=1; 38     int j=0; 39     for(j=0;j<w;j++){ 40         s[j]=o%2; 41         o/=2; 42     } 43     j=0; 44     while(j<w) 45     { 46         while(s[j]==1 && j<w) j++; 47         cou=0; 48         while(s[j]==0 && j<w){ 49             cou++;j++; 50         } 51         if(cou%2==1) return 0; 52     } 53     if(cou%2==1) return 0; 54     return 1; 55 } 56  57 void solve() 58 { 59     int o,i,p,te; 60     if(h*w%2==1) return; 61     for(o=0;o<(1<<w);o++){ 62         if( check(o)==0 ) continue; 63         dp[1][o]=1; 64        // printf(" o=%d\n",o); 65     } 66  67     for(i=2;i<h;i++){ 68         for(o=0;o<(1<<w);o++){ 69             for(p=0;p<(1<<w);p++){ 70                 if( (p&o)!=0 ) continue; 71                 te=o | p; 72                 if( check(te)==0 ) continue; 73                 dp[i][o]+=dp[i-1][p]; 74             } 75         } 76     } 77  78     for(p=0;p<(1<<w);p++){ 79         if( check(p)==0 ) continue; 80         dp[h][0]+=dp[h-1][p]; 81     } 82     ans=dp[h][0]; 83 } 84  85 void out() 86 { 87     printf("%I64d\n",ans); 88 } 89  90 int main() 91 { 92    // freopen("data.in","r",stdin); 93     //freopen("data.out","w",stdout); 94     //scanf("%d",&T); 95    // for(int cnt=1;cnt<=T;cnt++) 96    // while(T--) 97     while(scanf("%d%d",&h,&w)!=EOF) 98     { 99         if(h==0 && w==0) break;100         ini();101         solve();102         out();103     }104     return 0;105 }

 

POJ 2411 状压dp