【状压DP】 Mondriaan's Dream(入门)

12 篇文章 0 订阅
1 篇文章 0 订阅



Problem Description
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! 


 

Input
The input file contains several test cases. Each test case is made up of two integer numbers: the height h and the width w of the large rectangle. Input is terminated by h=w=0. Otherwise, 1<=h,w<=11. 
 

Output
For each test case, 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.
 

Sample Input
  
  
1 2 1 3 1 4 2 2 2 3 2 4 2 11 4 11 0 0
 

Sample Output
  
  
1 0 1 2 3 5 144 51205

题目:http://acm.hdu.edu.cn/showproblem.php?pid=1400

题意:求在一块高为h,宽为w的空板中放满1*2或2*1的积木有多少中放法。

思路:h,w都不大于11,易联想到状压DP(貌似DFS也可以做?)。

确定用DP了。首先要确定的是如何表示状态,这里我们用一个二进制数stae来表示某一层积木的情况。

1表示此位置有木块,0表示空白。dp【i】【stae】表示当第i层的状态为stae时,可以放的方法数。那么答案应该是

dp【h+1】【0】(当第h+1层的状态为0时(00000000,没有任何木块))。

确定了最终状态,我们开始推导状态转移方程。

假设dp【i】【stae】为一个合法的状态,我们把stae转化成一个二进制数,01100(假设),我们去遍历stae的每一位,在空白处放上积木。

如何判断第k位是否为空白?既然是二进制数表示状态,那么显然用位运算会很快。

操作为:stae&(1<<(k-1)),如果stae第k位为1,那么结果应该是1<<(k-1),否则为0。

如果该位为空白,那么我们就可以考虑放1个1*2的积木,如果我们放了一个1*2的积木,就会对第i+1层产生影响,第i+1的第k位也应该为1,下一层的状态next=next|1<<(k-1),或者可以放一个2*1的积木(第k+1位也是0),对下一层的影响为0.

转移方程:dp【i+1】【next】+=dp【i】【stae】。

下面贴出代码:

#include<stdio.h>
#include<string.h>
#include<algorithm>
#define met(s,k) memset(s,k,sizeof s)
#define scan(a) scanf("%d",&a)
#define scann(a,b) scanf("%d%d",&a,&b)
#define scannn(a,b,c) scanf("%d%d%d",&a,&b,&c)
#define prin(a) printf("%d\n",a)
using namespace std;
int w,h;
long long dp[13][1<<11];
void dfs(int i,int j,int now,int next)//当前状态第i层,处理第j个位置,now当前的状态,next对下一层的影响
{
    if(now==(1<<w)-1)//这一层已放满,直接加到对下一层状态为0的贡献
    {
        dp[i+1][0]+=dp[i][now];
        return ;
    }
    if(j==w)//当前层放满,计算对next的贡献
    {
        dp[i+1][next]+=dp[i][now];
        return ;
    }
    if(!(now&(1<<j)))dfs(i,j+1,now,next|(1<<j));//有空位,放1*2的情况
    else dfs(i,j+1,now,next);//无空位
    if(!((now&(1<<j))||(now&(1<<(j+1))))&&j+2<=w)dfs(i,j+2,now,next);//放2*1的情况
}
int main()
{
    while(scanf("%d%d",&w,&h),w)
    {
        memset(dp,0,sizeof dp);//初始化,0代表状态不合法,所有合法状态都是又其他合法状态转移而来
        dp[1][0]=1;//初始化状态,什么都不放就只有一种方法,也是已知的唯一一个合法状态
        for(int i=1; i<=h; i++)
        {
            int mx=1<<w;//该层所有可能的合法状态
            for(int j=0; j<mx; j++)
            {
                if(dp[i][j])dfs(i,0,j,0);//判断状态是否合法,若合法dfs转移到下一个状态
            }
        }
        printf("%lld\n",dp[h+1][0]);
    }
    return 0;
}


Problem Description
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! 


 

Input
The input file contains several test cases. Each test case is made up of two integer numbers: the height h and the width w of the large rectangle. Input is terminated by h=w=0. Otherwise, 1<=h,w<=11. 
 

Output
For each test case, 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.
 

Sample Input
   
   
1 2 1 3 1 4 2 2 2 3 2 4 2 11 4 11 0 0
 

Sample Output
   
   
1 0 1 2 3 5 144 51205
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值