poj 3254 Corn Fields 動態規劃

poj 3254 Corn Fields 動態規劃

Corn Fields
Time Limit: 2000MS   Memory Limit: 65536K
Total Submissions: 8491   Accepted: 4527

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的草地裏面每個單位面積上分爲老的和嫩的草,現在fj希望在這塊草地上放老牛,其中老牛隻吃嫩草(嘻嘻)並且爲了不讓老牛相互爭奪嫩草資源,所以都不能邊相連(不能共有一條邊),求fj總共有多少總方案可以讓他該死的老牛遲到嫩草?(不讓老牛遲到嫩草也算一種,畢竟沒人規定老牛就一定要遲到嫩草,不是麼)。

這是一個入門級的狀壓dp(動態規劃):先進行情況預處理,然後對輸入狀態壓縮,再然後進行dp[line][i] += dp[line-1][j] (0 <= i ,j < 狀態總數)最後計算總數!

代碼:

#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
#define fi(i,k) for(int i = 0; i < k; i ++)
const int STATE_CASE = 390;
const int MOD = 100000000;
int state[STATE_CASE], dp[13][STATE_CASE],infertile[13], n, m, cnt;
// 預處理
void init()
{
    cnt = 0;
    int total = 1 << n;
    for(int i = 0; i < total; i ++)
        if(!(i & ( i << 1 )))
            state[cnt ++] = i;
}

int main()
{
    while(~scanf("%d%d", &m, &n))
    {
        init();
//狀態壓縮
        fi(line, m)
        {
            infertile[line] = 0;
            fi(row,n)
            {
                int tmp;
                scanf("%d", &tmp);
                infertile[line] += (!tmp) << row;
            }
        }
        memset(dp, 0, sizeof(dp));
        fi(i,cnt) dp[0][i] = (state[i] & infertile[0]) ? 0 : 1; 
        for(int line = 1; line < m; line ++) fi(i,cnt)
        {
            if(state[i] & infertile[line]) continue;
            fi(j,cnt)
            {
                if(state[i] & state[j]) continue;
                dp[line][i] += dp[line-1][j];//狀態轉移方程
                if(dp[line][i] > MOD) dp[line][i] %= MOD;
            }
        }
        int ans = 0;
        fi(i,cnt) ans += dp[m-1][i];//計算結果!
        printf("%d\n", ans % MOD);
    }
}


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章