POJ 3254 Corn Fields 矬到荼蘼的状压DP

dp[ i ][ j ] += dp[i+1][ k ] (j,k两种状态均合法)。

留着写XXX论文。

#include <iostream>
#include <algorithm>
#include <cstdlib>
#include <cstdio>
#include <cstring>
#include <queue>
#include <cmath>
#include <stack>

#pragma comment(linker, "/STACK:1024000000");
#define LL long long int
#define ULL unsigned long long int
#define _LL __int64
#define _INF 0x3f3f3f3f
#define INF 4000000
#define Mod 100000000

using namespace std;

int mark[14][14];

ULL dp[13][(1<<12)];

int sta;

bool Match(int s1,int s2,int m)
{
    int i,f;

    for(i = 1,f = 1;i <= m; ++i)
    {
        if((s1&f) && (s2&f))
            return false;
        f <<= 1;
    }

    return true;
}

bool Is_Valid(int n,int m,int sta)
{
    int f,i;

    for(i = 1,f = 1;i <= m; ++i)
    {
        if(sta&f)
        {
            if(mark[n][i] == 0)
                return false;

            if(i > 1)
            {
                if(sta&(f>>1))
                    return false;
            }
        }

        f <<= 1;
    }
    return true;
}

int main()
{
    int n,m;
    ULL ans = 0;

    scanf("%d %d",&n,&m);

    int i,j,k;

    for(i = 1;i <= n; ++i)
    {
        for(j = 1;j <= m; ++j)
        {
            scanf("%d",&mark[i][j]);
        }
    }

    int Max = (1<<m)-1;

    for(j = 0;j <= Max; ++j)
    {
        if(Is_Valid(n,m,j) == false)
            dp[n][j] = 0;
        else
            dp[n][j] = 1;
    }

    for(i = n-1;i >= 1; --i)
    {
        for(j = 0;j <= Max; ++j)
        {
            dp[i][j] = 0;

            if(Is_Valid(i,m,j))
            {
                for(k = 0;k <= Max; ++k)
                {
                    if(dp[i+1][k] != 0 && Match(j,k,m))
                    {
                        dp[i][j] += dp[i+1][k];
                        dp[i][j] %= Mod;
                    }
                }
            }
        }
    }

    for(ans = 0,i = 0;i <= Max; ++i)
    {
        ans += dp[1][i];
        ans %= Mod;
    }

    printf("%llu\n",ans);

    return 0;
}


你可能感兴趣的:(POJ 3254 Corn Fields 矬到荼蘼的状压DP)