POJ 3254 Corn Fields

Corn Fields
Time Limit: 2000MS Memory Limit: 65536K
Total Submissions: 9744 Accepted: 5147

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.

/* ***********************************************
Author :
Created Time :2015/8/1 9:54:13
File Name :1.cpp
************************************************ */
#include <iostream>
#include <cstring>
#include <cstdlib>
#include <stdio.h>
#include <algorithm>
#include <vector>
#include <queue>
#include <set>
#include <map>
#include <string>
#include <math.h>
#include <stdlib.h>
#include <iomanip>
#include <list>
#include <deque>
#include <stack>
#define ull unsigned long long
#define ll long long
#define mod 100000000
#define INF 1<<30
#define maxn 10000+10
#define cle(a) memset(a,0,sizeof(a))
const int N=13;
const int M=1<<N;
const double eps=1e-5;
using namespace std;
bool cmp(int a,int b){
return a>b;
}
int n,m;
int st[M],mp[M];//分别存储每一行的状态和给出的状态
int dp[N][M];//表示第i行状态为j时可以放的牛的数量
bool judge1(int x){
return (x&(x<<1));//判断两头牛是否相邻 移动一位&
}
bool judge2(int i,int x){
return (mp[i]&st[x]);//判断当前的状态是否在 已经给出的状态中
}
int main()
{
#ifndef ONLINE_JUDGE
freopen("in.txt","r",stdin);
#endif
//freopen("out.txt","w",stdout);
while(cin>>n>>m){
//每一行可能的状态 处理出来
//dp[i][s]=sum(dp[i-1][t]);
cle(dp);
cle(st);
cle(mp);
int x;
for(int i=1;i<=n;i++){
for(int j=0;j<m;j++){
cin>>x;
if(x==0)//为什么当x==0时处理呢?因为存在这一行都不放牛的情况
mp[i]+=(1<<j);//存储当前行的状态
}
}
//初始化满足的在非约束条件下的每行可能的状态
int k=0;
for(int i=0;i<(1<<m);i++){
if(!judge1(i))
st[k++]=i;//存储满足牛不相邻的状态(有的题目是通过dfs找的)
//k的值也就是 满足牛不相邻的状态的个数
}
//dp 边界的处理
//因为dp的时候要涉及到上一行 所以这里先处理出第一行

for(int i=0;i<k;i++){
if(!judge2(1,i))
dp[1][i]=1;
}
//正式dp
for(int i=2;i<=n;i++){
for(int j=0;j<k;j++){
if(!judge2(i,j)){
for(int t=0;t<k;t++){
if(!judge2(i-1,t)){
if(!(st[j]&st[t])){//不相互包含
dp[i][j]+=dp[i-1][t];
}
}
}
}
}
}
int ans=0;
for(int i=0;i<k;i++){
ans+=dp[n][i];
ans%=mod;
}
printf("%d\n",ans);
}
return 0;
}





你可能感兴趣的:(POJ 3254 Corn Fields)