Corn Fields(POJ-3254)(状压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.

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列大小相等的格子,其中一些格子是可以放牧的(用1标记),农夫可以在这些格子里放牛,其他格子则不能放牛(用0标记),并且要求不可以使相邻格子都有牛。现在输入数据给出这块地的大小及可否放牧的情况,求该农夫有多少种放牧方案可以选择(不放也是一种方案)。

思路:这道题的话,我们从样例入手去推导,以样例数据第一行为例,三个格子都可以放牧,即每个格子都可以选择放,或不放。再考虑附加条件“相邻格子不可同时放牧”,那么我们可以列出单看第一行时的所有可行状态如下(1代表放牧,0代表不放牧):

1      0 0 0
20 0 1
30 1 0
41 0 0
51 0 1

由此,我们就可以将这些状态看成二进制数,将其用十进制表示出来。

然后我们设dp[i][j]表示当第i行的状态为j时前i行的放牛方案总数。

所以状态转移方程便是 dp[i][j] = dp[i][j]+dp[i-1][x] //x代表第i-1行所有符合条件的状态数。

AC代码:

#include <stdio.h>
#include <string>
#include <string.h>
#include <iostream>
#include <algorithm>
#include <math.h>
typedef long long ll;
const int maxx=110;
const int inf=0x3f3f3f3f;
const int mod=100000000;
using namespace std;
int dp[maxx][1<<15];
int state[1<<15];
int a[15];
int n,m;
bool judge(int x)
{
    if(x&(x<<1))
        return false;
    return true;
}
int main()
{
    while(~scanf("%d%d",&n,&m))
    {
        int k=0;
        for(int i=0; i<(1<<m); i++)
        {
            if(judge(i))
                state[++k]=i;
        }
        int ans;
        memset(dp,0,sizeof(dp));
        for(int i=1; i<=n; i++)
        {
            a[i]=0;
            for(int j=1; j<=m; j++)
            {
                scanf("%d",&ans);
                if(ans==0)
                    a[i]+=(1<<(j-1));
            }
        }
        for(int i=1; i<=k; i++)
        {
            if(!(a[1]&state[i]))
            {
                dp[1][i]=1;
            }
        }
        for(int i=2; i<=n; i++)
        {
            for(int j=1; j<=k; j++)
            {
                if(a[i]&state[j])
                    continue;
                for(int x=1; x<=k; x++)
                {
                    if(a[i-1]&state[x])
                        continue;
                    if(state[j]&state[x])
                        continue;
                    dp[i][j]=(dp[i][j]+dp[i-1][x]+mod)%mod;
                }
            }
        }
        int num=0;
        for(int i=1; i<=k; i++)
        {
            num=(num+dp[n][i]+mod)%mod;
        }
        printf("%d\n",num);
    }
    return 0;
}

 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值