【POJ2411】Mondriaan's Dream

【POJ2411】Mondriaan's Dream

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. 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

Sample Output

1
0
1
2
3
5
144
51205

Solution

用1*2的方格去覆盖n*m的矩阵,求覆盖方法的总数。
我们发现这其实是一道很简单的基础状压题型,分别枚举每一行可能的状态然后检查两行是否兼容即可。
但是这种暴力的动规方式会产生大量的无效状态,效率不高。(提前进行预处理也可以)
所以我们使用一种和联通有关的dp,采用逐格递推的方式,大大提高运算速度。(也就是传说中的插头+轮廓线dp了)
蒟蒻的程序写得非常丑陋。。。 
#include<iostream>
#include<cstring>
#include<cstdio>
using namespace std;
const long long Maxn=10007,p=1007;
struct OutLine{
	long long cnt;
	long long h[p],state[Maxn],next[Maxn];
	long long f[Maxn];
	inline void Clean(){cnt=0;memset(h,0,sizeof(h));return ;}
	inline void Insert(long long x,long long val){
		long long v=x%p+1;
		for(long long i=h[v];i;i=next[i])
			if(state[i]==x){f[i]+=val;return ;}
		state[++cnt]=x;next[cnt]=h[v];h[v]=cnt;f[cnt]=val;
		return ;
	}
}Line[2];
long long n,m,cur;
long long code[15],map[15][15];
inline void Decode(long long x){for(long long i=m;i>=0;i--)code[i]=x&1,x>>=1;return ;}
inline long long Encode(long long f){
	long long rec=0;
	for(long long i=0;i<=m;i++)rec=((rec<<1)|code[i]);
	return rec>>f;
}
inline void ST(){
	Line[cur].Clean();
	Line[cur].Insert(0,1);
	memset(map,0,sizeof(map));
	for(long long i=1;i<=n;i++)
		for(long long j=1;j<=m;j++)map[i][j]=1;
	return ;
}
inline void DP(long long x,long long y){
	for(long long i=1;i<=Line[cur].cnt;i++){
		Decode(Line[cur].state[i]);
		if(code[y]&&code[y-1])continue;
		if(code[y]||code[y-1]){
			code[y]=code[y-1]=0;
			Line[cur^1].Insert(Encode(y==m),Line[cur].f[i]);
			continue;
		}
		if(map[x][y+1]){
			code[y]=1;code[y-1]=0;
			Line[cur^1].Insert(Encode(y==m),Line[cur].f[i]);
		}
		if(map[x+1][y]){
			code[y]=0;code[y-1]=1;
			Line[cur^1].Insert(Encode(y==m),Line[cur].f[i]);
		}
	}return ;
}
long long ans;
int main(){
	while(scanf("%lld%lld",&n,&m)!=EOF){
		ST();
		for(long long i=1;i<=n;i++){
			for(long long j=1;j<=m;j++){
				Line[cur^1].Clean();
				DP(i,j);cur^=1;
			}
		}ans=0;
		for(long long i=1;i<=Line[cur].cnt;i++)ans+=Line[cur].f[i];
		cout<<ans<<'\n';
	}
	return 0;
}

Hide Information »

 

Case Time Limit: 1000 ms
Memory Limit: 65536 KB
Case score: 10
Comparison: Traditional
Level: 5
 Congratulation !
Users Submitted: 68
Users Solved: 53
Total Submits: 123
Accepted: 66
Time Out: 8
Wrong Answer: 19
Compile Error: 30

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值