POJ 3254 Corn Fields DP 状态压缩 入门

Description

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

题意:农场主有一块M X N的玉米地,每一小块地可能有两种情况,肥沃(1) 和不肥沃(0) ,只有肥沃的地能种玉米 , 而两块相邻土地不能同时种玉米 
问共有多少种种法    

思路 将每行的的状态 看做一个2进制  以10进制的形式保存 再通过 位运算, 和动态规划的思想解决问题。。。
dp[i][j]表示第i行上出现j状态时候的所有种类
代码:
 
#include<iostream>
#include<cstdio>
#include<cstring>
#include<cmath>
#define mod 100000000
using namespace std;
int main()
{
    int m,n,i,j,s,k,t;
    int ans;
    int dp[20][555];
    int q[20];   //  保存每一行的状态  2进制  以 10进制表示
    int w[555];  // 一行n个数字 符合条件的状态
    while(~scanf("%d%d",&m,&n))
    {
        ans=0;
        memset(w,0,sizeof(w));
        memset(q,0,sizeof(q));
        memset(dp,0,sizeof(dp));
        for(i=0; i<m; i++)
            for(j=n-1; j>=0; j--)
            {
                scanf("%d",&s);
                if(s==0)
                    q[i]=q[i]+(1<<j);    //将 不肥沃的土地用1表示 方便之后&的与运算
            }
        t=0;
        for(i=0; i<(1<<n); i++)
        {
            if((i&(2*i)))       //二进制的特性 原数X2=最右边加个0  如果两个1相邻 就不符合条件
                continue;
                w[t++]=i;
        }
        for(i=0; i<t; i++)
          {
              if((q[0]&w[i]))    //初始 第一行
                continue;
                dp[0][i]=1;
          }
        for(i=1; i<m; i++)
            for(j=0; j<t; j++)
            {
                if(q[i-1]&w[j])    //前一行的不符合条件的状态 跳过
                    continue;
                for(k=0;k<t;k++)
                {
                    if((q[i]&w[k])||(w[k]&w[j]))  //同上
                        continue;
                    dp[i][k]=(dp[i][k]+dp[i-1][j])%mod;
                }
            }
        for(i=0;i<t;i++)
            ans=(ans+dp[m-1][i])%mod;
            cout<<ans<<endl;
    }
    return 0;
}

值得注意的位运算的优先级 弱于 + - X / 用位运算一定要打括号。。
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值