poj 2663 压缩DP

#include<stdio.h>
#include<string.h>
#include<math.h>
#include<string>
#include<iostream>
#include<algorithm>

using namespace std;
long long dp[33][1<<4];
int map[1<<4][1<<4];
int n,m;
void dfs(int x,int y,int dep){
    if(dep==m){
        map[x][y]=1;
        return;
    }
    int t1=(x>>dep)&1;
    if(dep==m-1){
        if(t1) dfs(x,y,dep+1);
        else dfs(x,y+(1<<dep),dep+1);
        return ;
    }
    int t2=(x>>(dep+1))&1;
    if((!t1)&&(!t2)){
        dfs(x,y,dep+2);
        dfs(x,y+(1<<dep),dep+1);
    }
    if((!t1) && t2){
         dfs(x,y+(1<<dep),dep+2);
    }
    if(t1){
        dfs(x,y,dep+1);
    }
}
long long deal(){
    if(m==0) return 1;
    if(n*m %2) return 0;
    memset(map,0,sizeof(map));
    for(int x=0;x<(1<<m);x++)
        dfs(x,0,0);

    memset(dp,0,sizeof(dp));
    dp[0][0]=1;

    for(int i=1;i<=n;i++){
        for(int x=0;x<(1<<m);x++) if(dp[i-1][x])
           for(int y=0;y<(1<<m);y++){
              if(map[x][y]) dp[i][y]+=dp[i-1][x];
           }
    }
    return dp[n][0];
}
int main()
{

//     freopen("in.in","r",stdin);
     while(~scanf("%d",&n)&&(n!=-1)){
        m=min(n,3);
        n=max(n,3);
        cout<< deal() << endl;
     }
     return 0;
}

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值