291. 蒙德里安的梦想

题目链接

291. 蒙德里安的梦想

求把 \(N×M\) 的棋盘分割成若干个 \(1×2\) 的的长方形,有多少种方案。

例如当 \(N=2,M=4\) 时,共有 \(5\) 种方案。当 \(N=2,M=3\) 时,共有 \(3\) 种方案。

如下图所示:

image

输入格式

输入包含多组测试用例。

每组测试用例占一行,包含两个整数 \(N\)\(M\)

当输入用例 \(N=0,M=0\) 时,表示输入终止,且该用例无需处理。

输出格式

每个测试用例输出一个结果,每个结果占一行。

数据范围

\(1≤N,M≤11\)

输入样例:

1 2
1 3
1 4
2 2
2 3
2 4
2 11
4 11
0 0

输出样例:

1
0
1
2
3
5
144
51205

解题思路

状压dp

  • 状态表示:\(f[i][j]\) 表示前 \(i\) 列状态为 \(j\) 时的合法方案数

  • 状态计算:\(f[i][j]=\sum_{}f[i-1][k]\),其中 \(k\)\(i-1\) 列合法状态

分析:总的合法方案数为横着放小方块的总的合法方案数,其余由竖着的小方块塞进去即可,设当前列 \(i\) 由前面开始横着放的小方块状态为 \(1\),否则为 \(0\),则其可由 \(i-1\) 转移过来,注意第一列和最后一列的下一列状态为 \(0\)

  • 时间复杂度:\(O(m\times 4^n)\)

代码

// Problem: 蒙德里安的梦想
// Contest: AcWing
// URL: https://www.acwing.com/problem/content/293/
// Memory Limit: 64 MB
// Time Limit: 5000 ms
// 
// Powered by CP Editor (https://cpeditor.org)

// %%%Skyqwq
#include <bits/stdc++.h>
 
//#define int long long
#define help {cin.tie(NULL); cout.tie(NULL);}
#define pb push_back
#define fi first
#define se second
#define mkp make_pair
using namespace std;
 
typedef long long LL;
typedef pair<int, int> PII;
typedef pair<LL, LL> PLL;
 
template <typename T> bool chkMax(T &x, T y) { return (y > x) ? x = y, 1 : 0; }
template <typename T> bool chkMin(T &x, T y) { return (y < x) ? x = y, 1 : 0; }
 
template <typename T> void inline read(T &x) {
    int f = 1; x = 0; char s = getchar();
    while (s < '0' || s > '9') { if (s == '-') f = -1; s = getchar(); }
    while (s <= '9' && s >= '0') x = x * 10 + (s ^ 48), s = getchar();
    x *= f;
}

const int N=1<<11;
int n,m;
LL f[12][N];
bool st[N];
int main()
{
    while(cin>>n>>m,n||m)
    {
    	for(int i=0;i<1<<n;i++)
		{
			int cnt=0;
			st[i]=true;
			for(int j=0;j<n;j++)
				if(i>>j&1)
				{
					if(cnt&1)break;
					else
						cnt=0;
				}
				else
					cnt++;
			if(cnt&1)st[i]=false;
		}
		memset(f,0,sizeof f);
    	f[0][0]=1;
    	for(int i=1;i<=m;i++)
    		for(int j=0;j<1<<n;j++)
    			for(int k=0;k<1<<n;k++)
    				if((j&k)==0&&st[j|k])f[i][j]+=f[i-1][k];
    		cout<<f[m][0]<<'\n';
    }
    return 0;
}
posted @ 2022-02-28 15:26  zyy2001  阅读(384)  评论(0编辑  收藏  举报