Mondriaan's Dream
Time Limit: 3000MS | Memory Limit: 65536K | |
Total Submissions: 8892 | Accepted: 5136 |
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!
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
/*
目的:用!*2瓷砖铺满规定规格的地板,求有多少种方案
措施:
总体思想:利用状态压缩的动态规划法:
给地板每个1*1的小方格定位置状态,且状态只有0和1两种,然后一行一行地铺地板,
每行都和前一行相比较看是否和法
首先:初始化状态,设横铺上的位置为1,竖铺位置的前一行所在位置为0.后一行为1(这样做的目的是为了便于区分横铺和竖铺0
),由于第一行是首行,所以必须先初始化:(若(i,j)=1,则为横铺故(i,j+1)=1,跳到(i,j+2);
若(i,j)=0,跳到(i,j+1);并将合法状态设立 为1;
其次:判断合法情况,这里分为两种情况:(注意边界情况)
一,(i,j)=0,为竖铺,则(i-1,j)必为1,比较 j+1
二,(i,j)=1时:若:(i-1,x)=0时,为竖铺,跳转到j+1;若(i-1,j)=1时,为横铺则(i,j+1)=1.(i-1,j+1)=1,跳转至j+2
如不符合以上条件则不合法,退出
最后:定状态转移方程,dp[i][j]:用来表示第i行的j状态,则dp[i][j]+=dp[i-1][k],此时j和k必须合法;*/
#include<string.h>
#include<stdlib.h>
#include<stdio.h>
#include <iostream>
const int N=11;
long long dp[N][2<<N];
int w,h,sta;
int firstlinesta(int j)
{
int i=0;
while(i<w)
{
if(j&(1<<i))//在使用位运算符时要注意运算的优先性质
{
if((j&(1<<(i+1)))==0||i==w-1)
return 0;
i+=2;
}
else
i++;
}
return 1;
}
void initfirstline()
{
for(int i=0;i<sta;i++)
{
if(firstlinesta(i))
dp[0][i]=1;
//printf("dp[i][%d]=%d\n",i,dp[0][i]);
}
}
int cmptwolines(int j,int k)
{
int i=0;
while(i<w)
{
if(j&(1<<i))
{
if((k&(1<<i))==0)
i++;
else if((i==w-1)||!((j&(1<<(i+1)))&&(k&(1<<(i+1))) ))
return 0;
else i+=2;
}
else
{if((k&(1<<i))==0)
return 0;
i++;
}
}
return 1;
}
void work()
{ int i,j,k;
for(i=1;i<h;i++)
for(j=0;j<sta;j++)
for(k=0;k<sta;k++)
if(cmptwolines(j,k))
dp[i][j]+=dp[i-1][k];
printf("%lld\n",dp[h-1][sta-1]);
}
int main()
{
while(scanf("%d%d",&w,&h)!=EOF)
{ if(w==0&&h==0)
break;
int t;
if(w>h)
{t=w;
w=h;h=t;
}
sta=2<<(w-1);
//printf("%d",2<<12);
memset(dp,0,sizeof(dp));
initfirstline();
work();
}
return 0;
}