Mondriaan's Dream
Time Limit: 3000MS | Memory Limit: 65536K |
Total Submissions: 18359 | Accepted: 10487 |
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
2
3
5
144
51205
插頭dp模闆題之:1*2的多米諾骨牌放滿網格紙的方案數。
這個玩意有超級多變種,比如說:
1.不放滿
2.不允許有貫穿網格的直線
3.矩陣版(行和列其中一個<=6另一個血大)
4.(骨牌形狀的多樣性)
當然這些本質都是一樣的:插頭dp。
這種題的共同特點就是不能像普通狀壓dp一樣把整行整列當做狀态的一部分,這樣不如把
适合題目的輪廓線當成狀态的一部分來的簡單。
(留個坑,基于連通性的狀壓dp還完全不會(據說轉移方案一般都是兩位數寫起來賊帶勁hhhh))
#include<iostream>
#include<cstring>
#include<algorithm>
#include<cstdlib>
#include<cstdio>
#include<cmath>
#define ll long long
using namespace std;
ll f[2][3000],n,m,ans;
int ci[30],now,nxt,to;
int main(){
ci[0]=1;
for(int i=1;i<=20;i++) ci[i]=ci[i-1]<<1;
while(scanf("%lld%lld",&n,&m)==2&&n&&m){
if(n>m) swap(n,m);
now=ans=0,memset(f,0,sizeof(f));
/*
if(n==1){
if(m&1) puts("0");
else printf("%d\n",m>>1);
continue;
}
*/
f[0][ci[n]-1]=1;
for(int i=0;i<m;i++)
for(int j=0;j<n;j++){
nxt=now^1;
memset(f[nxt],0,sizeof(f[nxt]));
for(int S=0;S<ci[n];S++){
if(!(S&ci[n-1])){
//豎着放
to=(S<<1)|1;
f[nxt][to]+=f[now][S];
}else{
if(j&&!(S&1)){
//橫着放
to=((S<<1)|3)^ci[n];
f[nxt][to]+=f[now][S];
}
//不放
f[nxt][(S<<1)^ci[n]]+=f[now][S];
}
}
now=nxt;
}
ans=f[now][ci[n]-1];
printf("%lld\n",ans);
}
return 0;
}
我愛學習,學習使我快樂