poj 3254 Corn Fields

Corn Fields
Time Limit: 2000MS   Memory Limit: 65536K
Total Submissions: 18381   Accepted: 9678

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.
 
题意:在可种植草的地方种草,并且要求没有两块草坪是相邻的,求所有可能的情况的数量(一块草坪都不种植的情况也包括在内)
思路:状态压缩dp,设前一行的当前状态为prev,后一行的当前状态为cur,若prev和cur状态都合法,并且不冲突(前后没有两块草坪连在一起),则得到prev状态的所有情况数量可以转移给cur状态
AC代码:
#define _CRT_SECURE_NO_DEPRECATE
#include <iostream>
#include<vector>
#include<algorithm>
#include<cstring>
#include<bitset>
#include<set>
#include<map>
using namespace std;
#define N_MAX 14
#define MOD 100000000
#define INF 0x3f3f3f3f
typedef long long ll;
int Map[N_MAX][N_MAX];
int n, m;
bool is_valid(int state,int k) {//第k行状态state
    int i = 0;
    while (i<m) {
        if (Map[k][i] == 0 && (state >> i & 1))return false;
        if (i && (state >> i & 1) && (state >> (i - 1) & 1)) return false;
        i++;
    }
    return true;
}
bool is_compatible(int cur, int prev) {
    int i = 0;
    while (i<m) {
        if ((cur >> i & 1) && (prev >> i & 1))return false;
        i++;
    }
    return true;
}


ll dp[N_MAX][1 << N_MAX];
int main() {
    while (scanf("%d%d", &n, &m) != EOF) {
        memset(dp,0,sizeof(dp));
        memset(Map, 0, sizeof(Map));
        for (int i = 0; i < n; i++)
            for (int j = 0; j < m; j++)
                scanf("%d", &Map[i][j]);
        int allstates = 1 << m;
        for (int i = 0; i < allstates; i++) {
            if (is_valid(i, 0))
                dp[0][i] = 1;
        }
        for (int i = 1; i < n; i++) {
            for (int j = 0; j < allstates; j++) {
                if (!is_valid(j, i))continue;
                for (int k = 0; k < allstates; k++) {//状态j合法并且不和第i-1行的状态k冲突
                    if (dp[i - 1][k] > 0 && is_compatible(j, k)) {
                        dp[i][j] += dp[i - 1][k];
                    }

                }
            }
        }
        ll sum = 0;
        for (int i = 0; i < allstates;i++) {
            sum += dp[n - 1][i];
            sum %= MOD;
        }
        printf("%lld\n",sum);
    }
    return 0;
}

 

 
 

posted on 2018-04-07 20:12  ZefengYao  阅读(121)  评论(0编辑  收藏  举报

导航