POJ 1684 Corn Fields(状压dp)
描述
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.
输入
Line 1: Two space-separated integers: M and N
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)
输出
Line 1: One integer: the number of ways that FJ can choose the squares modulo 100,000,000.
样例输入
2 3
1 1 1
0 1 0
样例输出
9
提示
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<stdio.h>
#include<string.h>
using namespace std; const int mod=1e9; int dp[][],state[],cur[];
int n,m,k,tot;
int main()
{
while(scanf("%d%d",&m,&n)!=EOF)
{
tot=;
for(int i=;i<(<<n);i++)///列的所有可行状态
if(!(i&(i<<)))///没有连续两个1
state[++tot]=i; for(int i=;i<=m;i++)
for(int j=;j<=n;j++)
{
scanf("%d",&k);
if(!k)cur[i]+=<<(n-j);///第i行不可行置1,用于判断不可行状态
} for(int i=;i<=tot;i++)
if(!(state[i]&cur[]))
dp[][i]=;
for(int i=;i<=m;i++)
{
for(int j=;j<=tot;j++)///枚举当前状态
{
if(state[j]&cur[i])continue;///当前状态不可行
for(int k=;k<=tot;k++)///枚举前一个状态
{
if(state[k]&cur[i-])continue;///当前前一状态不可行
if(state[j]&state[k])continue;///当前状态和前一状态不可行
dp[i][j]=(dp[i][j]+dp[i-][k])%mod;///当前状态可以从前一状态的K状态转移过来
}
}
}
int ans=;
for(int i=;i<=tot;i++)
ans=(ans+dp[m][i])%mod;
printf("%d\n",ans);
}
return ;
}
POJ 1684 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. \ ...
- 【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打算在牧场上的某几格里种上美味的草,供他的 ...
- [USACO06NOV]玉米田Corn Fields (状压$dp$)
题目链接 Solution 状压 \(dp\) . \(f[i][j][k]\) 代表前 \(i\) 列中 , 已经安置 \(j\) 块草皮,且最后一位状态为 \(k\) . 同时多记录一个每一列中的 ...
随机推荐
- mingw编译ffmpeg 错误:Unknown option "--enable-memalign-hack"
据说mingw编译ffmpeg的话需要添加 --enable-memalign-hack 开关 但如果源码是最新版比如:ffmpeg4.0.2 的话 好像已经禁用了该开关. “我可以确认新的ffmpe ...
- opencv-3.3安装记录-ubuntu 14.04
这个二逼问题不会是最后一次. ipcv-****.tar.gz 这个文件在cmake的时候会卡住,这里先下载这个文件,大概38M,放到.cache/ippcv目录下就可以了.貌似还需要改下名字. 就可 ...
- 记一个pg连接数过多的异常
java中使用数据库连接池,如果在部署的时候,操作不慎,可能出现典型的连接池爆满的问题 org.postgresql.util.PSQLException: FATAL: sorry, too man ...
- docker的完整解决方案2
这个解决方案很简单 使用docker自带的swarm 首先初始化集群 docker swarm init 然后其余节点加入集群,这个就不说,太简单了 集群初始化后,可以查看下集群状态 docker n ...
- RabbitMQ系列教程之五:主题(Topic)(转载)
RabbitMQ系列教程之五:主题(Topic) (本实例都是使用的Net的客户端,使用C#编写),说明,中文方括号[]表示名词. 在上一个教程中,我们改进了我们的日志记录系统. 没有使用只能够进行虚 ...
- 删除oracle数据库用户
手工删除ORACLE数据库用户时常会出现会话进程仍在使用导致删除失败的情况.需要查询会话并将会话删除才能成功将数据库用户删除,比较不方便. 适用场景 自动删除oracle数据库用户 基本知识 orac ...
- Structs复习 Result第二部分
1.动态结果 2.web.xml <?xml version="1.0" encoding="UTF-8"?> <web-app versio ...
- 一个漂亮的 PlaceHolder
预览: 不知道为什么下面这个窗口中的 JavaScript 代码没有运行-_-||,想看实际效果就把下面的代码保存下来打开看吧. 代码: <!DOCTYPE HTML> <html ...
- unity 三种注入示例
/* * 演示Unity 注入 * */ using Microsoft.Practices.Unity; using System; namespace Unity.Property.Inject ...
- Java NIO Tutorial
Java NIO Tutorial Jakob JenkovLast update: 2014-06-25