Corn Fields(玉米地)

POJ3254传送门

 

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;
}

 加油吧Acmer!!

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

嵩韵儿

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值