sgu223


状态压缩 Dp ,按行处理。

复杂度: O(nk3n)


#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<iostream>
#include<algorithm>

const int maxn = 15, maxk = 105, sz = 10;
struct Trans
{
    int fr,to,imp;
    Trans(int fr = 0,int to = 0,int imp = 0):fr(fr),to(to),imp(imp){}
};

int n, k;

Trans ts[1<<(sz<<1)]; int tl;
long long f[maxn][maxk][1<<sz], ans;

void DFS(int col,int last,int now,int cnt)
{
    if(col == n)
        ts[++tl] = Trans(last,now,cnt); 
    else
    {
        DFS(col + 1, last, now, cnt);

        if(!col || (!(last&(1<<(col-1))) && !(now&(1<<(col-1)))))
        {
            DFS(col + 1, last|(1<<col), now, cnt);
            DFS(col + 1, last, now|(1<<col), cnt + 1);
        }
    }
}

int main()
{   
#ifndef ONLINE_JUDGE    
    freopen("sgu223.in","r",stdin);
    freopen("sgu223.out","w",stdout);
#endif

    std::cin >> n >> k;

    f[0][0][0] = 1;

    DFS(0,0,0,0);

    for(int i = 1; i <= n; i++)
        for(int j = 0; j <= k; j++)
            for(int p = 1; p <= tl; p++)
                if(j - ts[p].imp >= 0)
                    f[i][j][ts[p].to] += f[i-1][j-ts[p].imp][ts[p].fr];

    for(int i = 0; i < (1<<n); i++)
            ans += f[n][k][i];

    std::cout << ans;       

#ifndef ONLINE_JUDGE    
    fclose(stdin);
    fclose(stdout);
#endif
    return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值