【POJ2411】Mondriaan's Dream

来源:互联网 发布:node passport 编辑:程序博客网 时间:2024/06/14 08:13

【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 msMemory Limit:65536 KBCase score:10Comparison:TraditionalLevel:5
 Congratulation !Users Submitted:68Users Solved:53Total Submits:123Accepted:66Time Out:8Wrong Answer:19Compile Error:30
Submit Status Discuss
Add notes and tags...
 Unmark


0 0