POJ.2411.Mondriaan's Dream(轮廓线DP)

一道好水的题...还是写篇博客吧...(我以为轮廓线DP入门要做两三个题,结果。。)

题目链接

题意:求用\(1*2\)的矩形完全覆盖\(n*m\)的棋盘的方案数。
轮廓线DP入门。
另外可以DFS预处理哪些状态能转移到哪些状态,就不用每次\(2^m\)枚举了。反正复杂度\(O(nm2^m)\)

一个代码看了半小时还是十分感觉它不对,怀疑自己智商ing=-=。

//388K  16MS
#include <cstdio>
#include <cstring>
#include <algorithm>
typedef long long LL;
const int N=11;

LL f[2][(1<<N)+1];

int main()
{
    int n,m;
    while(scanf("%d%d",&n,&m),n&&m)
    {
        if(n*m&1) {puts("0"); continue;}
        if(n<m) std::swap(n,m);
        int lim=(1<<m)-1,p=1;
        memset(f[p],0,sizeof f[p]);
        f[p][lim]=1;
        for(int i=0; i<n; ++i)
            for(int j=0; j<m; ++j)
            {
                p^=1; memset(f[p],0,sizeof f[p]);
                for(int s=0; s<=lim; ++s)
                {
                    f[p][s^(1<<j)]+=f[p^1][s];
                    if(j && s>>j&1 && !((s>>j-1)&1)) f[p][s|(1<<j-1)]+=f[p^1][s];
                }
            }
        printf("%lld\n",f[p][lim]);
    }
    return 0;
}

猜你喜欢

转载自www.cnblogs.com/SovietPower/p/10211604.html