P1879 [USACO06NOV]玉米田Corn Fields

P1879 [USACO06NOV]玉米田Corn Fields

这道题跟互不侵犯差不多,但是数据范围更大了

对于状态压缩dp,提前枚举出转移是一种很好的优化

然后对于转移也需要仔仔细细的分析,如同期望dp

#include<cstdio>
#include<algorithm>
#include<iostream>
const int mod=1e8;
int map[20];
long long f[2][1<<12];
int b[400],tot;
int main()
{
    int n,m;
    scanf("%d%d",&n,&m);
    int a;
    for(int i=1;i<=n;i++)
        for(int j=1;j<=m;j++)
        {
            scanf("%d",&a);
            a^=1;
            map[i]=(map[i]<<1)+a;
        }
    for(int i=0;i<(1<<m);i++)
        if(!(i&(i<<1))&&!(i&(i>>1)))    b[++tot]=i;
    for(int i=1;i<=tot;i++)
        if(!(map[1]&b[i]))
            f[1][b[i]]=1;
    for(int i=2;i<=n;i++)
        for(int j=1;j<=tot;j++)
        {
            f[i&1][b[j]]=0;
            if(!(map[i]&b[j]))
                for(int k=1;k<=tot;k++)
                    if(!(b[j]&b[k])&&!(map[i-1]&b[k]))
                        f[i&1][b[j]]=(f[i&1][b[j]]+f[(i&1)^1][b[k]])%mod;
        }
    long long ans=0;
    for(int i=1;i<=tot;i++)
        ans=(ans+f[n&1][b[i]])%mod;
    printf("%lld",ans);
}

原文地址:https://www.cnblogs.com/Lance1ot/p/9379677.html

时间: 2024-08-07 19:09:01

P1879 [USACO06NOV]玉米田Corn Fields的相关文章

洛谷 P1879 [USACO06NOV]玉米田Corn Fields

P1879 [USACO06NOV]玉米田Corn Fields 题目描述 Farmer John has purchased a lush new rectangular pasture composed of M by N (1 ≤ M ≤ 12; 1 ≤ N ≤ 12) square parcels. He wants to grow some yummy corn for the cows on a number of squares. Regrettably, some of the

洛谷P1879 [USACO06NOV]玉米田Corn Fields【状压DP】题解+AC代码

题目描述 Farmer John has purchased a lush new rectangular pasture composed of M by N (1 ≤ M ≤ 12; 1 ≤ N ≤ 12) square parcels. He wants to grow some yummy corn for the cows on a number of squares. Regrettably, some of the squares are infertile and can't b

【luogu P1879 [USACO06NOV]玉米田Corn Fields】 题解

题目链接:https://www.luogu.org/problemnew/show/P1879 状压DP. 设dp[i][j]表示第i行,状态为j的方案数 初始dp[0][0] = 1 这样一共12行12列,最多1<<12. 这样转移时,只要满足上下没有两个1,这两行分别满足没有相邻1. 加法原理转移. $ j&k==0 $ $ dp[i][j] += dp[i-1][k] $ #include <cstdio> #include <cstring> #inc

P1879 [USACO06NOV]玉米田Corn Fields题解(注释版)

题目描述 Farmer John has purchased a lush new rectangular pasture composed of M by N (1 ≤ M ≤ 12; 1 ≤ N ≤ 12) square parcels. He wants to grow some yummy corn for the cows on a number of squares. Regrettably, some of the squares are infertile and can't b

P1879 [USACO06NOV]玉米田Corn Fields 状压dp

链接在此! 正解:状压dp(emm……据说插头dp也可以趴但我不管!!!不会!!! 解题报告: ……我真的太菜了……我以为一个小时前要搞完的题目调错误调了一个小时……90分到100我差不多搞了一个小时…… 然后这题还是做过的……就很气,觉得确实是要搞下博客没事儿复习下不然做过的题目还花俩小时我真的哭死…… 先放上错误的90分代码讲一下错哪儿了(因为……其实100并不难是可以想到的……没有太大讲的意义,主要我太菜了所以才会搞这么久TT 点我♂看♂沙雕灵巧在线WA题 然后错误的点是最后一个点RE,开

【p1879】[USACO06NOV]玉米田Corn Fields

Description 农场主John新买了一块长方形的新牧场,这块牧场被划分成M行N列(1 ≤ M ≤ 12; 1 ≤ N ≤ 12),每一格都是一块正方形的土地.John打算在牧场上的某几格里种上美味的草,供他的奶牛们享用. 遗憾的是,有些土地相当贫瘠,不能用来种草.并且,奶牛们喜欢独占一块草地的感觉,于是John不会选择两块相邻的土地,也就是说,没有哪两块草地有公共边. John想知道,如果不考虑草地的总块数,那么,一共有多少种种植方案可供他选择?(当然,把新牧场完全荒废也是一种方案) I

【状压DP水题】[USACO06NOV]玉米田Corn Fields

题目描述 Farmer John has purchased a lush new rectangular pasture composed of M by N (1 ≤ M ≤ 12; 1 ≤ N ≤ 12) square parcels. He wants to grow some yummy corn for the cows on a number of squares. Regrettably, some of the squares are infertile and can't b

[luoguP1879] [USACO06NOV]玉米田Corn Fields(DP)

传送门 说要统计方案,感觉就是个 Σ 而矩阵中只有 01 ,可以用二进制表示 这样,预处理出每一个每一行所有可能的状态 s 然后初始化第一行所有状态的方案数为 1 f[i][j] = Σf[i - 1][k] (k 和 j 不冲突,j 为第 i 行所有方案,k 为第 i - 1 行所有方案) ——代码 1 #include <cstdio> 2 #include <iostream> 3 #define mod 100000000 4 5 int n, m, ans; 6 int

luogu1879 [USACO06NOV]玉米田Corn Fields

学习位运算 #include <iostream> #include <cstdio> using namespace std; int n, m, dp[15][4105], a[15], uu, ans; const int mod=100000000; int main(){ cin>>m>>n; for(int i=1; i<=m; i++) for(int j=1; j<=n; j++){ scanf("%d", &