【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 ...
随机推荐
- 硕士研究生入学考试复试试卷答案.tex
%该模板用于数学答题 \documentclass[UTF8]{ctexart}%[中文编码 UTF8] \usepackage{fancyhdr}%{页眉页脚页码} \pagestyle{fancy ...
- OpenGL Common Mistakes
https://www.opengl.org/wiki/Common_Mistakes Do not use constructors/destructors to initialize/destro ...
- tyvj1106 登山
背景 在很久很久以前,有一个动物村庄,那里是猪的乐园(^_^),村民们勤劳.勇敢.善良.团结…… 不过有一天,最小的小小猪生病了,而这种病是极其罕见的,因此大家都没有储存这种药物.所以晴 ...
- 【html】学习记录-拖放(drag and drop)
目的:实现拖动目标并放置到指定区域. 使元素可拖动,涉及到元素的全局属性draggable <img draggable="true" /> 即img元素设置为可拖 ...
- python 内置&&递归
lambda 优点: 1:可以简单使用一个脚本来替代我们的函数 2:不用考虑命名的问题 3:简化代码的可读性,不用跳转到def了,省去这样的步骤 内置函数:bif filter:过滤器 map:映射 ...
- mysql数据表拷贝
select * into 目标表名 from 源表名 insert into 目标表名(fld1, fld2) select fld1, 5 from 源表名 以上两句都是将 源表 的数据插入到 目 ...
- MVC学习笔记
7 天玩转 ASP.NET MVC 阅读笔记 1.Web Form的问题 1.响应慢,web控件转化成html需要时间 2.带宽消耗,页面保存web控件的状态(viewstate) 3.由web控件生 ...
- grep 正则表达式
本文转自:http://www.jb51.net/article/31207.htm 正则表达式只是一种表示法,只要工具支持这种表示法, 那么该工具就可以处理正则表达式的字符串.vim.grep.aw ...
- 图像处理中的matlab使用
图像的矩阵表示 类和图像类型 虽然使用的是整数坐标, 但 MATLAB 中的像素值(亮度)并未限制为整数. 表 1-1 列出了 MATLAB 和图像处理工具箱为描述像素值而支持的各种类. 表中的前 8 ...
- python中文字符乱码(GB2312,GBK,GB18030相关的问题)
转自博主 crifan http://againinput4.blog.163.com/blog/static/1727994912011111011432810/ 在玩wordpress的一个博客搬 ...