• poj 3254 Corn Fields 状压dp入门


    题目链接

    题意

    (M imes N)(0,1)格子上放东西,只有标记为(1)的格子可以放东西,且相邻的格子不能同时放东西。问有多少种放法。

    思路

    参考:swallowblank.

    (dp[i][state])表示放到第(i)行状态为(state)时的情况总数。显然有

    [dp[i][state]=sum dp[i-1][state'] ]

    其中,(state)与第(i)行的地图相容,(state')与第(i-1)行的地图相容,且(state)(state')相容。

    至于每一行中合法的状态,可以通过预处理得到:如果(state&(state<<1)==0),则不存在相邻的(1),则合法。

    Code

    #include <stdio.h>
    #define F(i, a, b) for (int i = (a); i < (b); ++i)
    #define F2(i, a, b) for (int i = (a); i <= (b); ++i)
    #define dF(i, a, b) for (int i = (a); i > (b); --i)
    #define dF2(i, a, b) for (int i = (a); i >= (b); --i)
    #define maxn 13
    #define maxs 5010
    #define mod 100000000
    using namespace std;
    typedef long long LL;
    int cur[maxn], state[maxs], dp[maxn][maxs];
    int main() {
        int m, n, x, tot=0;
        scanf("%d%d", &m, &n);
        F(i, 0, m) {
            F(j, 0, n) {
                scanf("%d", &x);
                (cur[i] <<= 1) |= x;
            }
        }
        F(i, 0, 1<<n) {
            if (!(i&(i<<1))) {
                if (!(i&~cur[0])) dp[0][i] = 1;
                state[tot++] = i;
            }
        }
        F(i, 1, m) {
            F(j, 0, tot) {
                if (!(state[j]&~cur[i])) {
                    F(k, 0, tot) {
                        if (!(state[k]&~cur[i-1]) && !(state[k]&state[j])) {
                            (dp[i][state[j]] += dp[i-1][state[k]]) %= mod;
                        }
                    }
                }
            }
        }
        int ans = 0;
        F(i, 0, tot) (ans += dp[m-1][state[i]]) %= mod;
        printf("%d
    ", ans);
        return 0;
    }
    
    
  • 相关阅读:
    Spring shiro学习(二)
    Spring shiro学习(一)
    Redis/zookeeper/ActiveMQ在Mac下的安装配置
    Mac下利用brew安装Intellij IDEA
    MySQL服务相关
    Ruby变量常量
    Web性能测试工具:Siege安装&使用简介
    无限级分类功能实践
    Ubuntu Bash and Dash
    安装好的虚拟机,外部通过ssh工具连接,报connection failed
  • 原文地址:https://www.cnblogs.com/kkkkahlua/p/8448464.html
Copyright © 2020-2023  润新知