您的位置:首页 > 其它

POJ 2411 状态压缩DP

2014-04-10 10:48 239 查看
原文:http://www.2cto.com/kf/201208/146894.html

编程之美的课后题也有一个和整个题目一样的。(P269)

题目

这个题目的题意很容易理解,在一个N*M的格子里,我们现在有两种类型的 砖块,1 * 2 和 2 * 1,问一共有多少种方案,可以将整个N*M的空间都填满。

最简单的例子就是下面的了:

<span style="font-family:Times New Roman;font-size:14px;">#include<iostream>
#include<algorithm>
#include<cstring>
#include<ctime>
#include<cmath>
#include<string>
#include<cstdio>
#include<vector>
using namespace std;

int h, w;
long long dp[12][2100];

void dfs(int r, int c, int cur, int lst) {
if(r == 1) { // if r == 1 only have two state
if(c == w) {
dp[r][cur]++;
return ;
}
if(c + 1 <= w) dfs(r, c+1, cur<<1, lst);
if(c + 2 <= w) dfs(r, c+2, cur<<2|3, lst);
}
else {
if(c == w) { // one state occurs
dp[r][cur] += dp[r-1][lst];
return;
}
if(c + 1 <= w) {
dfs(r, c+1, cur<<1|1, lst<<1);
dfs(r, c+1, cur<<1, lst<<1|1);
}
if(c + 2 <= w) {
dfs(r, c+2, cur<<2|3, lst<<2|3);
}
}
}

int main() {
while(~scanf("%d%d", &h, &w)) {
if(h + w == 0) break;
if((h*w) & 1) {
puts("0"); continue;
}
if(h < w) swap(h, w);
memset(dp, 0, sizeof(dp));
for(int r = 1; r <= h; ++r)
dfs(r, 0, 0, 0);

printf("%lld\n", dp[h][(1<<w)-1]);
}
return 0;
}
</span>


View Code
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: