sgu223 分类: sgu 2015-06-12 10:21 11人阅读 评论(0) 收藏


状态压缩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;
}

版权声明:本文为博主原创文章,未经博主允许不得转载。

posted @ 2015-06-12 10:21  <Dash>  阅读(122)  评论(0编辑  收藏  举报