poj 3254 Corn Field 状态压缩dp

农夫约翰新购得一块牧场,需要规划如何种植玉米供奶牛食用。考虑到奶牛不喜欢相邻吃草,约翰需要计算在限制条件下有多少种种植方案。此问题可通过动态规划解决。

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

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.

题目大意: 农夫有一块地,被划分为m行n列大小相等的格子,其中一些格子是可以放牧的(用1标记),农夫可以在这些格子里放牛,其他格子则不能放牛(用0标记),并且要求不可以使相邻格子都有牛。现在输入数据给出这块地的大小及可否放牧的情况,求该农夫有多少种放牧方案可以选择(注意:任何格子都不放也是一种选择,不要忘记考虑!

考虑总数的话 就是每个状态叠加 考虑状态最大值就是求最大的状态。
这个叠加状态即可

#include <iostream>
#include <cstdio>
#include <algorithm>
#include <cstring>
using namespace std;
const int mod=1e8;
int dp[30][1010];
int mp[30];
int st[1010];
int n,m;
int ok(int x)
{
    if((x<<1)&x) return 0;
    return 1;
}
int tot;
void findst()
{
    int en=(1<<m);
    for(int i=0;i<en;i++)
    {
        if(ok(i)) st[tot++]=i;
    }
}

int main()
{
    while(scanf("%d%d",&n,&m)!=EOF)
    {
        memset(dp,0,sizeof(dp));
        for(int i=0;i<n;i++)
        {
            for(int j=0;j<m;j++)
            {
                int a;
                scanf("%d",&a);
                if(a==0)
                mp[i]|=(1<<j);
            }
        }
        tot=0;
        findst();
        for(int i=0;i<tot;i++)
        {
            if(st[i]&mp[0]) continue;
            dp[0][i]=1;
        }
        for(int r=1;r<n;r++)
        {
            for(int i=0;i<tot;i++)
            {
                if(st[i]&mp[r]) continue;
                for(int j=0;j<tot;j++)
                {
                    if(st[i]&st[j]) continue;
                    if(st[j]&mp[r-1]) continue;
                    dp[r-1][j]%=mod;
                    dp[r][i]+=dp[r-1][j];
                    dp[r][i]%=mod;
                }
            }
        }
        int maxx=0;
        for(int i=0;i<tot;i++)
        {
            maxx=(maxx+dp[n-1][i])%mod;
        }
        printf("%d\n",maxx );
    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值