填坑行动12-状压DP

27 篇文章 0 订阅
13 篇文章 1 订阅

文章目录

板子题

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

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

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

算法复杂度 Θ ( 2 w + 4 w k ) \Theta\left(2^w+4^wk\right) Θ(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; 
}

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值