[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 f[13][1 << 13], pos[13][1 << 13], s[13][1 << 13];
 7
 8 inline int read()
 9 {
10     int x = 0, f = 1;
11     char ch = getchar();
12     for(; !isdigit(ch); ch = getchar()) if(ch == ‘-‘) f = -1;
13     for(; isdigit(ch); ch = getchar()) x = (x << 1) + (x << 3) + ch - ‘0‘;
14     return x * f;
15 }
16
17 inline void dfs(int k, int i, int num)
18 {
19     if(i > pos[k][0])
20     {
21         s[k][++s[k][0]] = num;
22         return;
23     }
24     dfs(k, i + 1, num);
25     if((pos[k][i] ^ (pos[k][i - 1] + 1)) || (i == 1))
26         dfs(k, i + 1, num + (1 << pos[k][i] - 1));
27     else if((i ^ 1) && (pos[k][i] == pos[k][i - 1] + 1))
28         if(((num >> pos[k][i - 1] - 1) & 1) ^ 1)
29             dfs(k, i + 1, num + (1 << pos[k][i] - 1));
30 }
31
32 int main()
33 {
34     int i, j, k, x;
35     n = read();
36     m = read();
37     for(i = 1; i <= n; i++)
38         for(j = 1; j <= m; j++)
39         {
40             x = read();
41             if(x) pos[i][++pos[i][0]] = j;
42         }
43     for(i = 1; i <= n; i++) dfs(i, 1, 0);
44     for(i = 1; i <= s[1][0]; i++) f[1][i] = 1;
45     for(i = 2; i <= n; i++)
46         for(j = 1; j <= s[i][0]; j++)
47             for(k = 1; k <= s[i - 1][0]; k++)
48                 if(!(s[i][j] & s[i - 1][k]))
49                 {
50                     f[i][j] += f[i - 1][k];
51                     if(f[i][j] >= mod) f[i][j] -= mod;
52                 }
53     for(i = 1; i <= s[n][0]; i++)
54     {
55         ans += f[n][i];
56         if(ans >= mod) ans -= mod;
57     }
58     printf("%d\n", ans);
59     return 0;
60 }

时间: 2024-10-07 02:47:55

[luoguP1879] [USACO06NOV]玉米田Corn Fields(DP)的相关文章

洛谷 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

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;

洛谷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

【状压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

题解——[USACO06NOV]玉米田Corn Fields 状压DP

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

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

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

[USACO06NOV]玉米田Corn Fields (状压$dp$)

题目链接 Solution 状压 \(dp\) . \(f[i][j][k]\) 代表前 \(i\) 列中 , 已经安置 \(j\) 块草皮,且最后一位状态为 \(k\) . 同时多记录一个每一列中的不能放的位置 \(w[i]\). 然后就可以很轻松的转移了... 转移方程看代码. Code #include<bits/stdc++.h> #define ll long long using namespace std; ll f[13][145][10000],n,K; ll js[1000

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

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", &