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. Mplayer ARM平台下交叉编译

    下载MPlayer http://www.mplayerhq.hu/design7/dload.html 编译环境 系统 : ubuntu 11.04 交叉编译器版本 : Sourcery G++ L ...

  2. Java代理模式——静态代理模式

    一:代理模式 代理模式的作用是:为其他对象提供一种代理以控制这个对象的访问.在某些情况下,一个客户不想或者不能直接引用另一个对象,而代理对象可以在客户端和目标对象之间起到中介的作用. 二:代理模式设计 ...

  3. MySql5压缩包安装

    一. 解压所有文件到一个目录:例如D:\Program Files\mysql-5.6.22-winx64 二. 配置系统的环境变量:在Path路径后追加:;D:\Program Files\mysq ...

  4. Uncaught TypeError: Object [object Object] has no method 'live'

    $( selector ).live( events, data, handler );                // jQuery 1.3+$( document ).delegate( se ...

  5. ajax全局函数运用

  6. PNG图片小结

    PNG: 便携式网络图形(Portable Network Graphics,PNG)是一种无损压缩的位图图形格式,支持索引.灰度.RGB三种颜色方案以及Alpha通道等特性.PNG的开发目标是改善并 ...

  7. Oops信息及栈回溯

    1. Oops信息来源及格式Oops这个单词含义为“惊讶”,当内核出错时(比如访问非法地址)打印出来的信息被称为Oops信息.Oops信息包含以下几部分内容:(1)一段文本描述信息.      比如类 ...

  8. 给WebApp加一个“壳”,实现Andriod系统添加到桌面

    IOS系统的Safari浏览器有一个“添加到桌面”的功能,能在手机桌面上为你的Webapp添加一个快捷方式,其外观和Native App看起来一样. 这个功能对Webapp来说太有用了,它能让用户像“ ...

  9. python笔记之Cmd模块

    python笔记之Cmd模块 Cmd类型提供了一个创建命令行解析器的框架,默认情况下,它使用readline来进行交互式操作.命令行编辑和命令完成. 使用cmd创建的命令行解释器循环读取输入的所有行并 ...

  10. 验证角谷猜想(hd1279)

    验证角谷猜想 Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Subm ...