poj2411——Mondriaan's Dream(状态压缩DP)

Mondriaan's Dream
Time Limit: 3000MS Memory Limit: 65536K
Total Submissions: 8702 Accepted: 5024
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

解析:

        这是一道状态压缩DP的入门题,用f[i][j]表示第i行状态为j时的方案数。。。

        当前行为1,上一行为0表示竖放;当前行连续两个0表示横放。。。

        对于当前行的状态s,它是由前一行的状态s’转化过来的,显然,对于该行某个位置j:

        如果前一行该位置为0,那么该位置可以直放 即 0-> 1

        如果前一行连续两个位置为0,那么这两个连续位置可以横放 即00-> 00

        如果前一行该位置为1,显然该位置不能再放,于是应该把该位置设置为0 ,即1-> 0

        对于每一种i-1行的情况可以转移到第i行则累加。。。

代码:

        

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

int n,m;
long long f[14][1<<13];

bool check(int s1,int s2)//状态从S2转移到S1
{
    int i,j,k;
    if((s1&s2)!=0) return 0;//判断能否竖放
    s1|=s2;//将两个状态压在s1里
    for(i=0;i<m;)//判断能否横放
    {
        j=s1&(1<<i);
        if(j==0 )
        {
            if(i==m-1)return 0;
            if( (s1& ( 1<<(i+1) ) )!=0  ) return 0;
            i+=2;
        }
        else i++;
    }
    return 1;
}


void readdata()
{
    int t,j,k,i;
    long long sum;
    int tol;
    freopen("xx.in","r",stdin);
    freopen("xx.out","w",stdout);
    while(scanf("%d%d",&n,&m)==2 && m+n)
    {
        if((n*m)%2==1)//特判
        {
            cout<<0<<endl;
            continue;
        }
        if(n == 1){
            puts("1");
            continue;
        }

        if(n<m)swap(n,m);
        tol=(1<<m)-1;
        memset(f,0,sizeof(f));
        f[1][0]=1;
        for(i=1;i<=n;i++)
        {
            for(j=0;j<=tol;j++)
            {
                sum=0;
                for(k=0;k<=tol;k++)
                {
                    if(check(j,k))//检查状态j,k是否兼容
                    {
                        f[i+1][j]+=f[i][k];//累加方案数
                    }
                }
            }
        }
        cout<<f[n+1][0]<<endl;
    }
}

int main()
{
    readdata();
    //work();
    return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值