HDU.1693.Eat the Trees(插头DP)

题目链接

http://www.cnblogs.com/LadyLex/p/7326874.html

插头DP。对于本题我们只需要记录之前\(m\)个格子的\(m+1\)个插头是否存在。
转移时根据左边、上边是否有插头讨论。用位运算可以写的很方便。

因为想对DP数组压压维,我也觉得写的好不直观=-=。反正就是从上一个格子转移,解决完一行将状态左移一位转给下一行。要看直观的代码可以看上面链接里的代码...

最简单的插头DP...反正我也是做过插头DP的人了233

//0MS   1260K
#include <cstdio>
#include <cctype>
#include <cstring>
#include <algorithm>
#define gc() getchar()
typedef long long LL;
const int N=12;

LL f[2][(1<<N)+2];//m+1
bool mp[N][N];

inline int read()
{
    int now=0;register char c=gc();
    for(;!isdigit(c);c=gc());
    for(;isdigit(c);now=now*10+c-'0',c=gc());
    return now;
}
inline void Work(int x,int y,int lim,LL *f,LL *g)
{
    if(mp[x][y])
        for(int s=0,p1=1<<y-1,p2=1<<y; s<lim; ++s)
        {
            f[s^p1^p2]+=g[s];
            if((s>>y-1&1)!=(s>>y&1)) f[s]+=g[s];
        }
    else
        for(int s=0,p1=1<<y-1,p2=1<<y; s<lim; ++s)
            if(!(s&p1) && !(s&p2)) f[s]=g[s];
            else f[s]=0;
}

int main()
{
    for(int T=1,T2=read(); T<=T2; ++T)
    {
        int n=read(),m=read(),l1=1<<m,l2=1<<m+1;
        for(int i=1; i<=n; ++i)
            for(int j=1; j<=m; ++j) mp[i][j]=read();
        int p=0; memset(f[p],0,sizeof f[p]);
        f[p][0]=1;
        for(int i=1; i<=n; ++i)
        {
            for(int j=1; j<=m; ++j)
            {
                p^=1, memset(f[p],0,sizeof f[p]);
                Work(i,j,l2,f[p],f[p^1]);
            }
            if(i!=n)
            {
                p^=1, memset(f[p],0,sizeof f[p]);
                for(int s=0; s<l1; ++s) f[p][s<<1]=f[p^1][s];
            }
        }
        printf("Case %d: There are %lld ways to eat the trees.\n",T,f[p][0]);//f[n][m][0]
    }
    return 0;
}

转载于:https://www.cnblogs.com/SovietPower/p/10212626.html

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值