天天看點

POJ 3254 Corn Fields(狀壓DP)

Corn Fields

Time Limit: 2000MS Memory Limit: 65536K

Total Submissions: 14901 Accepted: 7806

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.

Source

USACO 2006 November Gold

題意: 農夫有一塊地,被劃分為m行n列大小相等的格子,其中一些格子是可以放牧的(用1标記),其他格子則不能放牛(用0标記),并且要求不可以使相鄰格子都有牛。求該農夫有多少種放牧方案可以選擇(注意:任何格子都不放也是一種選擇,不要忘記考慮!)
題解:以 dp[i][state(j)] 來表示對于 前i行 , 第i行 采用 第j種狀态 時可以得到的 可行方案總數。狀态轉移方程dp[i][state(j)]=dp[i-1][state(k1)]+dp[i-1][state(k2)]+……+dp[i-1][state(kn)] (kn即為上一行可行狀态的編号,上一行共有n種可行狀态)
代碼:
#include <iostream>
#include <cstring>
#include <cstdio>
#include <iostream>
#include <cstring>
#include <cstdio>
#include <algorithm>
#include <vector>

using namespace std;
const int mod = ;
const int maxn = ;
int m, n, top;//top表示每行最多的狀态數
int sa[maxn];//sa存放每行所有的可行狀态
int dp[][maxn];
int c[maxn];//c[i]表示的是第i行整行的情況
/**
判斷第i為是否為1 (這個位是從右數起,從0開始。) if (x&(1<<i) {} 或
者 if ((x>>i)&1) {} 。 前面兩種寫法都是可以的,這裡要注意不要忘記了
位運算兩側的括号。因為位運算的優先級很低。
設定第i位為1 x |= 1<<i;
設定第i位為0 x &= ~(1<<i);
切換第i位 x ^= 1<<i;
*/
// x & (x<<1)來判斷一個數相鄰兩位是不是同時為1,假如同時為 1 則傳回一個值,否則傳回 0
//x & y 的布爾值來判斷相同為是不是同時為1。
int main()
{
    while (cin >> m >> n)
    {
        top = ;
        int tot =  << n;
        for (int i = ;i < tot;i++)
        {
            if (!(i&(i << )))
                sa[top++] = i;
        }
        memset(dp, , sizeof(dp));
        for (int i = ;i <= m;i++)
        {
            c[i] = ;
            int num;
            for (int j = ;j <= n;j++)
            {
                cin >> num;
                if (!num)
                {
                    c[i] += ( << (n - j));
                }
            }
        }
        for (int i = ;i <= top;i++)
        {
            if (!(sa[i] & c[]))
                dp[][i] = ;
        }
        for (int i = ;i <= m;i++)
        {
            for (int j = ;j <= top;j++) //上一行狀态
            {
                if (!(sa[j] & c[i]))
                for (int k = ;k <= top;k++) //這一行狀态
                {
                    if ((sa[k] & c[i - ])==&& (sa[j] & sa[k])==)
                    dp[i][j] = (dp[i][j] + dp[i - ][k]) % mod;
                }
            }
        }
        int res = ;//累加最後一行所有可能狀态的值,即得最終結果
        for (int i = ;i <= top;i++)
        {
            res = (res + dp[m][i]) % mod;
        }
        cout << res << endl;
    }
    return ;
}
           

繼續閱讀