zoukankan      html  css  js  c++  java
  • 洛谷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
    ",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), '', 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_textcode_streamin.txt","r",stdin);
        //freopen("D:common_textcode_streamout.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 == '
    ');
        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';
            }
        }
    }
    
    
    
    
    
    
    本博客为本人原创,如需转载,请必须声明博客的源地址。 本人博客地址为:www.cnblogs.com/qieqiemin/ 希望所写的文章对您有帮助。
  • 相关阅读:
    最大生成树与最小生成树
    有限制的最短路spfa+优先队列
    KM算法(最优匹配)
    网络最大流解方程组
    网络费用流-最小k路径覆盖
    树链剖分-点的分治(点数为k且距离最长的点对)
    树链剖分-点的分治(链的点的个数为k的点对数)
    树链剖分-点的分治(dis[i]+dis[j]==k的点对数量)
    树链剖分-点的分治(dis[i]+dis[j]<=k的点对数量)
    无向图欧拉通路
  • 原文地址:https://www.cnblogs.com/qieqiemin/p/11147095.html
Copyright © 2011-2022 走看看