POJ 3254 状压DP(基础题)
Time Limit: 2000MS | Memory Limit: 65536K | |
Total Submissions: 17749 | Accepted: 9342 |
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的矩阵,上面1表示肥沃的土地,0表示贫瘠的土地,然后要让牛必须站在肥沃的土地上,又不能相邻(可以整个都不站牛)
题解:那就用二进制来表示整个土地的样子,状态转移是dp[i][j] = sum(dp[i-1][k]) 表示第i行的站法是由i-1行所有可能的站法并且两行不能冲突的和
技巧:
1.判断一个数字是否有相邻的两个1 :x&(x<<1)=0,则表示没有,否则有。
2.两行是否有上下相邻的:若x&y=0,则没有,否则有。
3. 判断当前站位是否满足土地条件:若maps[i]=maps[i] | x,则x情况下的站位满足土地条件。
代码:
//#include "bits/stdc++.h"
#include "cstdio"
#include "map"
#include "set"
#include "cmath"
#include "queue"
#include "vector"
#include "string"
#include "cstring"
#include "time.h"
#include "iostream"
#include "stdlib.h"
#include "algorithm"
#define db double
#define ll long long
//#define vec vector<ll>
#define Mt vector<vec>
#define ci(x) scanf("%d",&x)
#define cd(x) scanf("%lf",&x)
#define cl(x) scanf("%lld",&x)
#define pi(x) printf("%d\n",x)
#define pd(x) printf("%f\n",x)
#define pl(x) printf("%lld\n",x)
#define rep(i, x, y) for(int i=x;i<=y;i++)
const int N = 1e5 + ;
const int mod = 1e9 + ;
const int MOD = mod - ;
const db eps = 1e-;
const db PI = acos(-1.0);
using namespace std;
int maps[],ok[N];
int f[][N];
int n,m;
bool cal1(int i){
return i&(i<<);//检查相邻的1
}
bool cal2(int x,int y){
return maps[x]==(maps[x]|ok[y]);//检查合理的站位与土地条件是否冲突
}
int main()
{
while(scanf("%d%d",&n,&m)==&&n!=)
{
memset(maps,, sizeof(maps));
memset(f,, sizeof(f));
for(int i=;i<n;i++)
{
for(int j=;j<m;j++){
int x;
ci(x);
if(x!=) maps[i]+=(<<j);//更新第i行的土地
}
}
int cnt=;
for(int i=;i<(<<m);i++){
if(!cal1(i)) ok[cnt++]=i;//保存合理的站位
}
for(int i=;i<cnt;i++){
if(cal2(,i)) f[][i]=;//处理出第一行的合理站位
}
ll ans=;
for(int i=;i<n;i++){
for(int j=;j<cnt;j++){
if(!cal2(i,j)) continue;//检查对于第i行是否合理
for(int k=;k<cnt;k++){//检查与i-1行是否冲突
if(!(ok[j]&ok[k])) f[i][j]+=f[i-][k];
}
}
}
for(int i=;i<cnt;i++) ans+=f[n-][i],ans%=;
pl(ans);
}
return ;
}
POJ 3254 状压DP(基础题)的更多相关文章
- poj 3254 状压dp入门题
1.poj 3254 Corn Fields 状态压缩dp入门题 2.总结:二进制实在巧妙,以前从来没想过可以这样用. 题意:n行m列,1表示肥沃,0表示贫瘠,把牛放在肥沃处,要求所有牛不能相 ...
- POJ 3254 (状压DP) Corn Fields
基础的状压DP,因为是将状态压缩到一个整数中,所以会涉及到很多比较巧妙的位运算. 我们可以先把输入中每行的01压缩成一个整数. 判断一个状态是否有相邻1: 如果 x & (x << ...
- POJ 3254 状压DP
题目大意: 一个农民有一片n行m列 的农场 n和m 范围[1,12] 对于每一块土地 ,1代表可以种地,0代表不能种. 因为农夫要种草喂牛,牛吃草不能挨着,所以农夫种菜的每一块都不能有公共边. ...
- Corn Fields(POJ 3254状压dp)
题意: n*m网格1能放0不能放 放的格子不能相邻 求一共多少种可放的方案. 分析: dp[i][j]第i行可行状态j的的最大方案数,枚举当前行和前一行的所有状态转移就行了(不放牛也算一种情况) #i ...
- POJ 3254 - Corn Fields - [状压DP水题]
题目链接:http://poj.org/problem?id=3254 Time Limit: 2000MS Memory Limit: 65536K Description Farmer John ...
- hdu 3254 (状压DP) Corn Fields
poj 3254 n乘m的矩阵,1表示这块区域可以放牛,0,表示不能,而且不能在相邻的(包括上下相邻)两个区域放牛,问有多少种放牛的方法,全部不放也是一种方法. 对于每块可以放牛的区域,有放或者不放两 ...
- 【bzoj1087】【互不侵犯King】状压dp裸题(浅尝ACM-D)
[pixiv] https://www.pixiv.net/member_illust.php?mode=medium&illust_id=54329606 向大(hei)佬(e)势力学(di ...
- 【bzoj3195】【 [Jxoi2012]奇怪的道路】另类压缩的状压dp好题
(上不了p站我要死了) 啊啊,其实想清楚了还是挺简单的. Description 小宇从历史书上了解到一个古老的文明.这个文明在各个方面高度发达,交通方面也不例外.考古学家已经知道,这个文明在全盛时期 ...
- 7月15日考试 题解(链表+状压DP+思维题)
前言:蒟蒻太弱了,全打的暴力QAQ. --------------------- T1 小Z的求和 题目大意:求$\sum\limits_{i=1}^n \sum\limits_{j=i}^n kth ...
随机推荐
- Python元组类型、字典类型及常用操作
一.元组类型 1.用途 记录多个值,当多个值没有改的需求,此时用元组更合适,Python的元组与列表类似,不同之处在于元组的元素不能修改. 2.定义方式 在()内用逗号分隔开多个任意类型的值 t=(1 ...
- 零基础逆向工程35_Win32_09_临界区_CRITICAL_SECTION结构
1 引入 为什么会存在临界区这中机制呢?是为多线程同时访问全局变量而引入的.也就是上一篇帖子的末尾流出的问题程序的解决办法. 看懂了上面的,那么我们再罗嗦总结一下: 1.多线程访问全局变量时,存在线程 ...
- Java设计模式—中介者模式
中介者模式是一种并不常用的模式,在此简单阐述阐述. 定义:用一个中介者对象封装一系列的对象交互,中介者使各对象不需要显示地相互作用,从而使耦合松散,而且可以独立地改变它们之间的交互. 类型:行为类模式 ...
- Static 用法
1.Static关键字含意:static译文是静态的,静止的,因此使用 static 修饰符声明属于类型本身而不是属于特定对象(new创建的对象)的静态成员. 2.修饰使用范围 static 修饰符可 ...
- sharepoint知识点总结
{ users.Add(value.User); } else { SPGroup group = web.Groups.GetByID(value.LookupId); groups.Add(gro ...
- SharePoint 2010 技术参数(整理)
今天整理一些 SharePoint 2010 的技术参数,其内容都来自 SharePoint-Sandbox 网站. 有些参数值是硬性的,比如列表单条记录的尺寸:而有些是为了使用和性能考虑的推荐值. ...
- 项目01-flume、kafka与hdfs日志流转
项目01-flume.kafka与hdfs日志流转 1.启动kafka集群 $>xkafka.sh start 3.创建kafka主题 kafka-topics.sh --zookeeper s ...
- php中的curl_multi的应用(php多进程)
相信许多人对PHP手册中语焉不详的curl_multi一族的函数头疼不已,它们文档少,给的例子 更是简单的让你无从借鉴,我也曾经找了许多网页,都没见一个完整的应用例子. curl_multi_add_ ...
- PHP:return与exit的区别
return 虽然返回数据,并且不再往下执行,但是它会返回执行上一步的操作,所以return的只是当前function而不会影响其他function的执行: exti 是完全将整个项 ...
- node实现爬虫
node实现获取到豆瓣电影排行榜页面. 准备工作: 1.新建一个文件夹node 在当前文件夹中打开cmd 下载 npm install 初始化 npm init(注意一下:如果你的npm init没有 ...