POJ_2411_Mondriaan's Dream_状态压缩dp

Mondriaan's Dream
Time Limit: 3000MS   Memory Limit: 65536K
Total Submissions: 15407   Accepted: 8889

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

大意:h,w分别为一个矩形的长和宽(h,m<=11),问用1*2的小矩形(可横竖摆放),问一共有多少种方法。

状态压缩,竖着摆放的小矩形占据该行的该位置和上一行对应位置,0表示该位置不摆放(即下一行竖着摆放的小矩形占据这个位置),1表示该位置摆放(横或竖)
   dfs(l+2,now<<2|3,pre<<2|3);   上下两行横着摆放
     dfs(l+1,now<<1|1,pre<<1);    当前行竖着摆放,并占据上一行对应位置
     dfs(l+1,now<<1,pre<<1|1);    上一行对应位置无论如何被占据,当前行由下一行竖着摆放的矩形占据
dfs搜出所有可能的路径path[num][2];path[i][0]存第i条路的当前状态,
path[i][1]存第i条路的上一行状态。
剩下的dp很常规。
#include<iostream>
#include<cstring>
#include<queue>
#include<cstdio>
#include<map>
#include<algorithm>
using namespace std;
#define LL long long

int path[14000][2];
LL dp[15][2<<11];
int h,w,num;
void dfs(int l,int now,int pre)
{
    if(l>w)
        return;
    if(l==w)
    {
        path[num][0]=now;
        path[num++][1]=pre;
        return;
    }
    dfs(l+2,now<<2|3,pre<<2|3);
    dfs(l+1,now<<1|1,pre<<1);
    dfs(l+1,now<<1,pre<<1|1);
}

int main()
{
    while(scanf("%d%d",&h,&w)!=EOF&&h+w)
    {
        num=0;
        dfs(0,0,0);
        memset(dp,0,sizeof(dp));
        dp[0][(1<<w)-1]=1;
        for(int i=0;i<h;i++)
            for(int j=0;j<num;j++)
                dp[i+1][path[j][0]]+=dp[i][path[j][1]];
        printf("%I64d\n",dp[h][(1<<w)-1]);
    }
    return 0;
}

 

posted on 2016-08-16 15:48  JASONlee3  阅读(201)  评论(0编辑  收藏  举报

导航