poj3254Corn Fields题解

Corn Fields

Time Limit: 2000MS   Memory Limit: 65536K
Total Submissions: 9623   Accepted: 5092

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.

题目大意:

给你一个N*M的矩阵,矩阵里的元素由0和1组成,1代表肥沃的土地可以种草,0则不可以种草。如下: N=2 M=3 1 1 1 0 1 0 现有若干头牛,请将它们放入有草的地方吃草,注意上下左右不能相邻。 那么问题来了,请问有多少种放法?

分析:

  1 #include<iostream>
  2 #include<sstream>
  3 #include<stdio.h>
  4 #include<string>
  5 #include<string.h>
  6 #include<math.h>
  7 #include<time.h>
  8 #include<algorithm>
  9
 10 #define LEN 1000000
 11 #define INF 99999
 12 #define ALLSTATES 4096 //最大状态
 13
 14 using namespace std;
 15
 16 int allstates=0;
 17 int n,m;
 18 int F[13][ALLSTATES]={0};   //方法数
 19 int Matrix[13][13]={0};//土地的样子
 20
 21 bool andMatrix(int row,int states)//是否和土地兼容
 22 {
 23     for(int i=1;i<=m;i++)
 24     {
 25         if(Matrix[row][i]==0)//如果这里是空的 那么肯定不能放牛
 26         {
 27             if(states&(1<<i-1))
 28             {
 29                 return false;
 30             }
 31         }
 32
 33     }
 34     return true;
 35 }
 36
 37 bool linetest(int states)//判断行是否相邻
 38 {
 39     int i=0;
 40
 41     while(i<m)
 42     {
 43         if(states&(1<<i))//如果是1 那么你的左边不应该是1
 44         {
 45             if(i+1<m && states&(1<<(i+1))){    return false;        }//是1返回错误
 46              else{i+=2;}//不是1 跳过一格
 47         }
 48         else{    i++;    }
 49     }
 50         return true;
 51 }
 52
 53 bool upanddown(int upstates,int downstates)//判断上下是否相邻
 54 {
 55     int i=0;
 56
 57     while(i<m)
 58     {
 59         if(upstates&(1<<i) && downstates&(1<<i))
 60         {
 61             return false;
 62         }
 63         else
 64         {
 65             i++;
 66         }
 67
 68     }
 69     return true;
 70 }
 71
 72 int main()
 73 {
 74     //读入--------------------------------------------
 75     cin>>n>>m;
 76      //读入矩阵
 77     for(int i=1;i<=n;i++)
 78       for(int j=1;j<=m;j++)
 79       {
 80             cin>>Matrix[i][j];
 81       }
 82     //处理--------------------------------------------
 83     allstates=1<<m;//m个格子的所有状态
 84 //    cout<<andMatrix(1,5)<<endl;
 85 //    cout<<linetest(5)<<endl;
 86     //cout<<allstates;
 87     //先处理第一行
 88     for(int j=0;j<allstates;j++)//allstates为所有状态
 89     {
 90         if(andMatrix(1,j) && linetest(j))//
 91         {
 92             F[1][j]=1;
 93         }
 94     }
 95     //cout<<allstates;
 96     //处理后面的行数
 97       for(int row=2;row<=n;row++)
 98        for(int j=0;j<allstates;j++)
 99        {
100                if(andMatrix(row,j)==0 || !linetest(j))//如果j不符合土地直接跳过
101             {
102                 continue;
103             }
104         for(int k=0;k<allstates;k++)
105         {
106             if(F[row-1][k] && upanddown(j,k))
107             {
108                 F[row][j]+=F[row-1][k];
109             }
110         }
111       }
112       //统计所有方法数
113      int ct=0;
114
115      //for(int i=1;i<=n;i++)
116 //     {
117 //      for(int j=0;j<allstates;j++)
118 //      {
119 //          cout<<F[i][j];
120 //      }
121 //      cout<<endl;
122 //     }
123       //cout<<ct;
124        for(int j=0;j<allstates;j++)
125        {
126             ct+=F[n][j];
127             ct%=100000000;
128        }
129       cout<<ct;
130
131     return 0;
132 }

2015-07-26 13:21:58

时间: 2024-11-25 16:22:03

poj3254Corn Fields题解的相关文章

poj3254Corn Fields状压Dp

用一个数记录上一行取的状态,在枚举此时的状态,并且把符合条件的传递下去.判断写的有点丑,roll 直接位运算搞定. #include <cstdio> #include <cstring> #include <cmath> #include <algorithm> #include <climits> #include <string> #include <iostream> #include <map> #i

poj3254--Corn Fields(状压dp)

Corn Fields Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 8512   Accepted: 4540 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 yumm

P1879 [USACO06NOV]玉米田Corn Fields题解(注释版)

题目描述 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 b

POJ3254Corn Fields 状压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 plan

题解——[USACO06NOV]玉米田Corn Fields 状压DP

题面: 农场主John新买了一块长方形的新牧场,这块牧场被划分成M行N列(1 ≤ M ≤ 12; 1 ≤ N ≤ 12),每一格都是一块正方形的土地.John打算在牧场上的某几格里种上美味的草,供他的奶牛们享用. 遗憾的是,有些土地相当贫瘠,不能用来种草.并且,奶牛们喜欢独占一块草地的感觉,于是John不会选择两块相邻的土地,也就是说,没有哪两块草地有公共边. John想知道,如果不考虑草地的总块数,那么,一共有多少种种植方案可供他选择?(当然,把新牧场完全荒废也是一种方案) 输出一个整数,即牧

洛谷P1879 [USACO06NOV]玉米田Corn Fields【状压DP】题解+AC代码

题目描述 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 b

【luogu P1879 [USACO06NOV]玉米田Corn Fields】 题解

题目链接:https://www.luogu.org/problemnew/show/P1879 状压DP. 设dp[i][j]表示第i行,状态为j的方案数 初始dp[0][0] = 1 这样一共12行12列,最多1<<12. 这样转移时,只要满足上下没有两个1,这两行分别满足没有相邻1. 加法原理转移. $ j&k==0 $ $ dp[i][j] += dp[i-1][k] $ #include <cstdio> #include <cstring> #inc

【题解】Luogu P2212 [USACO14MAR] 浇地 Watering the Fields 最小生成树

裸的板子,判一下d和c的大小 我菜死了,kruskal写挂的原因竟然是,sort的范围错了 WA到爆了 kruskal都忘了怎么写,赶快滚去复习 code 1 #include <bits/stdc++.h> 2 using namespace std; 3 namespace gengyf{ 4 #define ll long long 5 const int maxn=1e6+10; 6 inline int read(){ 7 int x=0,f=1; 8 char c=getchar(

Poj 3254 Corn Fields(状态压缩)

Corn Fields Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 8291   Accepted: 4409 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 yumm