ICode9

精准搜索请尝试: 精确搜索
首页 > 其他分享> 文章详细

G - Corn Fields

2019-01-22 18:02:26  阅读:350  来源: 互联网

标签:plant include ways Fields Corn state squares dp


G - Corn Fields

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的地方为空地,现在要建造围栏,围栏必须建在草上,且围栏不能上下左右相邻。

分析:

暴力出所有情况即可,数位dp的入门题,用n位数的二进制代表这一行围栏放的情况,d[r][state]d[r][state]d[r][state]为第 r 行状态为state的方法数,计算该方法数的时候 暴力枚举上一行所有的状态,如果状态兼容,则可以有这种状态,上一行状态的方法数即可。

dp[r][state]rrstatedpdp[m][state]dp[r][state]表示只考虑前r行兼容,第r行状态为state的方法数,然后dp即可,得到dp[m][state]求和即是答案dp[r][state]表示只考虑前r行兼容,第r行状态为state的方法数,然后dp即可,得到dp[m][state]求和即是答案

#include<cstdio>
#include<algorithm>
#include<stack>
#include<queue>
#include<map>
#include<set>
#include<cmath>
#include<vector>
#include<cstring>
#include<string>
#include<iostream>
#include<iomanip>
#define mset(a,b)   memset(a,b,sizeof(a))
using namespace std;
typedef unsigned long long ull;
typedef long long ll;
const int maxn=1e5+10;
const int branch=26;
const int inf=0x7fffffff;
const ll  mod=1e8;
int w[15];
ll dp[15][1<<12];
int state[1<<12],tot;
char hhh()
{
    char c=getchar();
    while(c==' '||c=='\n')
        c=getchar();
    return c;
}
int main()
{
    int m,n;
    scanf("%d%d",&m,&n);
    for(int i=1; i<=m; ++i)
    {
        w[i]=0;
        for(int j=0; j<n; ++j)
        {
            w[i]<<=1;
            if(hhh()=='1')
                w[i]+=1;
        }
    }
    tot=0;
    for(int i=0; i<(1<<n); ++i)//预处理出肯定的一行可能的状态
    {
        if(((i<<1)&i)==0)//1 1不能相邻
            state[tot++]=i;
    }
    mset(dp,0);
    for(int i=0; i<tot; ++i)//预处理第一行
    {
        if((state[i]&w[1])==state[i])
            dp[1][i]=1;
    }
    for(int r=2; r<=m; ++r)
    {
        for(int i=0; i<tot; ++i)
        {
            if((state[i]&w[r])==state[i])
            {
                for(int j=0; j<tot; ++j)
                {
                    if(((state[j]&w[r-1])==state[j])&&(state[i]&state[j])==0)
                    {
                        dp[r][i]+=dp[r-1][j];
                    }

                }
                dp[r][i]%=mod;
            }

        }
    }
    ll ans=0;
    for(int i=0;i<tot;++i)
    {
        ans=(ans+dp[m][i])%mod;
    }
    cout<<ans<<endl;
    return 0;

}

标签:plant,include,ways,Fields,Corn,state,squares,dp
来源: https://blog.csdn.net/Dch19990825/article/details/86598951

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有