poj2411 Mondriaan's Dream

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 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

Source

Ulm Local 2000

这题依旧是有个地方错了然后悲剧了n久现在才过,郁闷……
其实一直很想做这题,好几次都想不出怎么做扔一边去了,现在终于搞出来了……呕耶。
好了,进入正题,用1*2的多米诺骨牌填充n*m的位置,n和m都小于等于11,所以很显然状态压缩dp。
先预处理每一行可能填充的情况,在这里横的用1表示,竖的用0表示,那么预处理是要筛出相邻两个1的情况(因为让1*2的骨牌横躺着),之后枚举每一行的所有可行情况,如果相邻两行的0对上了,那么可以用一个竖的填充,那么这样也让该状态的0变成1,在这里0表示没有填充,1表示填充了。这样就可以过了。
代码
#include <stdio.h>
#include <string.h>

long long dp[15][1<<12];
int av[1<<12];//available -_-|||
int vis[15];

int main()
{
    int i,j,n,up,m,t,l,k;
    while(1)
    {
        scanf("%d%d",&n,&m);
        if (n==0 && m==0) break;
        up=0;
        for (i=0;i<(1<<m);i++)
        {
            memset(vis,0,sizeof(vis));
            for (j=1;j<m;j++)
            {
                if (((i & (1<<j))!=0) && ((i & (1<<(j-1)))!=0) && vis[j-1]==0)
                {
                    vis[j]=vis[j-1]=1;
                }
            }
            for (j=0;j<m;j++)
            {
                if ((i & (1<<j))!=0 && vis[j]==0) break;
            }
            if (j==m) av[up++]=i;
        }
        memset(dp,0,sizeof(dp));
        for (i=0;i<up;i++)
        {
            dp[0][av[i]]=1;
        }
        for (i=1;i<n;i++)
        {
            for (j=0;j<(1<<m);j++)
            {
                for (k=0;k<up;k++)
                {
                    t=0;
                    for (l=m-1;l>=0;l--)
                    {
                        if (((1<<l) & j)==((1<<l) & av[k])) t=(t<<1)+1;
                        else if (((1<<l) & j)!=0 && ((1<<l) & av[k])==0) t=(t<<1);
                        else break;

                    }
                    if (l==-1) dp[i][t]+=dp[i-1][j];
                }
            }
        }
        printf("%lld\n",dp[n-1][(1<<m)-1]);
    }
    return 0;
}



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值