poj 3254 Corn Fields //状态压缩DP



#include <cstdio>
#include <cstring>
const int MAXN = (1 << 12) - 1;
const int MOD = 100000000;
int n, m;
int state[12][MAXN];
int dp[2][MAXN];
void get_state(int id, int val)
{
    //printf("cal=%d\n", val);
    state[id][0] = 0;
    for(int i = 0; i <= val; i++)
    {
        if((i | val) != val) continue; //牛在不长草的地方吃草
        if((i & (i << 1)) || (i & (i >> 1)))  continue;//相邻的情况
        state[id][++state[id][0]] = i;
        //printf("i=%d\n",i);
    }
}
void solve()
{
    //printf("num=%d\n", state[0][0]);
    for(int i = 1; i <= state[0][0]; i++)  dp[0][i] = 1;

    for(int i = 1; i < n; i++)
    {
        int a = i % 2;
        int b = 1 - a;
        for(int j = 1; j <= state[i][0]; j++)
        {
            dp[a][j] = 0;
            for(int k = 1; k <= state[i-1][0]; k++)
            {
                if(state[i][j] & state[i-1][k])  continue;
                dp[a][j] += dp[b][k];
            }
        }
    }

    int ans = 0;
    int a = (n - 1) % 2;
    for(int i = 1; i <= state[n-1][0]; i++)
    {
        ans += dp[a][i];
        ans %= MOD;
    }
    printf("%d\n", ans);
}
int main()
{
    while(scanf("%d%d", &n, &m) != EOF)
    {
        int x;
        for(int i = 0; i < n; i++)
        {
            int val = 0;
            for(int j = 0; j < m; j++)
            {
                val <<= 1;
                scanf("%d", &x);
                val += x;
            }
            get_state(i, val);
        }
        solve();
    }
    return 0;
}




你可能感兴趣的:(poj 3254 Corn Fields //状态压缩DP)