Mondriaan's Dream POJ2411

题目描述:

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.
Mondriaan's Dream POJ2411_第1张图片

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

题目分析:

大意是从m*n的矩形中,将数个1*2的小矩形填充,问将其填充完毕有多少种方案?
毫无疑问的状态压缩DP。代码中有详细介绍。

代码如下:

#include <stdio.h>
#include <string.h>
#include <iostream>
#include <algorithm>
#include <vector>
#include <queue>
#include <stack>
#include <set>
#include <map>
#include <string>
#include <math.h>
#include <stdlib.h>
#include <time.h>
using namespace std;
typedef long long ll;
const int INF = 0x3f3f3f3f;
const double eps = 1e-4;
const int MAXN=15;
const int MAX=(1<<11)+10;

ll dp[MAXN][MAX];//dp[i][j]表示在第i行j状态(2进制转化为10进制)方法数(其中i-1及以上行数排列完毕)
int n,m;

bool firstrow(int t)//统计能否在第一行放置t状态
{
    for(int i=0; i<m; )
    {
        if (t & (1<<i))//若为1,则是横放
        {
            if (i==m-1) return false;
            if (t& (1<<(i+1))) i+=2;//横放需要连续两个格子
            else return false;
        }
        else i++;
    }
    return true;
}

bool judge(int tt,int t)
{
    for(int i=0; i<m; )
    {
        if (t & (1<<i))//c行的i列为1
        {
            if (tt & (1<<i))//c-1行的i列为1,说明c行为横放
            {//横放是否合法
                if ((i==m-1) || !(t&(1<<(i+1))) || !(tt&(1<<(i+1))) ) return false;
                else i+=2;
            }
            else i++;//c-1行i列为0,c行i列为1,竖放
        }
        else
        {
            if (tt&(1<<i)) i++;//c行i列为0,那么c-1行i列必须为1
            else return false;
        }
    }
    return true;
}

void DP()
{
    if (n<m) {n^=m;m^=n;n^=m;}//使n更大,状态数量变少
    int max=(1<<m)-1;//最多的状态数
    memset(dp,0,sizeof(dp));
    for(int i=0; i<=max; i++)//第一行所有状态数
    {
        if (firstrow(i)) dp[1][i]=1;//第一行i状态合法置1
    }
    for(int c=2; c<=n; c++)//从第二行开始dp
    {
        for(int i=0; i<=max; i++)//第c行所有状态
        {
            for(int ii=0; ii<=max; ii++)//第c-1行状态,因为第i行状态是受c-1行影响的
            {
                if(judge(ii,i)) dp[c][i]+=dp[c-1][ii];//如果c-1行状态与第c行状态合法,更新c行i状态方法数
            }
        }
    }
    printf("%lld\n",dp[n][max]);//n行max状态(均为1)方法数
}

int main()
{
    while(~scanf("%d%d",&n,&m) && (n || m))
    {
        if (n&1 && m&1) {cout<<0<<endl;continue;}//如果n,m同为奇数,不可能填充完全
        DP();
    }
    return 0;
}

你可能感兴趣的:(状压dp)