Mondriaan's Dream POJ

来源:互联网 发布:好帮手软件简介 编辑:程序博客网 时间:2024/06/08 14:09

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

这道题还是挺可以的,有了一点点插头dp的意思了(不过的确插头dp快的好多),不过还是仍然用了状态压缩dp来做了,其实初见感觉很难下手因为你会发现如果还是用一个数字来表示一行的状态,后来受大神点播,还是把代码码出来了…
代码

#include<cstdio>#include<cstring>#include<algorithm>#include <iostream>#include<cmath>#include <string>using namespace std;int n,m;long long  ans[12][12];//记忆化记录答案long long  dp[12][1<<12];bool firstLine(int state)//对于第一行的合法判定{    int i=1;    while(i<=m)    {        if(state&(1<<(i-1)))        {            if(i==m||!(state&(1<<i)))                return false;            i+=2;        }        else i++;    }    return true;}bool match(int last,int now)//行与行之间的合法判定{     int i=1;     while(i<=m)     {         if(last&(1<<(i-1)))         {             if(!(now&(1<<(i-1))))                i++;             else             {                if(i==m||!(last&(1<<i))||!(now&(1<<i)))                    return false;                else i+=2;             }         }         else         {            if(!(now&(1<<(i-1))))                return false;            else                i++;         }     }     return true;}int main(){    memset(ans,-1,sizeof(ans));    for(;;)    {        scanf("%d%d",&n,&m);        if(!n&&!m)break;        if(n<m)//默认都是n>m;        {            int temp=m;            m=n;            n=temp;        }        if(ans[n][m]!=-1)//如果答案有记录,则直接输出        {            printf("%lld\n",ans[n][m]);            continue;        }        memset(dp,0,sizeof(dp));        for(int i=0;i<(1<<m);i++)            if(firstLine(i))                dp[1][i]=1;//对于第一行的初始化,也是dp的起点        for(int i=2;i<=n;i++)        {            for(int j=0;j<(1<<m);j++)            {                for(int k=0;k<(1<<m);k++)                {                    f(!(dp[i-1][k]))//这行if语句是出于效率原因加的,的确加了以后直接从1704ms到了504ms,我是出于测试性的原因加了,我觉得效率会有所提高,但是没想这么大,可见合法的行之间关系不多,                        continue;                    if(match(k,j))//前一行和后一行的合法判定                        dp[i][j]+=dp[i-1][k];                }            }        }        for(int i=m;i<=n;i++)            ans[n][m]=dp[i][(1<<m)-1];//一路过的其实都顺便计算了,保存即可        printf("%lld\n",ans[n][m]);    }    return 0;}

下次更新插头dp的做法,目测发现插头当前非常快

原创粉丝点击