POJ - 2411 Mondriaan's Dream(状压DP,详细解释)

Mondriaan's Dream

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





题意:给你一堆1*2的骨牌,问你铺满h*w的棋盘有多少种铺法。


解题思路:状压DP,做完这道题,对状态压缩有更深的理解了。我们用dp[i][j]代表前i个行已经铺好了,且第i行的状态为j的方法数。如11011就代表该行的第1245个格子有骨牌。然后我们就可以枚举所有状态,如果当前状态能够有一个方法使其变为11111111....,那么就状态转移。这个铺的过程用深度优先搜索枚举铺的方法。这道题启发了我状压DP肯定是要遍历所有状态的……详细看代码。




#include<iostream>
#include<deque>
#include<memory.h>
#include<stdio.h>
#include<map>
#include<string>
#include<algorithm>
#include<vector>
#include<math.h>
#include<stack>
#include<queue>
#include<set>
#define INF 1<<29
using namespace std;
typedef long long int ll;

ll dp[15][1<<12];
int h,w;

//当前行的初始状态,当前行现在的状态,下一行的状态,现在是第几列,现在是第几行
void dfs(int prestate,int nowstate,int nextstate,int n,int cnt){

    //如果该行被填满了
    if(n>=w){
        dp[cnt+1][nextstate]+=dp[cnt][prestate];//状态转移
        return;
    }

    //如果该列已经有骨牌,跳到下一列
    if(((1<<n)&nowstate)!=0){
        dfs(prestate,nowstate,nextstate,n+1,cnt);
        return;
    }

    //如果该列没有骨牌,那么往该列竖着插一个骨牌
    dfs(prestate,nowstate|(1<<n),nextstate|(1<<n),n+1,cnt);

    //如果有两个空位
    if(n+1<w&&((1<<(n+1))&nowstate)==0){
        //往这两个空位横着放一个骨牌,只影响当前行的状态,不影响下一行
        dfs(prestate,nowstate|(1<<n)|(1<<(n+1)),nextstate,n+2,cnt);
    }

}


int main(){

    while(~scanf("%d%d",&h,&w)){
        if(h==0&&w==0)
            break;

        memset(dp,0,sizeof(dp));
        
        dp[1][0]=1;//空的铺法只有一种,就是空。


        int state=1<<w;

        for(int i=1;i<=h;i++)//一行一行的递推
            for(int j=0;j<state;j++)//遍历所有状态
                dfs(j,j,0,0,i);

        printf("%lld\n",dp[h+1][0]);

    }

    return 0;
}


















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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值