【POJ3254】Corn Fields 状压DP第一次
!!!!!!!
第一次学状压DP,其实就是运用位运算来实现一些比较,挺神奇的。。
为什么要发“!!!”因为!x&y和!(x&y)。。感受一下。。
#include <iostream>
#include <cstdio>
#include <cstring>
#define N 13
#define M 1<<13
#define MOD 1000000000
using namespace std;
int n,m,t,ans;
int so[M],map[M];
int dp[N][M];
bool ju1(int x) {return x&(x<<);}
bool ju2(int x,int y) {return map[x]&so[y];}
int main()
{
while (~scanf("%d%d",&n,&m))
{
memset(map,,sizeof(map));
memset(so,,sizeof(so));
memset(dp,,sizeof(dp));
ans=;
t=;
for (int i=;i<=n;i++)
for (int j=;j<=m;j++)
{
int x;
scanf("%d",&x);
if (x==) map[i]+=(<<(j-));
}
for (int i=;i<(<<m);i++)
if (!ju1(i)) so[t++]=i;
for (int i=;i<t;i++)
if (!ju2(,i)) dp[][i]=;
for (int i=;i<=n;i++)
for (int j=;j<t;j++)
{
if (ju2(i,j)) continue;
for (int k=;k<t;k++)
{
if (ju2(i-,k)) continue;
if (!(so[j]&so[k])) dp[i][j]+=dp[i-][k];
}
}
//for (int i=1;i<=n;i++)
//printf("%d\n",t);
for (int j=;j<t;j++)
ans=(ans+dp[n][j])%MOD;
printf("%d\n",ans);
}
return ;
}
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
【POJ3254】Corn Fields 状压DP第一次的更多相关文章
- 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 - [状压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 能状态压缩 能状态转移 能状态压缩的题 ...
- 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\) . 同时多记录一个每一列中的 ...
- [poj3254]Corn Fields_状压dp
Corn Fields poj3254 题目大意:给你一个n*m的地,每一块地可以种或不种,两块种过的地不能挨着,可以一块都不种,问所有的种地方案数. 注释:读入用0和1,1<=n,m<= ...
- Poj - 3254 Corn Fields (状压DP)(入门)
题目链接:https://vjudge.net/contest/224636#problem/G 转载于:https://blog.csdn.net/harrypoirot/article/detai ...
随机推荐
- jquery实现简单瀑布流布局(续):图片懒加载
# jquery实现简单瀑布流布局(续):图片懒加载 这篇文章是jquery实现简单瀑布流布局思想的小小扩展.代码基于前作的代码继续完善. 图片懒加载就是符合某些条件时才触发图片的加载.最常见的具体表 ...
- windows安装mysql5.7
1下载mysql http://dev.mysql.com/downloads/mysql/2解压后,新建一个data文件夹,复制my-default.ini,并改名为my.ini,添加下面内容[cl ...
- Bash 会清空从父进程继承来的 OLDPWD
即便 Bash 没有从父进程继承任何的环境变量,Bash 自己也会创建三个环境变量,分别是: $ env -i bash -c export declare -x OLDPWD declare -x ...
- tyvj1106 登山
背景 在很久很久以前,有一个动物村庄,那里是猪的乐园(^_^),村民们勤劳.勇敢.善良.团结…… 不过有一天,最小的小小猪生病了,而这种病是极其罕见的,因此大家都没有储存这种药物.所以晴 ...
- PHP中如何在数组中随机抽取n个数据的值 - array_rand()?
PHP中如何在数组中随机抽取n个数据的值? 最佳答案 array_rand() 在你想从数组中取出一个或多个随机的单元时相当有用.它接受 input 作为输入数组和一个可选的参数 num_req,指明 ...
- Asp.Net Core--简单的授权
翻译如下: 在MVC中授权通过控制AuthorizeAttribute属性及其各种参数.在最简单的应用AuthorizeAttribute属性控制器或行动限制访问控制器或操作任何身份验证的用户. 例如 ...
- css动画 animation
今天用css做了一个简单的三角上下移动的一个小动画,说白了就是在改变该物体的height值.除了这个方法,还可以用js. 一.在用css写动画时,一定要记住兼容性问题.如何解决该兼容性?在前面加内核前 ...
- JsonFormatter PrettyPrint
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.T ...
- oracle查询某一个字段的数量总和
select count(*) from (select count(*) from 表名称 group by 多种数据量 ) 表名; 举个栗子: 比如说我有一个数据类型的字段, 里面有很多种的数据类 ...
- ASP.NET正则表达式(URL,Email)
public static bool IsUrl(this string str) { if (str.IsNullOrEmpty()) return fal ...