poj3254Corn Fields

Corn Fields
Time Limit: 2000MS   Memory Limit: 65536K
Total Submissions: 13765   Accepted: 7232

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

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.

Source

 

n*m的矩阵,放奶牛,有些地方不能放,任意两个奶牛不能挨着,问有多少种放法。

状压dp。以前做过....

位运算的奇技淫巧:

1) 要求集合中不能有两个相邻的元素

if ((mask >> 1) & mask) continue;

2) 在限定必须不取某些元素的前提下枚举子集

// mask的第x位为0表示x必须不在子集中(原集合中不含这个元素):

for (int mask1 = mask; mask1 >= 0; mask1 = (mask1 - 1) & mask)

3) 在限定必须取某些元素的前提下枚举子集

// mask的第x位为1表示x必须在子集中:

for (int mask1 = mask; mask1 < (1 << m); mask1 = (mask1 + 1) | mask)

4) 找出二进制中恰好含有 k个1的所有数

for (int mask = 0; mask < 1 << n; ) {

int tmp = mask & -mask;

mask = (mask + tmp) | (((mask ^ (mask + tmp)) >> 2) / tmp);

}

作者:李冠一
链接:https://www.zhihu.com/question/38206659/answer/75338913
来源:知乎
/* ***********************************************
Author        :guanjun
Created Time  :2016/10/31 10:32:03
File Name     :poj3254.cpp
************************************************ */
#include <iostream>
#include <cstdio>
#include <queue>
#include <cstring>
#include <string>
#define ull unsigned long long
#define ll long long
#define mod 100000000
#define INF 0x3f3f3f3f
#define maxn 10010
#define cle(a) memset(a,0,sizeof(a))
const ull inf = 1LL << 61;
const double eps=1e-5;
using namespace std;
priority_queue<int,vector<int>,greater<int> >pq;
struct Node{
    int x,y;
};
struct cmp{
    bool operator()(Node a,Node b){
        if(a.x==b.x) return a.y> b.y;
        return a.x>b.x;
    }
};

bool cmp(int a,int b){
    return a>b;
}
int dp[15][1000];//第i行状态为j 前i行所能得到方案数
int row[20];//第i行的状态
int n,m,num;
int st[1000];
void init(){
     int k=1<<m;
     num=0;
     for(int i=0;i<k;i++){
         if((i&(i<<1))==0)
             st[num++]=i;
     }
     //处理出 相邻两位不相同的集合
}
int main()
{
    #ifndef ONLINE_JUDGE
    freopen("in.txt","r",stdin);
    #endif
    //freopen("out.txt","w",stdout);
    int x;
    cin>>n>>m;
    cle(row);
    init();
    for(int i=1;i<=n;i++){
        for(int j=m-1;j>=0;j--){
            cin>>x;
            if(x)row[i]+=(1<<j);
        }
        //cout<<row[i]<<endl;
    }
    cle(dp);
    //还得初始化一下第一行
    for(int j=0;j<num;j++){
        dp[1][j]=((row[1]&st[j])==st[j])?1:0;
    }
    for(int i=2;i<=n;i++){
        for(int j=0;j<num;j++){
            if((row[i]&st[j])==st[j]){//枚举状态row[i]的子集
                for(int k=0;k<num;k++){
                    if(dp[i-1][k]&&((st[k]&st[j])==0))
                        dp[i][j]=(dp[i][j]+dp[i-1][k])%mod;
                }
            }
        }
    }
    //再处理最后一行
    int ans=0;
    for(int j=0;j<num;j++){
        if(dp[n][j])
            ans=(ans+dp[n][j])%mod;
    }
    cout<<ans<<endl;
    return 0;
}

 

posted on 2016-11-07 20:29  Beserious  阅读(225)  评论(0编辑  收藏  举报