openjudge Mondriaan's Dream

1413:Mondriaan's Dream

  • 查看
  • 提交
  • 统计
  • 提问
总时间限制: 
3000ms 
内存限制: 
65536kB
描述
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!
输入
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.
输出
openjudge Mondriaan's Dream_第1张图片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.
样例输入
1 2
1 3
1 4
2 2
2 3
2 4
2 11
4 11
0 0
样例输出
1
0
1
2
3
5
144
51205
来源

Ulm Local 2000

#include<iostream>
#include<cstdio>
#include<cstring>
using namespace std;
long long  dp[12][1<<12];
int i,j,n,m;
long long num;
void dfs(int i,int p,int x)//搜索扫一道在i行放横着的方块的所有可能,并且把这些状态累加上i-1的出发状态的方法数,如果该方法数为0,直接continue。
{
if (x>=m)//这一层填完了
{
dp[i][p]+=num;
return ;
}
dfs(i,p,x+1);//要注意数组是从左往右存储,而二进制是从右往左即高位靠左
if (x<=m-2&&(!(p&1<<x))&&(!(p&1<<x+1)))//看p的x+1,x+2的二进制位是不是1,如果不是那么可以横着放一块


 dfs(i,p|1<<x|1<<x+1,x+2);//更新状态继续放
}
int main()
{
scanf("%d%d",&n,&m);
while (n!=0&&m!=0)
{
memset(dp,0,sizeof(dp));
num=1;
dfs(1,0,0);//预处理一下第一行
for (i=2;i<=n;i++)
 {
   for (j=0;j<1<<m;j++)//用二进制表示状态,若m=4,则最小的状态时0000,最大的状态是(1<<m)-1即10000-1=1111
     {
      if (dp[i-1][j])
        num=dp[i-1][j];
      else
       continue;
      dfs(i,~j&((1<<m)-1),0);//如果i-1行的出发状态某处未放,必然要在i行放一个竖的方块,所以我对上一行状态按位取反之后的状态就是放置了竖方块的状态。
           }
  }
printf("%lld\n",dp[n][(1<<m)-1]);//因为最后要组成矩形,所以要输出全满的状态
   scanf("%d%d",&n,&m);
}
return 0;
}

你可能感兴趣的:(openjudge Mondriaan's Dream)