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 现有若干头牛,请将它们放入有草的地方吃草,注意上下左右不能相邻。 那么问题来了,请问有多少种放法?

分析:

 #include<iostream>
#include<sstream>
#include<stdio.h>
#include<string>
#include<string.h>
#include<math.h>
#include<time.h>
#include<algorithm> #define LEN 1000000
#define INF 99999
#define ALLSTATES 4096 //最大状态 using namespace std; int allstates=;
int n,m;
int F[][ALLSTATES]={}; //方法数
int Matrix[][]={};//土地的样子 bool andMatrix(int row,int states)//是否和土地兼容
{
for(int i=;i<=m;i++)
{
if(Matrix[row][i]==)//如果这里是空的 那么肯定不能放牛
{
if(states&(<<i-))
{
return false;
}
} }
return true;
} bool linetest(int states)//判断行是否相邻
{
int i=; while(i<m)
{
if(states&(<<i))//如果是1 那么你的左边不应该是1
{
if(i+<m && states&(<<(i+))){ return false; }//是1返回错误
else{i+=;}//不是1 跳过一格
}
else{ i++; }
}
return true;
} bool upanddown(int upstates,int downstates)//判断上下是否相邻
{
int i=; while(i<m)
{
if(upstates&(<<i) && downstates&(<<i))
{
return false;
}
else
{
i++;
} }
return true;
} int main()
{
//读入--------------------------------------------
cin>>n>>m;
//读入矩阵
for(int i=;i<=n;i++)
for(int j=;j<=m;j++)
{
cin>>Matrix[i][j];
}
//处理--------------------------------------------
allstates=<<m;//m个格子的所有状态
// cout<<andMatrix(1,5)<<endl;
// cout<<linetest(5)<<endl;
//cout<<allstates;
//先处理第一行
for(int j=;j<allstates;j++)//allstates为所有状态
{
if(andMatrix(,j) && linetest(j))//
{
F[][j]=;
}
}
//cout<<allstates;
//处理后面的行数
for(int row=;row<=n;row++)
for(int j=;j<allstates;j++)
{
if(andMatrix(row,j)== || !linetest(j))//如果j不符合土地直接跳过
{
continue;
}
for(int k=;k<allstates;k++)
{
if(F[row-][k] && upanddown(j,k))
{
F[row][j]+=F[row-][k];
}
}
}
//统计所有方法数
int ct=; //for(int i=1;i<=n;i++)
// {
// for(int j=0;j<allstates;j++)
// {
// cout<<F[i][j];
// }
// cout<<endl;
// }
//cout<<ct;
for(int j=;j<allstates;j++)
{
ct+=F[n][j];
ct%=;
}
cout<<ct; return ;
}
2015-07-26 13:21:58
 

poj3254Corn Fields题解的更多相关文章

  1. poj3254Corn Fields

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

  2. 洛谷 P1879 [USACO06NOV]玉米田Corn Fields 题解

    P1879 [USACO06NOV]玉米田Corn Fields 题目描述 Farmer John has purchased a lush new rectangular pasture compo ...

  3. 洛谷 P2212 [USACO14MAR]浇地Watering the Fields 题解

    P2212 [USACO14MAR]浇地Watering the Fields 题目描述 Due to a lack of rain, Farmer John wants to build an ir ...

  4. POJ3254Corn Fields(状态压缩DP入门)

    题目链接 题意:一个矩阵里有很多格子,每个格子有两种状态,可以放牧和不可以放牧,可以放牧用1表示,否则用0表示,在这块牧场放牛,要求两个相邻的方格不能同时放牛,即牛与牛不能相邻.问有多少种放牛方案(一 ...

  5. poj3254Corn Fields(状压)

    http://poj.org/problem?id=3254 第一个状压题 思路挺好想 用二进制表示每行的状态 然后递推 用左移 右移来判断是不是01间隔出现 c大神教的 我的代码WA在这个地方了.. ...

  6. POJ3254Corn Fields(状压DP)

    题意: John 有一个豪华的M*N个格子组成的新牧场 他想种美味的玉米 但是有些位置不能种 而且他种地不选择相邻的格子 求所有可能的种地方法 (不种也算一种选择)输入:第一行M和N, 第二行M*N地 ...

  7. POJ3254Corn Fields——状态压缩dp

    题目:http://poj.org/problem?id=3254 1.枚举行: 2.把有影响的“放不放牛”加入参数中,用二进制数表示该位置放不放牛,再用十进制数表示二进制数: 3.优美的预处理lis ...

  8. POJ3254:Corn Fields——题解

    http://poj.org/problem?id=3254 题面来自洛谷:https://www.luogu.org/problemnew/show/1879 农场主John新买了一块长方形的新牧场 ...

  9. POJ3254Corn Fields (状态压缩or插头DP)

    Description Farmer John has purchased a lush new rectangular pasture composed of M by N (1 ≤ M ≤ 12; ...

随机推荐

  1. Android studio教程:[1] 创建app项目

    Android studio作为面市不久的安卓开发工具,越来越受到大家的喜爱,这里我将介绍如何在Android studio中创建一个APP项目,并在以后经验中介绍其他有关Android studio ...

  2. 链接<a href="javascript:void(0)" />

    1.<a href="#"></a>——点击链接后,页面会向上滚到页首,# 默认锚点为 #TOP 2.<a href="javascript ...

  3. mono for android 学习资料

    http://www.cnblogs.com/Hua-Min/tag/Android/

  4. G - Reduced ID Numbers(第二季水)

    Description T. Chur teaches various groups of students at university U. Every U-student has a unique ...

  5. git 常用命令总结。

    引用:http://www.liaoxuefeng.com/wiki/0013739516305929606dd18361248578c67b8067c8c017b000/00137396284551 ...

  6. spring的IOC,DI及案例详解

    一:spring的基本特征 Spring是一个非常活跃的开源框架:它是一个基于Core来架构多层JavaEE系统的框架,它的主要目的是简化企业开发.Spring以一种非侵入式的方式来管理你的代码,Sp ...

  7. Windows -DOS 下Yii创建应用及出错处理

    Win7下,以管理员身份运行cmd.exe 本例是在wamp环境下:切换到d盘,再切换到framework目录.............1--C:\windows\system32>d: 2-- ...

  8. C语言基础09

    指向结构体变量的指针叫做结构体指针: typedef struct { int num; char name[30];    // char *name; 程序会崩溃,*name本身是指针,没有什么空 ...

  9. MYSQL SQL Server 事务

    开始: start transaction;   #   一定要有这个 ‘;’ 号. 注意: MYSQL  用的是快照隔离.就是说一个连接在修改的时候别的连接还是可以查询的. 例子: create t ...

  10. ASP.NET Request.QueryString 出现乱码问题

    前台: var txing = $("#txing").combobox("getValues"); .......... &tixing=" ...