牛客-Corn Fields

题目传送门

sol:状压和动规,把每一行的m个01压缩成一个int

  • 状压dp
    #include "bits/stdc++.h"
    using namespace std;
    const int MAXN = 15;
    const int MOD = 1e8;
    int n, m;
    bool mp[MAXN][MAXN];
    vector<int> num[MAXN], dp[MAXN];
    void dfs(int i, int j, int k) {
        if (j > m) {
            num[i].push_back(k);
            dp[i].push_back(0);
            return;
        }
        dfs(i, j + 1, k  << 1);
        if ((k & 1) == 0 && mp[i][j + 1] == 1) dfs(i, j + 1, k << 1 | 1);
    }
    int main() {
        scanf("%d%d", &n, &m);
        for (int i = 1; i <= n; i++) {
            for (int j = 1; j <= m; j++) scanf("%d", &mp[i][j]);
            dfs(i, 0, 0);
        }
        for (int j = 0; j < dp[1].size(); j++) dp[1][j] = 1;
        for (int i = 2; i <= n; i++) {
            for (int j = 0; j < dp[i].size(); j++) {
                for (int k = 0; k < dp[i - 1].size(); k++) {
                    if ((num[i][j] & num[i - 1][k]) == 0)
                    {
                        dp[i][j] = (dp[i][j] + dp[i - 1][k]) % MOD;
                    }
                }
            }
        }
        int ans = 0;
        for (int j = 0; j < dp[n].size(); j++) ans = (ans + dp[n][j]) % MOD;
        printf("%d\n", ans);
        return 0;
    }

     

上一篇:plant template


下一篇:JAVA花园布局(抽象工厂模式)