POJ 2411 Mondriaan's Dream 贴砖块类状态压缩DP入门

#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;
#define LL long long
int n, m;
LL dp[13][2666];
LL cnt;
int x, M;
void dfs(int y, int pre, int now) {
	if(y == m) {
		if(pre == M-1) {
			dp[x+1][now] += cnt;
		//	printf("x = %d cnt = %d\n", x ,cnt);
		}
		return;
	}
	if(pre&(1<<y) && !(now&(1<<y)) ) {
        if(y+1 < m) dfs(y+2, pre, now|(1<<y)|(1<<(y+1)));
        dfs(y+1, pre, now);
	}
	if(!(pre&(1<<y)) && !(now&(1<<y)))
		dfs(y+1, pre|(1<<y), now|(1<<y));
}
int main() {
	int i, j;
	while( ~scanf("%d%d", &n, &m) && n && m) {
		memset(dp, 0, sizeof(dp));
		M = (1<<m);
		dp[0][M-1] = 1;
		for(i = 0; i < n; i++)
			for(j = 0; j < M; j++) if(dp[i][j]){
				cnt = dp[i][j]; x = i;
			//	printf("i = %d j = %d\n", i, j);
				dfs(0, j, 0);
			}
		printf("%lld\n", dp[n][M-1]);
	}
	return 0;
}

你可能感兴趣的:(POJ 2411 Mondriaan's Dream 贴砖块类状态压缩DP入门)