跳到正文

个人记录

[POJ-2411] Mondriaan's Dream 题解报告

くろねこ约 4 分钟
原文
文章信息与版本

文章信息

发布
2018年12月8日
收录
2026年8月6日
更新
2026年11月17日
篇幅
约 4 分钟
获赞
3

版本

  1. v1当前

原题传送门!

初看该题的数据范围,肯定是一道状压dp如果你不怕TLE就是dfs

对于每一种方案,我们考虑以行为界进行状态压缩,当且仅当方块竖直放置时我们将其上一格置为1

代码就显而易见了


C++
/*
当木块为竖直放置时,状态为
 - 
|1|
 - 
|0|
 - 
其余都不会有1的状态 
*/
#include <cstdio>
#include <cmath>
#include <cstring>
#include <algorithm>
#include <iostream>
using namespace std;
#define rg register
#define LL long long
template <typename qwq> inline void read(qwq & x)
{
	x = 0;
	rg int f = 1;
	rg char c = getchar();
	while (c < '0' || c > '9')
	{
		if (c == '-') f = -1;
		c = getchar();
	}
	while (c >= '0' && c <= '9')
	{
		x = (x << 1) + (x << 3) + (c ^ 48);
		c = getchar();
	}
	x *= f;
}
template <typename qaq> inline void print(qaq x)
{
	if (x < 0)
	{
		x = -x;
		putchar('-');
	}
	if (x > 9) print(x / 10);
	putchar(x % 10 + '0');
}
LL dp[12][1 << 12];
bool state[1 << 12];
bool tot,used;
int n,m;
inline void prework()//预处理 
{
	for (rg int i = 0;i < (1 << m);++i)//枚举每行可能的状态 
	{
		tot = false;
		//tot:当前状态下"连续的0"是偶数个还是奇数个,偶数false,奇数true 
		for (rg int j = 0;j < m;++j)//枚举列数 
		{
			if ((i >> j) & 1)//当前点为1 
			{
				if (tot == true)//当前是奇数个0 
				{
					break;//直接跳出
				}
			}
			else tot ^= 1;//否则取反,由于只判断奇偶,可以直接用0 1代替奇偶,不需要相加,更快 
		}
		if (!tot) state[i] = true;
		else state[i] = false;//下面没有memset state,所以要加一步false置值 
	}
}
int main()
{
	while(1)
	{
		read(n),read(m);
		if (n & 1 && m & 1)
		{
			putchar('0'),putchar('\n');
			continue;
		}
		if (!n && !m) return 0;
		memset(dp,0,sizeof dp);
		prework();
		dp[0][0] = 1;
		for (rg int i = 1;i <= n;++i)//行 
		{
			for (rg int j = 0;j < (1 << m);++j)//i 行状态 
			{
				for (rg int k = 0;k < (1 << m);++k)//i - 1 行状态 
				{
					if (!(j & k) && state[j | k])//如果 i 行与 i - 1 行的该状态兼容,进行操作
					//j & k是保证不会出现上下同为1的尴尬局面,不然下面上面那个1会很为难(雾) 
					{
						dp[i][j] += dp[i - 1][k];
					}
				}
			}
		}
		print(dp[n][0]),putchar('\n');//由于最后一行不会有1(没有下一行来放了)所以状态全为0 
	}
}
原文
存档评论2 条抓取于