Corn fields(玉米田)状压dp入门第一题 洛谷P1879 poj3254

题目描述

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 be planted. Canny FJ knows that the cows dislike eating close to each other, so when choosing which squares to plant, he avoids choosing squares that are adjacent; no two chosen squares share an edge. He has not yet made the final choice as to which squares to plant.

Being a very open-minded man, Farmer John wants to consider all possible options for how to choose the squares for planting. He is so open-minded that he considers choosing no squares as a valid option! Please help Farmer John determine the number of ways he can choose the squares to plant.

农场主John新买了一块长方形的新牧场,这块牧场被划分成M行N列(1 ≤ M ≤ 12; 1 ≤ N ≤ 12),每一格都是一块正方形的土地。John打算在牧场上的某几格里种上美味的草,供他的奶牛们享用。

遗憾的是,有些土地相当贫瘠,不能用来种草。并且,奶牛们喜欢独占一块草地的感觉,于是John不会选择两块相邻的土地,也就是说,没有哪两块草地有公共边。

John想知道,如果不考虑草地的总块数,那么,一共有多少种种植方案可供他选择?(当然,把新牧场完全荒废也是一种方案)

输入输出格式

输入格式:

第一行:两个整数M和N,用空格隔开。

第2到第M+1行:每行包含N个用空格隔开的整数,描述了每块土地的状态。第i+1行描述了第i行的土地,所有整数均为0或1,是1的话,表示这块土地足够肥沃,0则表示这块土地不适合种草。

输出格式:

一个整数,即牧场分配总方案数除以100,000,000的余数。

输入输出样例

输入样例#1: 
2 3
1 1 1
0 1 0
输出样例#1: 
9
就不多做解释了
代码里有
#include<bits/stdc++.h>
using namespace std;
const int mod = 100000000;
int n, m;
int a[13][13];
int F[13];
//第[i]行的土地状态
int f[13][1 << 12 + 5];     
//f[i][j]前[i]行的状态为j时的合法方案数  注意是前i行不是第i行
bool g[1 << 12 + 5];
//g[i]记录i这个状态是否合法
/*
在样例中一共有2行3列
m = 2, n = 3;
1 1 1
0 1 0
那么第[1]行状态为F[1] = 111
    第[2]行状态为F[2] = 010

    1 001    2 010    3 011    4 100
    5 101    6 110    7 111   
 
    发现当i < (1 << n)时,
    i的二进制数位数 = 列数
    也就是每行的数的个数

    所以将g从0枚举到(1 << n)算出所有可能存在的状态
*/
int main() {
    scanf("%d %d", &m, &n); 
    for (int i = 1; i <= m; i++) {
        for (int j = 1; j <= n; j++) {
            scanf("%d", &a[i][j]);
            F[i] = (F[i] << 1) + a[i][j];
            //预处理F[i] 
        }
    }
    for (int i = 0; i < (1 << n); i++) 
        g[i] = (!(i & (i << 1))) && (!(i & (i >> 1)));
        //判断状态合不合法,结果只有0和非0 
    f[0][0] = 1;
    for (int i = 1; i <= m; i++) {
    //枚举每行
        for (int j = 0; j < (1 << n); j++) {
        //枚举这行每个可能的状态
            if (g[j] && ((j & F[i]) == j)) {
            //这行的状态没有并排的1
            //且状态j与F[i]相同
            //使j与F[i]相同保证是在肥沃土地上种草
                for (int k = 0; k < (1 << n); k++) {
                //枚举上一行的状态
                    if ((k & j) == 0) {
                    //与该行状态取&为0说明上一行与这一行不存在任意一块草地有公共边
                        f[i][j] = (f[i][j] + f[i - 1][k]) % mod;
                    }
                }
            }
        }
    }
    int ans = 0;
    for (int i = 0; i < (1 << n); i++) {
        ans = (ans + f[m][i]) % mod;
        //最后将前m行所有满足条件的方案数累加
    }
    printf("%d\n", ans);
    return 0;
}

代码转载自洛谷dalao

链接:https://www.luogu.org/space/show?uid=74596

如果有看不懂的同学,我安利一个同学的博客园

https://www.cnblogs.com/tfyzs/p/11200174.html

也很详细

希望有用!!!!

点赞推荐哟!

三玖天下第一!



猜你喜欢

转载自www.cnblogs.com/fakerOrz/p/11199767.html
今日推荐