您的位置:首页 > 其它

POJ2411 Mondriaan's Dream(压缩DP)

2016-07-18 09:54 489 查看
Mondriaan's Dream
Time Limit: 3000MS Memory Limit: 65536K
Total Submissions: 8974 Accepted: 5187
DescriptionSquares 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!InputThe 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.OutputFor 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 Input1 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;横的用11表示,竖的用法01表示。合法的状态应该是相邻两行的或是全1,与的话不存在连续的1的个数是奇数的。
/*
* 状态压缩DP一个h*w的矩阵(1<=h,w<=11),只能放1*2的模块,问完全覆盖的不同放发有多少种?
* 横着的定义为11,竖着的定义为01,然后按行DP,两行状态相或要全为1.两行相与要没有连续的1的个数是奇数个
*/

#include <iostream>
#include <string.h>
#include <stdio.h>
#include <algorithm>
using namespace std;

bool check(int s)//判断s有没有奇数个连续的1
{
int ret=0;
while(s)
{
if(s&1)ret++;
else
{
if(ret&1)return false;
ret=0;
}
s>>=1;
}
if(ret&1)return false;
return true;
}
long long dp[12][2050];
int main()
{
int n,m;
while(scanf("%d%d",&n,&m)==2)
{
if(n==0 && m==0)break;
int tot=(1<<m);
memset(dp,0,sizeof(dp));
for(int i=0;i<tot;i++)
if(check(i))
dp[1][i]=1;
for(int i=1;i<n;i++)
for(int j=0;j<tot;j++)
if(dp[i][j]!=0)
{
for(int k=0;k<tot;k++)
if( (j|k)==tot-1 && check(j&k) )
dp[i+1][k]+=dp[i][j];
}
printf("%I64d\n",dp
[tot-1]);
}
return 0;
}


关于程序算法艺术与实践更多讨论与交流,敬请关注本博客和新浪微博songzi_tea.
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  动态规划 POJ2411