Corn Fields(玉米地)

136 阅读1分钟

本文已参与「新人创作礼」活动,一起开启掘金创作之路。​​​ ​

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.

 数据:

n m(<=12)  n行 每行m个空地  1或0 代表能放牧和不能放牧

题意: 在一块地图里种草, 左右不能相邻,上下也不能相邻,  问一共有多少种种法(什么不种也算一种)。

CODE:

#include<stdio.h>
#include<string.h>
#include<algorithm>
using namespace std;
typedef long long ll;
const int mod=1e8;
#define mem(a,b) memset(a,b,sizeof(a))
int a[5000];///因为放1 不放0  只有两种状态  用二进制表示
///a[i]数组是第i行的数值  二进制数表示成十进制 表示这一行共有多少种状态
int dp[15][1<<13];///n行  每行1>>13种状态
int main()
{
    int n,m;
    scanf("%d %d",&n,&m);
    for(int i=1;i<=n;i++)
    {
        for(int j=1;j<=m;j++)
        {
            int b;
            scanf("%d",&b);///0 1状态
            a[i]=(a[i]<<1)+b;///a[i]记录第i行的值(十进制储存)
        }
    }
    int num=(1<<m)-1;
    dp[0][0]=1;///一头也不放牧  也算一种方案
    for(int i=1;i<=n;i++)
    {
        for(int j=0;j<=num;j++)///j当前状态
        {
            if((((j<<1)&j)==0)&&(((j>>1)&j)==0)&&((a[i]&j)==j))
            {///左不相邻  右不相邻  当前状态&a[i]==j 表示此状态可放牧
                for(int k=0;k<=num;k++)///上一行状态
                {
                    if((j&k)==0)///上下行的状态不相邻
                    dp[i][j]+=dp[i-1][k];///状态压缩
                }
            }
        }
    }
   int ans=0;
    for(int i=0;i<=num;i++)
    {
        ans=(ans+dp[n][i])%mod;///状态压缩到第n行 满足条件全部相加
    }
    printf("%d\n",ans);
    return 0;
}