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. poj2411 Mondriaan's Dream_第1张图片

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



你可能感兴趣的:(Integer,input,UP,each,output,Numbers)