Mondriaan's Dream——状压dp

描述

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.

输出

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.

样例

  • Input
    1 2
    1 3
    1 4
    2 2
    2 3
    2 4
    2 11
    4 11
    0 0
  • Output
    1
    0
    1
    2
    3
    5
    144
    51205

题解

  • 题意很简单,就是一个nm的矩阵,要求用1 2的方格来填满它,问有多少种填法
  • 对于每个位置,放置方块的上半部分用1表示,其他的用0表示
  • 当状态j,k满足以下条件时可用于转移:
    • j&k == 0,即不能一列的相邻位置都放上半部分
    • j|k中所有连续的0都必须有偶数个

      Code

      1
      2
      3
      4
      5
      6
      7
      8
      9
      10
      11
      12
      13
      14
      15
      16
      17
      18
      19
      20
      21
      22
      23
      24
      25
      26
      27
      28
      29
      30
      31
      32
      33
      34
      35
      36
      37
      38
      39
      40
      41
      42
      43
      44
      45
      46
      47
      48
      49
      50
      51
      52
      53
      54
      55
      #include<bits/stdc++.h>
      #define clr(a,b) memset(a,b,sizeof(a))
      #define ll long long
      #define ull unsigned long long
      #define rep(i,a,b) for(int i=a;i<=b;++i)
      #define repd(i,a,b) for(int i=a;i>=b;--i)
      #define dbg(a,i,n) printf("c",a," \n"[i==n])
      #define pii pair<int,int>
      #define pb push_back
      using namespace std;
      const int inf=0x3f3f3f3f;
      const ll linf=(1ll<<62)-1;
      const int N=20;
      const int M=(1<<13);
      const int mod=11380;
      template <typename T>inline void read(T &x){
      char c;int sign=1;x=0;
      do{c=getchar();if(c=='-')sign=-1;}while(c<'0'||c>'9');
      do{x=x*10+c-'0';c=getchar();}while(c>='0'&&c<='9');
      x*=sign;
      }
      ll f[N][M],can[M];
      int n,m,num;
      int main(){

      while(~scanf("%d%d",&n,&m) && n){
      if((m*n)%2==1){
      printf("0\n");
      continue;
      }
      for(int i=0;i< (1<<m); i++){
      can[i] = 1, num=0;
      for(int j=0;j< m;j++){
      if((i>>j & 1) == 0) num++;
      else {
      if(num & 1) can[i] = 0;
      num=0;
      }
      }
      if(num & 1) can[i] = 0;
      }
      clr(f,0);
      f[0][0] = 1;
      for(int i=1; i<=n; i++){
      for(int j=0; j< 1<<m; j++){
      for(int k=0; k< 1<<m; k++){
      if(!(k & j) && can[k|j])
      f[i][j] += f[i-1][k];
      }
      }
      }
      printf("%lld\n",f[n][0]);
      }
      return 0;
      }
Donate comment here