POJ2411 Mondriaan's Dream #状压DP#

Mondriaan's Dream

Time Limit: 3000MS   Memory Limit: 65536K
Total Submissions: 24357   Accepted: 13478

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

Solution

参考kuangbin聚聚的题解 https://www.cnblogs.com/kuangbin/archive/2013/04/28/3049684.html

用11表示横块,01表示竖块。

用二进制枚举每一行的各种情况,与上一行各状态运算逐一判断是否合法。

合法的状态应该是相邻两行的或是全1,与的话不存在连续的1的个数是奇数的

#include <iostream>
#include <cstring>
using namespace std;

typedef long long ll;
const int maxn = 12;
const int maxm = 1 << 11;
ll dp[maxn][maxm];

bool check(int s)
{
    int res = 0;
    while (s)
    {
        if (s & 1) res++;
        else
        {
            if (res & 1) return false;
            res = 0;
        }
        s >>= 1;
    }
    return !(res & 1);
}

int main()
{
    int n, m;
    while (~scanf("%d%d", &n, &m) && n && m)
    {
        int tot = 1 << m;
        memset(dp, 0, sizeof(dp));
        for (int i = 0; i < tot; i++) dp[1][i] = check(i);
        for (int i = 1; i < n; i++)
            for (int j = 0; j < tot; j++)
                for (int k = 0; dp[i][j] && k < tot; k++)
                    if ((j | k) == tot - 1 && check(j & k))
                        dp[i + 1][k] += dp[i][j];
        printf("%lld\n", dp[n][tot - 1]);
    }
    return 0;
}
发布了367 篇原创文章 · 获赞 148 · 访问量 2万+

猜你喜欢

转载自blog.csdn.net/qq_35850147/article/details/104096046