洛谷P1879 [USACO06NOV]玉米田Corn Fields (状态压缩DP)

本文详细解析了一道经典的算法题目,即如何在考虑到土地肥沃度和相邻限制的情况下,计算牧场种植草皮的所有可能方案数量。通过使用动态规划和状态压缩技巧,文章提供了一个高效的解决方案。

题目描述
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.

农场主John新买了一块长方形的新牧场,这块牧场被划分成M行N列(1 ≤ M ≤ 12; 1 ≤ N ≤ 12),每一格都是一块正方形的土地。John打算在牧场上的某几格里种上美味的草,供他的奶牛们享用。

遗憾的是,有些土地相当贫瘠,不能用来种草。并且,奶牛们喜欢独占一块草地的感觉,于是John不会选择两块相邻的土地,也就是说,没有哪两块草地有公共边。

John想知道,如果不考虑草地的总块数,那么,一共有多少种种植方案可供他选择?(当然,把新牧场完全荒废也是一种方案)

输入输出格式
输入格式:
第一行:两个整数M和N,用空格隔开。

第2到第M+1行:每行包含N个用空格隔开的整数,描述了每块土地的状态。第i+1行描述了第i行的土地,所有整数均为0或1,是1的话,表示这块土地足够肥沃,0则表示这块土地不适合种草。

输出格式:
一个整数,即牧场分配总方案数除以100,000,000的余数。

输入输出样例
输入样例#1:
2 3
1 1 1
0 1 0
输出样例#1:
9

题意:

思路:

对于每一行 ,最多有m个1,每一个牧田选择与不选择有两个状态,所以所有的状态是 2^m-1,因为m不大于31,所以我们可以用一个int类型的整数来表示牧田选择的信息。(二进制状态,第i为选择的话,那么int数中的第i位就为1)

首先预处理出0~(1<<m)-1 中所有可能情况中, 没有相邻牧田的状态。
然后处理一行中,符合肥沃条件而且符合不相邻约数条件的状态i,
我们令 dp[1][i]=1;
即我们定义DP的状态是 dp[i][j] 表示从第i行选择第j个状态时,所有可能的种类数。
那么转移方程是:
我们枚举每一个上一行的状态k (因为当前行是否合法只和上一行的状态有关。)
dp[i][j]=(dp[i][j]+dp[i-1][k])%mod;

代码有更每一行的功能备注和说明。

细节见代码:

#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cmath>
#include <queue>
#include <stack>
#include <map>
#include <set>
#include <vector>
#include <iomanip>
#define ALL(x) (x).begin(), (x).end()
#define rt return
#define dll(x) scanf("%I64d",&x)
#define xll(x) printf("%I64d\n",x)
#define sz(a) int(a.size())
#define all(a) a.begin(), a.end()
#define rep(i,x,n) for(int i=x;i<n;i++)
#define repd(i,x,n) for(int i=x;i<=n;i++)
#define pii pair<int,int>
#define pll pair<long long ,long long>
#define gbtb ios::sync_with_stdio(false),cin.tie(0),cout.tie(0)
#define MS0(X) memset((X), 0, sizeof((X)))
#define MSC0(X) memset((X), '\0', sizeof((X)))
#define pb push_back
#define mp make_pair
#define fi first
#define se second
#define eps 1e-6
#define gg(x) getInt(&x)
#define chu(x) cout<<"["<<#x<<" "<<(x)<<"]"<<endl
using namespace std;
typedef long long ll;
ll gcd(ll a,ll b){return b?gcd(b,a%b):a;}
ll lcm(ll a,ll b){return a/gcd(a,b)*b;}
ll powmod(ll a,ll b,ll MOD){ll ans=1;while(b){if(b%2)ans=ans*a%MOD;a=a*a%MOD;b/=2;}return ans;}
inline void getInt(int* p);
const int maxn=1000010;
const int inf=0x3f3f3f3f;
/*** TEMPLATE CODE * * STARTS HERE ***/
const ll mod=1e9;
int n,m;
int a[50];
bool can[maxn];
ll dp[15][maxn];
int main()
{
    //freopen("D:\common_text\code_stream\in.txt","r",stdin);
    //freopen("D:\common_text\code_stream\out.txt","w",stdout);
    gbtb;
    cin>>n>>m;
    int x;
    repd(i,1,n)
    {
        repd(j,1,m)
        {
            cin>>x;
            a[i]=(a[i]<<1)+x;
        }
    }
    int maxstate=(1<<m)-1;// 每一行最多的状态数
    for(int i=0;i<=maxstate;i++)
    {
        if(((i<<1)&i)==0&&((i>>1)&i)==0)// 确保数字i的二进制信息中没有相邻的1
        {
            can[i]=1;
        }
    }

    for(int i=0;i<=maxstate;i++)
    {
        if(can[i])
        {
            if((a[1]&i)==i)// 确实i是a[1]肥沃状态的子集
            {
                dp[1][i]=1;
            }
        }
    }
    for(int i=2;i<=n;i++)
    {
        for(int j=0;j<=maxstate;j++)
        {
            if(can[j]&&(a[i]&j)==j)
            {
                for(int k=0;k<=maxn;k++)// 暴力枚举上一行的所有状态
                {
                    if(can[k])
                    {
                        if((j&k)==0)// 上一行的k状态和这一行的j状态,没有上下相邻
                        {
                            dp[i][j]=(dp[i][j]+dp[i-1][k])%mod;
                        }
                    }
                }
            }
        }
    }
    ll ans=0ll;
    for(int i=0;i<=maxstate;i++)
    {
        ans=(ans+dp[n][i])%mod;// 累计答案。
    }
    cout<<ans<<endl;
    
    return 0;
}

inline void getInt(int* p) {
    char ch;
    do {
        ch = getchar();
    } while (ch == ' ' || ch == '\n');
    if (ch == '-') {
        *p = -(getchar() - '0');
        while ((ch = getchar()) >= '0' && ch <= '9') {
            *p = *p * 10 - ch + '0';
        }
    }
    else {
        *p = ch - '0';
        while ((ch = getchar()) >= '0' && ch <= '9') {
            *p = *p * 10 + ch - '0';
        }
    }
}




转载于:https://www.cnblogs.com/qieqiemin/p/11147095.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值