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

Ulm Local 2000




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


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




#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#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<










你可能感兴趣的:(————ACM相关————,——动态规划——,ACM,-,状压DP)