您的位置:首页 > 其它

POJ 2411 Mondriaan's Dream

2014-03-05 17:39 134 查看
状态压缩dp。搜了n多题解。。。都看不懂- -!最后在波神的解释下将discuss里一超短神解理解了。。。不能更神。详细注释下。代码完全是抄的。

#include <cstdio>
#include <algorithm>
#include <cstring>
#include <cstdlib>
#include <iostream>
#include <cmath>
using namespace std;
typedef long long LL;
#define INF 1000000007
#define N 1000
int h, w;
LL p;
LL dp[13][1 << 12];
void dfs(int i, int s, int pos){当前行, 前一行状态, 当前列位置
if(pos == w){dp[i][s] += p;return ;}//结束时将当前行s状态加上前一行转移过来的状态数
dfs(i, s, pos + 1);//若当前位置(为0)不放直接跳过,若竖直放置(为1)则已经处理过直接跳过
if(pos <= w - 2 && !(s & (1 << pos)) && !(s & (1 << pos + 1)))dfs(i, s | (1 << pos) | (1 << pos + 1), pos + 2);//水平放置
}
int main(){
while(scanf("%d%d", &h, &w), h || w){
memset(dp, 0, sizeof(dp));
p = 1;
dfs(1, 0, 0);
for(int i = 2; i <= h; i++){
for(int j = 0; j < (1 << w); j++){
if(dp[i - 1][j])p = dp[i - 1][j];
else continue;
dfs(i, ~j & ((1 << w) - 1), 0);//将前一行状态置反,即前一行没放的位置当前行状态直接处理为1
}
}
printf("%I64d\n", dp[h][(1 << w) - 1]);
}

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