POJ 3254 Corn Fields (like pressure 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.

Input
Line 1: Two space-separated integers: M and N
Lines 2… M+1: Line i+1 describes row i of the pasture with N space-separated integers indicating whether a square is fertile (1 for fertile, 0 for infertile)
Output
Line 1: One integer: the number of ways that FJ can choose the squares modulo 100,000,000.
Sample Input
2 3
1 1 1
0 1 0
Sample Output
9
Hint
Number the squares as follows:
1 2 3
4

There are four ways to plant only on one squares (1, 2, 3, or 4), three ways to plant on two squares (13, 14, or 34), 1 way to plant on three squares (134), and one way to plant on no squares. 4+3+1+1.



Meaning of the questions: Given an n * m matrix, the matrix has a value of 0 and 1,1 have the coordinates can be placed represent something, something required can not be placed adjacent to, ask how many put the law? n, m <= 12

Outline of Solution: dp [i] [j] denotes the i-th row is placed in a state number j what method, the transfer equation dp [i] [j] = sum (dp [i-1] [k]).
Specifically look at the code and comments:

#include<iostream>
#include<cstdio>
#include<cstring>
using namespace std;
#define mod 100000000
int dp[15][1<<13];///dp[i][j]表示i行状态为j的数量。
int ma[15][15]int state[15];///记录每行的状态
int n,m;
int pd1(int n)///判断是否含有连续的1;
{
    return !(n&(n<<1));
}
int pd2(int a,int b)///判断在相同位置上是否同时有1
{
    return !(a&b);
}
void solve()
{
    for(int i=0;i<(1<<m);i++){///初始化
        if(i==(i&state[0])&&pd1(i)){
            dp[0][i]=1;
            //printf("0 %d %d\n",i,dp[0][i]);
        }
    }
    for(int i=1;i<n;i++){///状态转移
        for(int j=0;j<(1<<m);j++){
            if(j==(j&state[i])&&pd1(j)){
                int sum=0;
                for(int k=0;k<(1<<m);k++){
                    if(pd2(j,k))
                        sum=(sum+dp[i-1][k])%mod;
                }
                dp[i][j]=sum;
                //printf("i=%d j=%d %d\n",i,j,dp[i][j]);
            }
        }
    }
}
int main()
{
    scanf("%d%d",&n,&m);
    for(int i=0;i<n;i++){
        for(int j=0;j<m;j++){
            scanf("%d",&ma[i][j]);
            if(ma[i][j]){
                state[i]|=(1<<j);///将每行的状态用一个二进制数存起来。
            }
        }
    }
    solve();
    int ans=0;
    for(int i=0;i<(1<<m);i++){
        ans=(ans+dp[n-1][i])%mod;
    }
    printf("%d\n",ans);
    return 0;
}

Published 36 original articles · won praise 10 · views 1900

Guess you like

Origin blog.csdn.net/weixin_44003265/article/details/103910827