poj3254Corn Fields
Time Limit: 2000MS | Memory Limit: 65536K | |
Total Submissions: 13765 | Accepted: 7232 |
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.
Source
n*m的矩阵,放奶牛,有些地方不能放,任意两个奶牛不能挨着,问有多少种放法。
状压dp。以前做过....
位运算的奇技淫巧:
1) 要求集合中不能有两个相邻的元素
if ((mask >> 1) & mask) continue;
2) 在限定必须不取某些元素的前提下枚举子集
// mask的第x位为0表示x必须不在子集中(原集合中不含这个元素):
for (int mask1 = mask; mask1 >= 0; mask1 = (mask1 - 1) & mask)
3) 在限定必须取某些元素的前提下枚举子集
// mask的第x位为1表示x必须在子集中:
for (int mask1 = mask; mask1 < (1 << m); mask1 = (mask1 + 1) | mask)
4) 找出二进制中恰好含有 k个1的所有数
for (int mask = 0; mask < 1 << n; ) {
int tmp = mask & -mask;
mask = (mask + tmp) | (((mask ^ (mask + tmp)) >> 2) / tmp);
}
作者:李冠一
链接:https://www.zhihu.com/question/38206659/answer/75338913
来源:知乎
/* ***********************************************
Author :guanjun
Created Time :2016/10/31 10:32:03
File Name :poj3254.cpp
************************************************ */
#include <iostream>
#include <cstdio>
#include <queue>
#include <cstring>
#include <string>
#define ull unsigned long long
#define ll long long
#define mod 100000000
#define INF 0x3f3f3f3f
#define maxn 10010
#define cle(a) memset(a,0,sizeof(a))
const ull inf = 1LL << ;
const double eps=1e-;
using namespace std;
priority_queue<int,vector<int>,greater<int> >pq;
struct Node{
int x,y;
};
struct cmp{
bool operator()(Node a,Node b){
if(a.x==b.x) return a.y> b.y;
return a.x>b.x;
}
}; bool cmp(int a,int b){
return a>b;
}
int dp[][];//第i行状态为j 前i行所能得到方案数
int row[];//第i行的状态
int n,m,num;
int st[];
void init(){
int k=<<m;
num=;
for(int i=;i<k;i++){
if((i&(i<<))==)
st[num++]=i;
}
//处理出 相邻两位不相同的集合
}
int main()
{
#ifndef ONLINE_JUDGE
freopen("in.txt","r",stdin);
#endif
//freopen("out.txt","w",stdout);
int x;
cin>>n>>m;
cle(row);
init();
for(int i=;i<=n;i++){
for(int j=m-;j>=;j--){
cin>>x;
if(x)row[i]+=(<<j);
}
//cout<<row[i]<<endl;
}
cle(dp);
//还得初始化一下第一行
for(int j=;j<num;j++){
dp[][j]=((row[]&st[j])==st[j])?:;
}
for(int i=;i<=n;i++){
for(int j=;j<num;j++){
if((row[i]&st[j])==st[j]){//枚举状态row[i]的子集
for(int k=;k<num;k++){
if(dp[i-][k]&&((st[k]&st[j])==))
dp[i][j]=(dp[i][j]+dp[i-][k])%mod;
}
}
}
}
//再处理最后一行
int ans=;
for(int j=;j<num;j++){
if(dp[n][j])
ans=(ans+dp[n][j])%mod;
}
cout<<ans<<endl;
return ;
}
poj3254Corn Fields的更多相关文章
- poj3254Corn Fields题解
Corn Fields Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 9623 Accepted: 5092 Descr ...
- POJ3254Corn Fields(状态压缩DP入门)
题目链接 题意:一个矩阵里有很多格子,每个格子有两种状态,可以放牧和不可以放牧,可以放牧用1表示,否则用0表示,在这块牧场放牛,要求两个相邻的方格不能同时放牛,即牛与牛不能相邻.问有多少种放牛方案(一 ...
- poj3254Corn Fields(状压)
http://poj.org/problem?id=3254 第一个状压题 思路挺好想 用二进制表示每行的状态 然后递推 用左移 右移来判断是不是01间隔出现 c大神教的 我的代码WA在这个地方了.. ...
- POJ3254Corn Fields(状压DP)
题意: John 有一个豪华的M*N个格子组成的新牧场 他想种美味的玉米 但是有些位置不能种 而且他种地不选择相邻的格子 求所有可能的种地方法 (不种也算一种选择)输入:第一行M和N, 第二行M*N地 ...
- POJ3254Corn Fields——状态压缩dp
题目:http://poj.org/problem?id=3254 1.枚举行: 2.把有影响的“放不放牛”加入参数中,用二进制数表示该位置放不放牛,再用十进制数表示二进制数: 3.优美的预处理lis ...
- POJ3254Corn Fields (状态压缩or插头DP)
Description Farmer John has purchased a lush new rectangular pasture composed of M by N (1 ≤ M ≤ 12; ...
- POJ 3254. Corn Fields 状态压缩DP (入门级)
Corn Fields Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 9806 Accepted: 5185 Descr ...
- 【BZOJ1725】[Usaco2006 Nov]Corn Fields牧场的安排 状压DP
[BZOJ1725][Usaco2006 Nov]Corn Fields牧场的安排 Description Farmer John新买了一块长方形的牧场,这块牧场被划分成M列N行(1<=M< ...
- poj 3254 Corn Fields
http://poj.org/problem?id=3254 Corn Fields Time Limit: 2000MS Memory Limit: 65536K Total Submissio ...
随机推荐
- vue-cli中圣杯布局失效问题
众所周知vue2在前端框架越来越流行,vue-cli这个脚手架工具是我们前端开发必用的,大大的提升了我们的开发效率.当然对于前端小白来说,有些遇到的问题需要和大家分享一下. 移动端页面经常都是需要圣杯 ...
- 浅谈es6 promise
本文是借鉴于ac黄的博客. 接触es6也有几个月了,貌似没有系统的去学习过它,总是用到什么,查查什么.今天就说下es6中的promise对象. 先说说promise解决了什么问题? 写前端的同学都经常 ...
- 13jsp、javaWeb开发模式
13jsp.javaWeb开发模式-2018/07/25 1.jsp jsp实际上就是servlet.jsp=html+java,为用户提供动态内容 不适合编写Java逻辑 2.JSP原理 翻译(生成 ...
- 爬虫之Selenium库
官方文档:https://selenium-python.readthedocs.io/ Selenium:自动化测试工具,支持多种浏览器.爬虫中主要用来解决JavaScript渲染的问题. 一.开始 ...
- 杂文Python
2.文件操作 文件操作的过程:打开文件获得句柄——>操作文件行(遍历等)——>关闭文件 打开文件获得句柄 比较low的方法: f = open("file_path", ...
- Python判断字符串是否全是字母或数字
str.isnumeric(): True if 只包含数字:otherwise False.注意:此函数只能用于unicode string str.isdigit(): True if 只包含数字 ...
- python爬虫25 | 爬取下来的数据怎么保存? CSV 了解一下
大家好 我是小帅b 是一个练习时长两年半的练习生 喜欢 唱! 跳! rap! 篮球! 敲代码! 装逼! 不好意思 我又走错片场了 接下来的几篇文章 小帅b将告诉你 如何将你爬取到的数据保存下来 有文本 ...
- 洛谷 2176 [USACO14FEB]路障Roadblock
[题意概述] 修改图中任一一条边的边权,使其加倍,问怎样使修改后图中的1~n的最短路最大.输出最短路的增量. [题解] 先跑一遍dijkstra求出1~n的路径长度,记录下经过的边.枚举这些边进行修改 ...
- JUnit 深入
Fixture 何谓 Fixture ?它是指在执行一个或者多个测试方法时需要的一系列公共资源或者数据,例如测试环境,测试数据等等.在编写单元测试的过程中,您会发现在大部分的测试方法在进行真正的测试之 ...
- 返回通知&异常通知&环绕通知
[返回通知] LoggingAspect.java: @Aspect @Component public class LoggingAspect { /* * 在方法正常执行后执行的通知叫返回通知 * ...