Corn Fields [state compression dp]

题目: 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.

Topic : Farmers have a matrix grassland for cattle farming. A block of 1 can be raised. A farmer of 0 cannot be raised. Moreover, each cow cannot be adjacent. Now I will give you a grassland and ask you how many farming schemes your farm name has.

input

2 3
1 1 1
0 1 0

output

9

analysis

Each piece of land has only two states: pasture and not pasture. Each pasture has two states of grazing and no grazing. It can be represented by 0 1 and compressed in this state.

There are two restrictions :

  1. Can only graze cattle on pasture
  2. Adjacent pastures cannot simultaneously graze

Now let's see how to deal with these two situations with bit operations

1. If 1 means pasture, 0 means not

Line i: 0 0 1 0 1 0 1

Grazing situation: 1 0 0 1 0 0 1

Bitwise and 1 0 0 0 0 0 0 0 ---> ≠ 0

Conclusion a> If a cow is placed in a place that is not a pasture, the result of & is not 0

Line i 1 0 1 1 0 0 1

Grazing situation 1 0 1 1 0 0 1

Herding << 1 0 1 1 0 0 1 0

Bitwise and 0 0 1 0 0 0 0 ≠ 0

Conclusion b> If cattle are laid in the adjacent pasture, the cattle grazing situation << 1 and the pasture & the value is not 0

#include<cstdio>
#include<algorithm>
#include<cstring>
#include<iostream>
#include<cmath>
using namespace std;
const int mod = 1e8;
int n, m;
int mn[15], dp[15][1<<13], vis[15][1<<13];
//mn[i]存储第 i 行最大的牧场状态,dp[i][k]表示第i行的状态为k时,前i行的最大方案数。,vis[i][j]表示第 i 行的 j 状态是否合法 
int main(){
    scanf("%d%d", &n, &m);
    for(int i=0; i<n; i++){
        int ans = 0;
        for(int j=0; j<m; j++){
            int a; scanf("%d", &a);
            ans = (ans<<1) + a;//ans左移一位,空出的一位记录当前是否有牧场
        }
        mn[i] = ans;
        for(int j=0; j<=mn[i]; j++){//枚举第i行的状态j
            if((j & mn[i]) != j) continue;//在没有牧场的地方放了牛
            if(j & (j<<1)) continue;//在相邻的牧场放了牛
            vis[i][j] = 1;//第i行的j状态合法
        }
    }
    for(int i=0; i<n; i++){//枚举行
        for(int j=0; j<=mn[i]; j++){//枚举状态
            if(!vis[i][j]) continue;//不合法则跳过
            if(i == 0) dp[i][j] = 1;//第一行任何合法的方案都可以
            else{
                for(int k=0; k<=mn[i-1]; k++){//枚举上一行的状态
                    if(!vis[i-1][k]) continue;//不合法则跳过
                    if((j & k) != 0) continue;//在竖直方向上的相邻位置放了牛
                    dp[i][j] = (dp[i][j]%mod + dp[i-1][k]%mod)%mod;//当前总共的方案数
                }
            }
        }
    }
    int ans = 0;
    for(int i=0; i<=mn[n-1]; i++){//枚举第n行上一行的状态
        if(!vis[n-1][i]) continue;//不合法则跳过
        ans = (ans%mod +dp[n-1][i]%mod)%mod;//求最终的方案和
    }
    printf("%d\n", ans);
    return 0;
}

Guess you like

Origin www.cnblogs.com/hzoi-poozhai/p/12694182.html