sgu223

来源:互联网 发布:石家庄软件开发培训 编辑:程序博客网 时间:2024/06/04 18:21

状态压缩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;}
0 0
原创粉丝点击