poj3254--Corn Fields(状压dp)

Corn Fields
Time Limit: 2000MS   Memory Limit: 65536K
Total Submissions: 8512   Accepted: 4540

Description

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=9.


题目大意:n*m的矩阵,其中1代表可以放牧,0代表不能放牧,要求放牧的两块地方不能相邻,问有多少种方法

对每行的状态计算,二进制数每一位对应一列的地放不放牧,0代表不放,1代表放牧。每一行可以出现的状态提前处理出来,dp[i][j]代表第i行在状态j的时候的方法。

因为要求不能相邻,所以相邻行之间的状态不能出现同一列的情况,按二进制判断,即dp[i][j] & dp[i+1][k] == 0


#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std ;
#define MOD 100000000
#define LL __int64
int Map[15][15] , num[15] , state[15][5000] ;
LL dp[15][5000] ;
void init(int n,int m)
{
    memset(num,0,sizeof(num)) ;
    int x , i , j , k ;
    for(i = 0 ; i < n ; i++)
    {
        x = 1 << m ;
        for(j = 0 ; j < x ; j++)
        {
            if( ( j & j<<1 ) > 0 )
                continue ;
            for(k = 0 ; k < m ; k++)
            {
                if( !Map[i][k] && ( 1<<k & j ) > 0 )
                    break ;
            }
            if( k == m )
                state[i][ num[i]++ ] = j ;
        }
    }
    for(i = 0 ; i < num[0] ; i++)
        dp[0][i] = 1 ;
}
int main()
{
    int n , m ;
    int i , j , k ;
    LL ans ;
    scanf("%d %d", &n, &m) ;
    for(i = 0 ; i < n ; i++)
        for(j = 0 ; j < m ; j++)
            scanf("%d", &Map[i][j]) ;
    init(n,m) ;
    for(i = 1 ; i < n ; i++)
        for(j = 0 ; j < num[i] ; j++)
            for(k = 0 ; k < num[i-1] ; k++)
                if( (state[i][j] & state[i-1][k]) == 0 )
                    dp[i][j] = ( dp[i][j] + dp[i-1][k] ) % MOD ;
    for(i = 0 , ans = 0 ; i < num[n-1] ; i++)
        ans = ( ans + dp[n-1][i] ) % MOD ;
    printf("%I64d\n", ans) ;
    return 0;
}


你可能感兴趣的:(poj3254--Corn Fields(状压dp))