您的位置:首页 > 其它

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

2013-03-04 22:23 381 查看
Mondriaan's Dream
Time Limit: 3000MS
Memory Limit: 65536K
Total Submissions: 8702Accepted: 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;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: