您的位置:首页 > 其它

hdu 1400 Mondriaan's Dream(DP+状态压缩)

2013-01-29 14:46 483 查看

Mondriaan's Dream

Time Limit: 20000/10000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)

Total Submission(s): 555 Accepted Submission(s): 377



[align=left]Problem Description[/align]
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!



[align=left]Input[/align]
The input file 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.

[align=left]Output[/align]
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.

[align=left]Sample Input[/align]

1 2
1 3
1 4
2 2
2 3
2 4
2 11
4 11
0 0


[align=left]Sample Output[/align]

1
0
1
2
3
5
144
51205


[align=left]Source[/align]
University of Ulm Local Contest 2000

[align=left]Recommend[/align]
JGShining

思路:状态压缩DP,把横放的记作1,竖放的前行记作0,下行的记作1.那么凡是1的都将不影响下一行,凡是0的,它的下一行必然是1.
dp[x][statue];第x行的状态。那么答案就是dp[h][1<<w-1]
dp[x][statue]=sum(dp[x-1][k_statue])k_statue是所有能转化到statue的合法状态。
横放,当前是1,则旁边必有一个1.

#include<iostream>
#include<cstring>
using namespace std;
const int mm=1<<13;
long long dp[14][mm];
int w,h;
bool line_one(int x)
{
for(int i=0;i<w;)
if((x&(1<<i))>0)
{
if(i==w-1||(x&(1<<(i+1)))==0)
return 0;
i+=2;
}else i++;
return 1;
}
bool trans(int a,int b)
{
for(int i=0;i<w;)
{
if((a&(1<<i))>0)
{
if((b&(1<<i))==0)i++;
else if(i==w-1||(a&(1<<(i+1)))==0||(b&(1<<(i+1)))==0)return 0;
else i+=2;
}
else if((b&(1<<i))>0)i++;
else return 0;
}
return 1;
}
int main()
{
while(cin>>w>>h)
{
if(w==0&&h==0)break;
if(w>h){int zz=w;w=h;h=zz;}
int z=1<<w;
for(int i=0;i<z;i++)
if(line_one(i))///判断一层状态合法
dp[1][i]=1;
for(int i=2;i<=h;i++)
{
for(int j=0;j<z;j++)
{ dp[i][j]=0;
for(int k=0;k<z;k++)
if(trans(k,j))///判断k状态能否转化到j状态
dp[i][j]+=dp[i-1][k];
}
}

cout<<dp[h][z-1]<<"\n";
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: