【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 ...
随机推荐
- Nancy总结(三)Nancy资料介绍
Nancy 是一个轻量级用于构建基于 HTTP 的 Web 服务,可以基于 .NET 和 Mono 平台构建轻量级基于 HTTP 的Web 服务.它更多的是借鉴了Ruby的一些特性和Ruby的MVC ...
- webstorm常用快捷键
常用快捷键—Webstorm入门 提高代码编写效率,离不开快捷键的使用,Webstorm拥有丰富的代码快速编辑功能,你可以自由配置功能快捷键. 快捷键配置 点击“File”-> “setting ...
- 【转】Controllers and Routers in ASP.NET MVC 3
Controllers and Routers in ASP.NET MVC 3 ambilykk, 3 May 2011 CPOL 4.79 (23 votes) Rate: vote 1vote ...
- [nosql之缓存memcache]安装篇LInux for Windows
首先呢在PHP开发的过程中会用到很多缓存服务,从而提升访问质量或者临时存储一些数据. 优点 结构简单,读取速度快,易于维护.还有一些特性memcache redis mongodb都可以用来做为缓存用 ...
- mysql [ERROR] Fatal error: Can't open and lock privilege tables: Table 'mysql.host' doesn't exist (转载)
mysql报错Fatal error: Can't open and lock privilege tables: Table 'mysql.host' doesn't exist 2013-11-2 ...
- 网址前面的icon
shortcut icon和icon代码之间究竟有何区别呢.下面介绍一下 语句一:<link rel="shortcut icon" href="favicon ...
- 曲面之美:三星 S6 Edge+
这些年安卓手机阵营一直拼得又激烈又惨烈,从拼配置,拼性能,拼性价比,到拼颜值拼情怀,拼得用户也都麻木了. 尤其是我这样的用户,不喜欢墨守成规,你配置高又如何,同样价钱的配置都差不多. 我想看不一样的东 ...
- iOS On-Demand Resources简单理解
ios9引入了一个新功能,On-Demand Resources,它是app thinning 的一部分.这个机能简单的说,就是在下载app的时候,app中包含的不重要资源不下载,等到需要时,在由系统 ...
- Path之Data属性语法A命令
<Path Width="300" Height="300" Fill="Red" Data="M 100,100 L 10 ...
- Scrapy002-框架安装
Scrapy002-框架安装 @(Spider)[POSTS] 这里我使用的是Python3.x, 1. Ubuntu上安装 在安装Scrapy之前,首先需要确定环境和版本: Ubuntu16.04 ...