码迷,mamicode.com
首页 > 其他好文 > 详细

POJ 3254 Corn Fields DP 状态压缩 入门

时间:2014-09-23 17:30:45      阅读:191      评论:0      收藏:0      [点我收藏+]

标签:c++

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 / 用位运算一定要打括号。。

POJ 3254 Corn Fields DP 状态压缩 入门

标签:c++

原文地址:http://blog.csdn.net/axuan_k/article/details/39499497

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!