POJ 3254 Corn Fields(状压DP)
| Time Limit: 2000MS | Memory Limit: 65536K | |
| Total Submissions: 13732 | Accepted: 7216 |
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.
题目链接:POJ 3254
做的第二道状态压缩DP题,按自己前一道的思路写的,写的比较慢但是过程感觉很清晰,修改了很多次,最后0MS过了还是不错的
这题跟做过的其他入门题有一点不同,就是他不仅要不相邻(可以同一列),因此第$i$行怎么放不影响第$i-2$,$i+2$行,因此就不需要用或进行状态叠加,显然状压基本法第一步就是初始化,把第一行的数据初始化,
所以为了方便和快速遍历所有本身不相邻的状态,先预处理把状态存到fit里,有tot个,然后枚举每一个状态看是否能放到这一行草地上,我就偷个懒直接用bitset for一遍判断——若当前不是草地但状态里却有一个牛显然这就不合法不能放进草地,否则$dp[0][当前十进制状态]=1$。
然后按照基本步骤先遍历每一行,再枚举上一行$k$和这一行的状态$j$,若上一行$j$可以放草地且这一行$k$也可以放草地且$k$与$j$不冲突则$dp[i][j]=dp[i][j]+dp[i-1][k]$
最后还是枚举存好的状态(不需要枚举1<<m次因为最后一行肯定也是合法的一定全在保存好的fit里面)把状态相加,最后想了一下为什么不是把每一行的每一种状态都加起来呢?因为你状态从$dp[i-1][k]$转移到$dp[i][j]$用的是+=已经把前面的状态算上去了。
最后送一组测试数据
3 3
1 1 1
1 0 1
1 1 1
答案是47
代码:
#include <stdio.h>
#include <iostream>
#include <algorithm>
#include <cstdlib>
#include <sstream>
#include <cstring>
#include <bitset>
#include <string>
#include <deque>
#include <stack>
#include <cmath>
#include <queue>
#include <set>
#include <map>
using namespace std;
#define INF 0x3f3f3f3f
#define CLR(arr,val) memset(arr,val,sizeof(arr))
#define LC(x) (x<<1)
#define RC(x) ((x<<1)+1)
#define MID(x,y) ((x+y)>>1)
typedef pair<int,int> pii;
typedef long long LL;
const double PI=acos(-1.0);
const int N=14;
const int mod=100000000;
int pos[N][N],Grass[N];
int dp[N][1<<N];
int fit[1<<N];
int tot,n,m,R; bool check(const int &a,const int &b)
{
return (a&b)==0;
}
bool check_grass(const int &grass,const int &v)
{
bitset<N> bg=grass,bv=v;
for (int i=0; i<N; ++i)
if(bg[i]==0&&bv[i]==1)
return false;
return true;
}
void init()
{
CLR(dp,0);
CLR(Grass,0); tot=0;
R=1<<m;
for (int i=0; i<R; ++i)
if(check(i,i<<1))
fit[tot++]=i;
}
int main(void)
{
int i,j,k;
while (~scanf("%d%d",&n,&m))
{
init();
for (i=0; i<n; ++i)
{
int bis=0;
for (j=0; j<m; ++j)
{
scanf("%d",&pos[i][j]);
bis=(bis<<1)+pos[i][j];
}
Grass[i]=bis;
} for (i=0; i<tot; ++i)
if(check_grass(Grass[0],fit[i]))
dp[0][fit[i]]=1; for (i=1; i<n; ++i)
{
for (j=0; j<tot; ++j)//第i行
{
if(check_grass(Grass[i],fit[j]))
{
for (k=0; k<tot; ++k)//枚举第i-1行
{
if(check_grass(Grass[i-1],fit[k])&&check(fit[k],fit[j]))//i-1行可放且与第i行不冲突
dp[i][fit[j]]+=dp[i-1][fit[k]];
}
}
}
}
int cnt=0;
for (i=0; i<tot; ++i)
{
cnt+=dp[n-1][fit[i]];
cnt%=mod;
}
printf("%d\n",cnt);
}
return 0;
}
POJ 3254 Corn Fields(状压DP)的更多相关文章
- POJ 3254 - Corn Fields - [状压DP水题]
题目链接:http://poj.org/problem?id=3254 Time Limit: 2000MS Memory Limit: 65536K Description Farmer John ...
- POJ 3254 Corn Fields (状压dp)
题目链接:http://poj.org/problem?id=3254 给你n*m的菜地,其中1是可以种菜的,而菜与菜之间不能相邻.问有多少种情况. 状压dp入门题,将可以种菜的状态用一个数的二进制表 ...
- [ An Ac a Day ^_^ ] POJ 3254 Corn Fields 状压dp
题意: 有一块n*m的土地 0代表不肥沃不可以放牛 1代表肥沃可以放牛 且相邻的草地不能同时放牛 问最多有多少种放牛的方法并对1e8取模 思路: 典型的状压dp 能状态压缩 能状态转移 能状态压缩的题 ...
- Poj - 3254 Corn Fields (状压DP)(入门)
题目链接:https://vjudge.net/contest/224636#problem/G 转载于:https://blog.csdn.net/harrypoirot/article/detai ...
- poj 3254 Corn Fields 状压dp入门
题目链接 题意 在\(M\times N\)的\(0,1\)格子上放东西,只有标记为\(1\)的格子可以放东西,且相邻的格子不能同时放东西.问有多少种放法. 思路 参考:swallowblank. \ ...
- POJ 1684 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 (状压入门)
Farmer John has purchased a lush new rectangular pasture composed of M by N (1 ≤ M≤ 12; 1 ≤ N ≤ 12) ...
- 【POJ3254】Corn Fields 状压DP第一次
!!!!!!! 第一次学状压DP,其实就是运用位运算来实现一些比较,挺神奇的.. 为什么要发“!!!”因为!x&y和!(x&y)..感受一下.. #include <iostre ...
- P1879 [USACO06NOV]玉米田Corn Fields 状压dp/插头dp
正解:状压dp/插头dp 解题报告: 链接! ……我真的太菜了……我以为一个小时前要搞完的题目调错误调了一个小时……90分到100我差不多搞了一个小时…… 然后这题还是做过的……就很气,觉得确实是要搞 ...
- [USACO06NOV]玉米田Corn Fields 状压DP
题面: 农场主John新买了一块长方形的新牧场,这块牧场被划分成M行N列(1 ≤ M ≤ 12; 1 ≤ N ≤ 12),每一格都是一块正方形的土地.John打算在牧场上的某几格里种上美味的草,供他的 ...
随机推荐
- [Android Pro] Android 打包流程
Android 打包流程: 官网地址:http://developer.android.com/tools/building/index.html 具体的打包步骤如下: 1:生成R.java类文件:E ...
- 【读书笔记】读《JavaScript设计模式》之桥接模式
桥接模式(Bridge)将抽象部分与它的实现部分分离,使它们都可以独立地变化. 一.使用场景 使用场景一:事件监控 对于前端而言,最典型的使用场景——事件监控.如—— addEvent(element ...
- ubuntu安装vmware tools
1.选择虚拟机菜单栏--安装VMware tools 选择你装虚拟机下的目录,点击 Linux.iso 再重新点击安装VMware Tools 这样会弹出一个包VMwareTools-9.6.0-1 ...
- php 克隆和引用类
/*class Ren { public $name; public $sex; function __construct($n,$s) { $this->name=$n; $this-> ...
- 有哪些关于 Android 开发的博客值得订阅?
链接:http://www.zhihu.com/question/19788650/answer/60771437来源:知乎 Google 官方[Android Developers Blog](An ...
- (二)WebRTC手记之框架与接口
转自:http://www.cnblogs.com/fangkm/p/4370492.html 转载请注明出处:http://www.cnblogs.com/fangkm/p/4370492.html ...
- 学会使用Chromium中的LOG
转自:http://blog.csdn.net/kuerjinjin/article/details/43937345 简介 众所周知chromium项目无比巨大,想去快速的了解,调试并添加自己想要的 ...
- Java POI Word 写文档
package apache.poi; import java.io.ByteArrayInputStream;import java.io.ByteArrayOutputStream;import ...
- HTTP基础01--web与互联网基础
WWW构建技术: 把SGML(标准通用标记语言)作为页面文本标记的HTML(超文本标记语言): 作为文档传递协议的HTTP(超文本传输协议): //严谨应该称为“超文本转移协议”: 指定文档所在地址 ...
- c# 使用正则表达式 提取章节小说正文全本篇
这一节主要内容是使用正则表达式提取网站的正文,主要面向于小说章节网站.其中涉及到一些其他知识点,比如异步读取.异步流写入等,代码中都会有详细的注解.现在流行的网络文学都是每日一更或几更,没有一个统一的 ...