POJ 2411 状态压缩DP

Mondriaan’s Dream
Time Limit: 3000MS Memory Limit: 65536K
Total Submissions: 14227 Accepted: 8210
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

题意:

给一个n*m的大矩形。
可以用1*2的小矩形横放或者竖放在拼成这个大矩形。问最终有多少种不同的放法。

题解:

定义dp[i][j]表示第i行放的状态是j,每一位0表示没有放,1表示放了。
每一行我们有三种操作:横放,竖放,不放。
横放:
则上一行也必须是横放的,pre<<2|3,cur<<2|3.
竖放:
则上一行必须是没有放的,pre<<1,cur<<1|1.
不放:
则上一行必须是放的,pre<<1|1,cur<<1.
然后记忆化搜索一下推出最终状态。

#define _CRT_SECURE_NO_WARNINGS
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <stack>
#include <string>
#include <set>
#include <cmath>
#include <map>
#include <queue>
#include <sstream>
#include <vector>
#include <iomanip>
#define m0(a) memset(a,0,sizeof(a))
#define mm(a) memset(a,0x3f,sizeof(a))
#define m_1(a) memset(a,-1,sizeof(a))
#define f(i,a,b) for(i = a;i<=b;i++)
#define fi(i,a,b) for(i = a;i>=b;i--)
#define lowbit(a) ((a)&(-a))
#define FFR freopen("data.in","r",stdin)
#define FFW freopen("data.out","w",stdout)
#define INF 0x3f3f3f3f
typedef long long ll;
typedef long double ld;
const ld PI = acos(-1.0);

using namespace std;
#define SIZE ( (1<<12)+100)

ll dp[12][SIZE];
int n, m;

void dfs(int a, int b, int pre, int cur) {
    if (b == m) {
        dp[a][cur] += dp[a - 1][pre];
        return;
    }

    if (b + 1 <= m) {
        dfs(a, b + 1, pre << 1, cur << 1 | 1);
        dfs(a, b + 1, pre << 1 | 1, cur << 1);
    }

    if (b + 2 <= m)
        dfs(a, b + 2, pre << 2 | 3, cur << 2 | 3);
}

int main()
{
    //ios_base::sync_with_stdio(false); cin.tie(0);
    //FFR;
    //FFW;
    int i;

    while (~scanf("%d%d",&n,&m)&&(n||m))
    { 

        if ((n*m) & 1) {
            printf("0\n");
            continue;
        }

        if (m > n) { int tem = m; m = n; n = tem; }

        m0(dp);
        int N = (1 << m) - 1;
        dp[0][N] = 1;
        f(i, 1, n)
            dfs(i, 0, 0, 0);

        printf("%I64d\n", dp[n][N]);
    }
    return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值