首页 > 代码库 > POJ 2411 Mondriaan's Dream (状压DP)

POJ 2411 Mondriaan's Dream (状压DP)

题意:给出一个n*m的棋盘,及一个小的矩形1*2,问用这个小的矩形将这个大的棋盘覆盖有多少种方法。

析:对第(i,j)位置,要么不放,要么竖着放,要么横着放,如果竖着放,我们记第 (i,j)位置为0,(i+1,j)为1,如果横着放,那么我们记

(i,j),(i,j+1)都为1,然后dp[i][s]表示 第 i 行状态为 s 时,有多少方法,那么我们就可以考虑与和匹配的状态,这个也很容易判断。

代码如下:

#pragma comment(linker, "/STACK:1024000000,1024000000")
#include <cstdio>
#include <string>
#include <cstdlib>
#include <cmath>
#include <iostream>
#include <cstring>
#include <set>
#include <queue>
#include <algorithm>
#include <vector>
#include <map>
#include <cctype>
#include <cmath>
#include <stack>
#include <sstream>
#define debug() puts("++++");
#define gcd(a, b) __gcd(a, b)
#define lson l,m,rt<<1
#define rson m+1,r,rt<<1|1
#define freopenr freopen("in.txt", "r", stdin)
#define freopenw freopen("out.txt", "w", stdout)
using namespace std;

typedef long long LL;
typedef unsigned long long ULL;
typedef pair<int, int> P;
const int INF = 0x3f3f3f3f;
const LL LNF = 1e16;
const double inf = 0x3f3f3f3f3f3f;
const double PI = acos(-1.0);
const double eps = 1e-8;
const int maxn = 1e6 + 10;
const int mod = 100000000;
const int dr[] = {-1, 0, 1, 0};
const int dc[] = {0, 1, 0, -1};
const char *de[] = {"0000", "0001", "0010", "0011", "0100", "0101", "0110", "0111", "1000", "1001", "1010", "1011", "1100", "1101", "1110", "1111"};
int n, m;
const int mon[] = {0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
const int monn[] = {0, 31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
inline bool is_in(int r, int c){
  return r >= 0 && r < n && c >= 0 && c < m;
}
LL dp[12][1<<11];

bool judge(int i, int j){
  int cnt = 0;
  for(int k = 0; k < m; ++k){
    if(i&(1<<k)){
      if(j&(1<<k))  cnt ^= 1;
      else if(cnt) return false;
    }
    else{
      if(!(j&(1<<k)))  return false;
      if(cnt)  return false;
    }
  }
  return cnt == 0;
}

bool judge1(int i){
  int cnt = 0;
  for(int j = 0; j < m; ++j)
    if(i&(1<<j))  cnt ^= 1;
    else if(cnt)  return false;
  return cnt == 0;
}

int main(){
  while(scanf("%d %d", &n, &m) == 2 && m+n){
    if(n < m)  swap(n, m);
    if(n * m & 1){ cout << "0" << endl;  continue; }
    int all = 1 << m;
    memset(dp, 0, sizeof dp);
    for(int i = 0; i < all; ++i)
      dp[1][i] = judge1(i);
    for(int i = 2; i <= n; ++i){
      for(int j = 0; j < all; ++j)
        for(int k = 0; k < all; ++k)
          if(judge(j, k))  dp[i][j] += dp[i-1][k];
    }
    cout << dp[n][all-1] << endl;
  }
  return 0;
}

  

POJ 2411 Mondriaan's Dream (状压DP)