POJ 3254 Corn Fields(状态压缩)

Farmer John在购买了一块由M行N列组成的牧场后,面临如何在耕地上种植玉米的问题。他需要避免选择相邻的地块进行种植,并考虑所有可能的种植方案,包括不种植的情况。本文详细介绍了如何计算不同的种植组合方式。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Corn Fields
Time Limit: 2000MS Memory Limit: 65536K
Total Submissions: 6159 Accepted: 3268

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
#define Max_N 13
#define STATENUM 4100
#define MOD 100000000

using namespace std;

int M;//列数
int N;//行数
int cannot[Max_N];//infertile用1表示,fertile用0表示
vector<int> state;
int all_state;//行中不相邻的状态数目
int dp[Max_N][STATENUM];//dp[i][j]第i行截止到状态j的情况数

bool ok(int i)
{
    if(i & (i << 1))
        return false;
    if(i & (i >> 1))
        return false;
    return true;
}

void get_all_states()
{
    for(int i = 0;i < (1 << M);i++)
    {
        if(ok(i))
            state.push_back(i);
    }
}

int DP()
{
    state.clear();
    get_all_states();
    memset(dp, 0, sizeof(dp));
    for(int i = 0;i < state.size();i++)//首先确定第1行
    {
        if(state[i]&cannot[1])
            continue;
        dp[1][state[i]] = 1;
    }

    for(int row = 2;row <= N;row++)//从第2行开始
    {
        for(int i = 0;i < state.size();i++)//考虑每一种状态
        {
            if(state[i] & cannot[row])
                continue;
            for(int j = 0;j < state.size();j++)//枚举上一行的情况
            {
                if(state[j] & cannot[row - 1])
                    continue;
                if(state[i] & state[j])//上一行与本行不能同边
                    continue;
                dp[row][state[i]] += dp[row - 1][state[j]];
                dp[row][state[i]] %= MOD;
            }
        }
    }
    int ans = 0;
    for(int j = 0;j < state.size();j++)
        ans = (ans + dp[N][state[j]]) % MOD;
    return ans;
}

int main()
{   //freopen("in.txt","r",stdin);
    while(cin >> N >> M)
    {
        for(int i = 1;i <= N;i++)
        {
            for(int j = 1;j <= M;j++)
            {
                int a;
                cin >> a;
                cannot[i] = (a == 0 ? cannot[i]|(1 << M - j) : cannot[i]);
            }
        }
        cout << DP() << endl;
    }
    return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值