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. Linux下重要日志文件及查看方式

    http://os.51cto.com/art/201108/282184_all.htm   1.Linux下重要日志文件介绍 /var/log/boot.log 该文件记录了系统在引导过程中发生的 ...

  2. [转]WCF:如何将net.tcp协议寄宿到IIS

    本文转自:http://www.cnblogs.com/Gyoung/archive/2012/12/11/2812555.html 1 部署IIS 1.1 安装WAS IIS原本是不支持非HTTP协 ...

  3. Android studio教程:[2]项目整体布局

    上篇介绍了如何创建项目,这一次将介绍创建完的项目如何呈现在开发者的眼前,介绍android studio开发环境的整体布局,让大家知道各个模块的位置和功能. 工具/原料 Android studio ...

  4. 自动化单元测试工具 EvoSuite 的简单使用

    一.EvoSuite简介 EvoSuite是由Sheffield等大学联合开发的一种开源工具,用于自动生成测试用例集,生成的测试用例均符合Junit的标准,可直接在Junit中运行. 通过使用此自动测 ...

  5. FatMouse' Trade(hdoj1009)

    Problem Description FatMouse prepared M pounds of cat food, ready to trade with the cats guarding th ...

  6. MYSQL 时间计算的 3 种函数

    方法 1. 加法 adddate('date_expression',interval value type); 'date_expression' + interval value type; -- ...

  7. Linux下Nginx+PHP 简单安装配置

    测试环境 Linux 2.6.18nginx-1.0.4 http://www.nginx.org/php-5.3.6 http://www.php.net/ 一,安装Nginxwget http:/ ...

  8. Nginx Upload Module 上传模块

    传统站点在处理文件上传请求时,普遍使用后端编程语言处理,如:Java.PHP.Python.Ruby等.今天给大家介绍Nginx的一个模块,Upload Module上传模块,此模块的原理是先把用户上 ...

  9. 键盘有没有NKRO ?微软帮你测

    玩家甚至媒体的解读是错的,所以小编在此重点说明一些概念.并分享如何测试.在许多游戏与软体中都会使用组合键功能,也就是同时按下特定几个按键之后就能触发特别的功能,简单的说就是一些动作的快捷键.不过,有时 ...

  10. css案例学习之relative与absolute

    代码 <!DOCTYPE html PUBliC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.o ...