Corn Fields poj3254(状态压缩DP)
| Time Limit: 2000MS | Memory Limit: 65536K | |
| Total Submissions: 6081 | Accepted: 3226 |
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
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
Sample Input
2 3
1 1 1
0 1 0
Sample Output
9
Hint
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.
#include <iostream>
#include <stdio.h>
#include <string.h>
#include <math.h>
#include <algorithm>
using namespace std;
#define mod 100000000
int a[],dp[][<<];
bool check(int x,int y)
{
int z=x|y;
if(z>y)return ;
else return ;
}
bool check2(int x)
{
int z=x&;
x>>=;
while(x)
{
if(z&(x&))return ;
z=x&;
x>>=;
}
return ;
}
bool check1(int x,int y)
{
if(x&y)return ;
else return ;
}
int main()
{
int m,n,i,j,x,k;
cin>>m>>n;
for(i=; i<m; i++)
{
x=,a[i]=;
for(j=; j<n; j++)
{
scanf("%d",&x);
a[i]=(a[i]<<)+x;
}
}
memset(dp,,sizeof(dp));
for(j=; j<(<<n); j++)
if(check(j,a[])&&check2(j))
dp[][j]=;
for(i=; i<m; i++)
{
for(j=; j<(<<n); j++)
{
if(check(j,a[i])&&check2(j))
{
for(k=; k<(<<n); k++)
{
if(check1(k,j))
dp[i][j]+=dp[i-][k],dp[i][j]%=mod;
}
}
}
}
int sum=;
for(i=;i<(<<n);i++)sum+=dp[m-][i],sum%=mod;
cout<<sum<<endl;
}
Corn Fields poj3254(状态压缩DP)的更多相关文章
- Corn Fields——POJ3254状态压缩Dp
Corn Fields Time Limit: 2000MS Memory Limit: 65536K Description Farmer John has purchased a lush new ...
- 洛谷P1879 [USACO06NOV]玉米田Corn Fields (状态压缩DP)
题目描述 Farmer John has purchased a lush new rectangular pasture composed of M by N (1 ≤ M ≤ 12; 1 ≤ N ...
- POJ 3254 Corn Fields(状态压缩DP)
题目大意:给出一个M*N的矩阵,元素为0表示这个地方不能种玉米,为1表示这个地方能种玉米,现在规定所种的玉米不能相邻,即每行或者没列不能有相邻的玉米,问一共有多少种种植方法. 举个例子: 2 3 1 ...
- POJ 3254:Corn Fields(状态压缩DP)
题目大意:一个矩形的草地,分为多个格子,有的格子可以有奶牛(标为1),有的格子不可以放置奶牛(标为0),计算摆放奶牛的方案数. 分析: f[i,j]表示第i行状态为j的方案总数. 状态转移方程f[i, ...
- poj3254 Corn Fields 利用状态压缩求方案数;
Corn Fields 2015-11-25 13:42:33 Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 10658 ...
- poj - 3254 - Corn Fields (状态压缩)
poj - 3254 - Corn Fields (状态压缩)超详细 参考了 @外出散步 的博客,在此基础上增加了说明 题意: 农夫有一块地,被划分为m行n列大小相等的格子,其中一些格子是可以放牧的( ...
- poj3254 状态压缩dp
题意:给出一个n行m列的草地,1表示肥沃,0表示贫瘠,现在要把一些牛放在肥沃的草地上,但是要求所有牛不能相邻,问你有多少种放法. 分析:假如我们知道第 i-1 行的所有的可以放的情况,那么对于 ...
- POJ 3254 Corn Fields(状态压缩)
一道状态压缩的题,错了好多次....应该先把满足的情况预处理出来 #include<iostream> #include<cstdio> #include<cstring ...
- POJ3254 - Corn Fields(状态压缩DP)
题目大意 给定一个N*M大小的土地,土地有肥沃和贫瘠之分(每个单位土地用0,1来表示贫瘠和肥沃),要求你在肥沃的单位土地上种玉米,如果在某个单位土地上种了玉米,那么与它相邻的四个单位土地是不允许种玉米 ...
随机推荐
- [2014-12-30]如何动态构造Lambda表达式(动态构造Lambda查询条件表达式)
声明 本文对Lambda表达式的扩展,示例代码来源于网络. 场景描述 web开发查询功能的时候,如果查询条件比较多,就会遇到动态组合查询条件的情况.在手写sql的情况下,我们一般会根据传入的参数,针对 ...
- MariaDB体验1----数据库安装
之前一直都是使用的微软Sql Server数据库,现在在学习数据分析,要用到mysql.刚好公司在进行云架构升级,数据库选型为MariaDB,就顺势安装体验了一把MariaDB.这里记录一下学习过程. ...
- java基础解析系列(四)---LinkedHashMap的原理及LRU算法的实现
java基础解析系列(四)---LinkedHashMap的原理及LRU算法的实现 java基础解析系列(一)---String.StringBuffer.StringBuilder java基础解析 ...
- postman 第3节 API请求和查看响应结果(转)
请求 postman支持很多请求类型,界面左侧可以看到请求类型:get.post.put.patch等,右侧是发送和保存按钮,下方是请求支持的认证方式.信息头.信息体.私有脚本和测试结果.下面我们介绍 ...
- 谈谈.NET,Java,php
开通博客后,一直都是转点别的朋友写的有意思的博文,今天我来写我在博客园的第一篇文章,说的不对的地方请你指正.希望本文能为一些准备学习编程的朋友有一点帮助. 开发桌面程序一直都是c语言,c++的天下,因 ...
- h5的video标签
在video标签中,我们可以使用属性:videoWidth & videoHeight,它获取的是video的宽度和高度(媒体本身). 虽然不能直接使用,但是可以通过计算宽高比得到 video ...
- C++中模板Template的使用
1. 在c++Template中很多地方都用到了typename与class这两个关键字,而且好像可以替换,是不是这两个关键字完全一样呢?class用于定义类,在模板引入c++后,最初定义模板的方法为 ...
- python 从视频中提取图片,并保存在硬盘上
使用python的moviepy库来提取视频中的图片,按照视频每帧一个图片的方式来保存. extract images from video, than save them to disk from ...
- 在数组a中,a[i]+a[j]=a[k],求a[k]的最大值,a[k]max——猎八哥fly
在数组a中,a[i]+a[j]=a[k],求a[k]的最大值,a[k]max. 思路:将a中的数组两两相加,组成一个新的数组.并将新的数组和a数组进行sort排序.然后将a数组从大到小与新数组比较,如 ...
- 201521123107 《Java程序设计》第8周学习总结
第7周作业-集合 1.本周学习总结 2.书面作业 1.List中指定元素的删除 题集jmu-Java-05-集合之4-1 1.1 实验总结 这次的函数题是编写convertStringToList和r ...