填坑行动12-状压DP

@

板子题

Mondriaan's Dream
题目描述
Squares and rectangles fascinated the famous Dutch painter Piet Mondriaan. One night, after producing the drawings in his 'toilet series' (where he had to use his toilet paper to draw on, for all of his paper was filled with squares and rectangles), he dreamt of filling a large rectangle with small rectangles of width 2 and height 1 in varying ways.

Expert as he was in this material, he saw at a glance that he'll need a computer to calculate the number of ways to fill the large rectangle whose dimensions were integer values, as well. Help him, so that his dream won't turn into a nightmare!

输入
The data is made up of two integer numbers: the height h and the width w of the large rectangle. Otherwise, 1<=h,w<=11.
输出
output the number of different ways the given rectangle can be filled with small rectangles of size 2 times 1. Assume the given large rectangle is oriented, i.e. count symmetrical tilings multiple times.
样例输入
4 11
样例输出
51205
题目就不翻译了,不懂的自己机翻
由于当时POJ挂了所以图片暂时没有。

思路

还是那句话:

如果有一道题,可以用贪心,也可以用搜索,这么这道题目一定是DP。——FLYing

显然这题可以搜索,复杂度Θ(2hw),明显会T飞,既然没有要求输出步骤,那么考虑DP。
显然,对于每一层可以有一个轮廓线,我们可以用01的方法轮廓线记录下来,但是我们发现,如果开一个维度为w的数组的话就不方便操作,那么就可以用到状态压缩,简称状压,我们就可以把这些01数字表示成一个二进制int类型的整数,就可以存下来了。

接下来推DP式。
不难得出,两个状态通过按位与之后一定是0。因为两个状态不可能同时是1,。
由于砖块是1×2的,所以按位或之后一定会出现偶数个0,因为只有砖块横着放才会出现这样的情况,当然这个条件需要预处理。

算法复杂度Θ(2w+4wk)

不保证正确的代码:

#include<cstdio>
#include<cstdlib>
using namespace std;
typedef long long ll;
bool f[2139];
int h,w,tmp;
void pre(){
int t[15],flag,x;
for(int i=0;i<(1<<w);i++){
x=i;
flag=1;
for(int k=1;k<=w;k++){
t[k]=x&1;
x>>=1;
}
for(int k=1;k<w;k++)
if(t[k]==0){
if(t[k+1]==0) t[k+1]=1;
else{ flag=0;break; }
}
if(t[w]==0) flag=0;
if(flag) f[i]=1;
}
return;
}
ll a[15][2139];
int main(){
//freopen("1.in","r",stdin);
scanf("%d%d",&h,&w);
pre();
a[0][0]=1;
for(int i=1;i<=h+1;i++)
for(int j=0;j<(1<<w);j++)
for(int k=0;k<(1<<w);k++)
if((j&k)==0 && (f[j|k]))
a[i][j]+=a[i-1][k];
/*printf("%d\n",1<<w);
for(int i=0;i<(1<<w);i++){
printf("%d %d\n",i,f[i]);
//getchar();
}
for(int i=0;i<=h;i++){
for(int j=0;j<(1<<w);j++)
printf("%d ",a[i][j]);
printf("\n");
}*/
printf("%lld",a[h][0]);
return 0;
}
posted @   jiangtaizhe001  阅读(42)  评论(0编辑  收藏  举报
编辑推荐:
· 开发者必知的日志记录最佳实践
· SQL Server 2025 AI相关能力初探
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
阅读排行:
· 阿里最新开源QwQ-32B,效果媲美deepseek-r1满血版,部署成本又又又降低了!
· Manus重磅发布:全球首款通用AI代理技术深度解析与实战指南
· 开源Multi-agent AI智能体框架aevatar.ai,欢迎大家贡献代码
· 被坑几百块钱后,我竟然真的恢复了删除的微信聊天记录!
· AI技术革命,工作效率10个最佳AI工具
点击右上角即可分享
微信分享提示