poj 2411 Mondriaan's Dream

Mondriaan's Dream
Time Limit: 3000MS Memory Limit: 65536K
Total Submissions: 9362 Accepted: 5409

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

Source

第一个状态压缩dp。状态压缩真是个神奇的东西!

最上面的为第1行,最下面为第n行

从上到下按行DP

其中一行的状态我们用一个二进制表示,0表示该格没有向下突出,1表示向下突出了

最后得到一个01串,这个串变回十进制就是一个状态

定义状态dp[i][s],表示前i-1行已经放满,第i行的状态为s的方案数

状态转移方程为 dp[i][s]=sum{ dp[i-1][ss] } ,其中状态s与状态ss兼容

如果上行j格为1那么该行j格必须为0.如果上行j格为0那么该行j格可以为1.如果上行j格j+1格都为0那么该行该位置可以横放即j格j+1格也为0。

那么目标状态是什么,就是dp[n][0],即第n-1行以上全部覆盖满,第n行的状态为0没有向下突出即整个矩形全部被覆盖满了的状态

#include <iostream>
#include<stdio.h>
#include<string.h>
using namespace std;
__int64 dp[15][1<<12];//dp[i][j]表示第i行状态为j时的方法数
int w,h;
void dfs(int p,int s1,int s2,int d)//p为行数.s1为上一行状态。s2为本行状态
{
    if(d==w)//如果枚举到最后一列满足条件的话
    {
        dp[p][s2]+=dp[p-1][s1];//该行状态为s2的方法数加上上行状态为s1的方法数
        return;
    }
    if((s1&(1<<d))==0)//如果上上第d列为0
    {
        dfs(p,s1,s2|(1<<d),d+1);//该行可放1
        if(d<w-1&&(s1&(1<<(d+1)))==0)//如果该行还有两个空位的话可以放00
            dfs(p,s1,s2,d+2);
    }
    else dfs(p,s1,s2,d+1);//否则只能放0
}
int main()
{
    int i,j;
    while(scanf("%d%d",&h,&w),h||w)
    {
        if((w*h)%2)//由于砖是1*2,2*1的所以铺成面积一定是偶数
        {
            printf("0\n");
            continue;
        }
        memset(dp,0,sizeof dp);
        dp[0][0]=1;
        for(i=1; i<=h; i++)//枚举行数
        {
            for(j=0; j<(1<<w); j++)//枚举上一行状态
                if(dp[i-1][j])
                    dfs(i,j,0,0);
        }
        printf("%I64d\n",dp[h][0]);
    }
    return 0;
}


 


  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值