首页 > 代码库 > UVA 11270 Tiling Dominoes(插头DP)

UVA 11270 Tiling Dominoes(插头DP)

题目链接:11270 - Tiling Dominoes

和POJ 2411一样的题目,试着用插头DP写了下
代码:
#include <stdio.h>
#include <string.h>
#include <algorithm>
using namespace std;

int n, m, pre = 0, now = 1;
long long dp[2][2222];
int main() {
	while (~scanf("%d%d", &n, &m)) {
		if (n<m) swap(n,m);
		int maxs = (1<<m);
		memset(dp[now], 0, sizeof(dp[now]));
		dp[now][maxs - 1] = 1;
  		for (int i = 0; i < n; i++) {
			for (int j = 0; j < m; j++) {
				swap(pre, now);
				memset(dp[now], 0, sizeof(dp[now]));
				for (int s = 0; s < maxs; s++) {
					if (j && !(s&1<<(j-1)) && (s&1<<j)) //横放的情况
						dp[now][s|1<<(j-1)] += dp[pre][s];
					dp[now][s^1<<j] += dp[pre][s];//竖放的情况
    			}
   			}
  		}
  		printf("%lld\n", dp[now][maxs - 1]);
 	}
	return 0;
}