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来表示贫瘠和肥沃),要求你在肥沃的单位土地上种玉米,如果在某个单位土地上种了玉米,那么与它相邻的四个单位土地是不允许种玉米 ...
随机推荐
- struts.xml如何加载到及配置问题
今天项目做客户化处理,看到struts.xml,突然间想不起来这个文件从哪里加载的了,真是越学越回去了.这里记录下. web工程启动的时候,系统会加载web.xml文件,在这个时候会加载Spring的 ...
- ORACLE SEQUENCE的简单介绍
先假设有这么一个表: create table S_Depart ( DepartId INT not null, DepartName NVARCHAR2() not null, DepartOrd ...
- [js高手之路]深入浅出webpack教程系列9-打包图片(file-loader)用法
我们还是接着上文继续,本文我们要讲的是图片资源的打包,图片在静态排版中,经常出现的两个位置是css通过background引入背景,还有一种就是在html模板文件中用img标签引入的方式,如果要在we ...
- java 实现微博,QQ联合登录
详见:http://blog.yemou.net/article/query/info/tytfjhfascvhzxcyt313 开发平台 http://connect.qq.com/ http:/ ...
- CountDownLatch和CyclicBarrier 特点比较
详见: http://blog.yemou.net/article/query/info/tytfjhfascvhzxcytp79 并发编程中的CountDownLatch和CyclicBarri ...
- 7_SQL Server通过代码删除数据
--通过代码方式删除数据select *from Employee --第一种删除方式,数据没了,表还在,id接着删除前的id继续加1delete from Employee where EmpId ...
- Spring MVC 解决无法访问静态文件和"全局异常处理"
我们都知道,Spring MVC的请求都会去找controller控制器,若果我们页面中引入了一个外部样式,这样是没效果的, 我们引入样式的时候是通过<like href="...&q ...
- centos6/7通用查看系统版本
查看centos6/7系统版本 要写一个centos系统的初始化脚本,但是centos6和centos7版本有很多命令都不相同,所以为了让脚本在两个版本之间都可以使用,就需要对centos系统版本 ...
- 团队作业4——第一次项目冲刺(Alpha版本)4.26
一.当天站立式会议照片 本次会议主要内容:汇报工作进度,根据完成情况调整进度,分配各自接口编写任务. 二.每个人的工作 三.燃尽图 横坐标:工作日,以天为单位,一共七天,代表着Alpha冲刺阶段的时间 ...
- 团队作业8----第二次项目冲刺(Beta阶段) 第七天
BETA阶段冲刺第六天 1.小会议ing 2.每个人的工作 (1)昨天已完成的工作 完成查重部分 (2) 今天计划完成的工作 (3) 工作中遇到的困难: 尤少辉:在测试的时候发现了,文件名保存到数据 ...