POJ 2411-Mondriaan's Dream(状压DP->骨牌铺满问题)

Mondriaan's Dream
Time Limit: 3000MS Memory Limit: 65536K
Total Submissions: 17226 Accepted: 9935

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

[Submit]   [Go Back]   [Status]   [Discuss]

Home Page   Go Back  To top

题意:给你一个n*m的矩形,让你用1*2的矩形覆盖它,使得该矩形被铺满,问你有多少种方案。

题解:这是一个经典问题,记得去年省赛好像出了,但是当时是一脸懵逼。。。。。听说2*n的矩形覆盖的方案数是一个公式。。感觉6到飞起,然而这道题是n*m的矩形,呢就只能老老实实状压DP了,但其实这道题的姿势好像有很多。。。什么轮廓线DP,插头DP,dfs什么的。。。然而我都不会,就只能状压随便搞搞了,然而具体要怎么搞呢,问题还是如何将当前已经放好的骨牌转化成2进制的形式,我们定义DP[i][j]表示前i-1行已经铺好的情况下第i行状态为j的方案数。。。然后就是状态转移了。。怎么转移呢??仔细想想都有那些情况。

首先假如当前行第j列的骨牌是竖着放的,呢要保证不与之前已经放好的骨牌冲突的情况下,第i-1行第j列必须是空着的,也就是该位是0,不然一定会冲突的是吧,故假如第i-1行第j列是空着的,呢此时第i行第j列就已经确定,且是唯一确定的!齐次就是横着放的情况,这种情况是不影响第i-1行的,只需判定当前状态第i+1位是否为1即可。。。。其他就是自己与自己冲突的情况了,代码里有注释。。。。

#include<map>      
#include<stack>      
#include<queue>      
#include<vector>      
#include<math.h>      
#include<stdio.h>      
#include<iostream>      
#include<string.h>      
#include<stdlib.h>      
#include<algorithm>      
using namespace std;      
typedef long long  ll;      
#define inf 1000000000     
#define mod 100000000       
#define  maxn  1<<13    
#define  lowbit(x) (x&-x)      
#define  eps 1e-10  
int n,m;
ll dp[15][maxn];
bool check(int x)
{
	for(int i=0;i<m;)
	{
		if(x&(1<<i))
		{
			if(i==m-1)
				return 0;
			if(x&(1<<(i+1)))
				i+=2;
			else
				return 0;
		}
		else
			i++;
	}
	return 1;
}
bool ok(int x,int y)
{
	for(int i=0;i<m;)
	{
		if(x&(1<<i))
		{
			if(y&(1<<i))//第i-1行的第j列也为1,则第i行必然是横放
			{
				//若第i行和第i-1行的j+1列不是1,则非法
				if(i==m-1 || !(x&(1<<(1+i))) || !(y&(1<<(1+i))))
					return 0;
				else
					i+=2;
			}
			else//第i-1行第j列为0,则说明第i行第j列是竖放的
				i++;
		}
		else//第i行第j列为0,则第i-1行的第j列必须是已经填充了的。
		{
			if(y&(1<<i))//若已经填充
				i++;
			else
				return 0;
		}
	}
	return 1;
}
void work()
{
	int i,j,k;
	if(n<m)swap(n,m);//让每一行的状态数尽可能少
	memset(dp,0,sizeof(dp));
	for(i=0;i<(1<<m);i++)
		if(check(i))
			dp[1][i]=1;
	for(i=2;i<=n;i++)
		for(j=0;j<(1<<m);j++)
			for(k=0;k<(1<<m);k++)
				if(ok(j,k))
					dp[i][j]+=dp[i-1][k];
	printf("%lld\n",dp[n][(1<<m)-1]);
}
int main(void)
{
	while(scanf("%d%d",&n,&m)!=EOF)
	{
		if(n== 0 && m==0)
			break;
		if(n*m%2!=0)
		{
			printf("0\n");
			continue;
		}
		work();
	}
	return 0;
}


  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值