POJ 3254 - Corn Fields - [状压DP水题]
题目链接:http://poj.org/problem?id=3254
Time Limit: 2000MS Memory Limit: 65536K
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.
题解:
怎么说呢,类似的状压DP题解写多了真的挺无聊的,如果你已经撸完了:
http://www.cnblogs.com/dilthey/p/7623028.html
http://www.cnblogs.com/dilthey/p/7604432.html
这两道题目的话,再来看本题,真的是很水的,所以就懒得写题解了,反正就是差不多的思路;
AC代码:
#include<cstdio>
#include<cstring>
int m,n,mp[];
int dp[][<<];
int main()
{
while(scanf("%d%d",&m,&n)!=EOF)//m行n列
{
for(int i=,tmp;i<=m;i++)
{
mp[i]=;
for(int j=;j<=n;j++)
{
scanf("%d",&tmp);
tmp=!tmp;
mp[i]|=tmp;
if(j!=n) mp[i]<<=;
}
} memset(dp,,sizeof(dp));
for(int i=;i<(<<n);i++)//初始化第一行
{
if( i&(i<<) || i&mp[] ) continue;
dp[][i]=;
}
for(int r=;r<=m;r++)//遍历第2~m行
{
for(int i=;i<(<<n);i++)//枚举第r行状态
{
if( i&(i<<) || i&mp[r] ) continue;
for(int j=;j<(<<n);j++)//枚举第r-1行状态
{
if( j&(j<<) || j&mp[r-] || i&j ) continue;
dp[r][i]+=dp[r-][j];
}
}
} int ans=;
for(int i=;i<(<<n);i++)
{
if( i&(i<<) || i&mp[m] ) continue;
ans+=dp[m][i];
}
printf("%d\n",ans%);
}
}
PS.哦对,有一点倒是值得提醒,需要对100000000取模,因为忘记了这个WA了一发,羞愧脸。
POJ 3254 - Corn Fields - [状压DP水题]的更多相关文章
- 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打算在牧场上的某几格里种上美味的草,供他的 ...
随机推荐
- c#系统消息类封装
今天封装了一个返回json的消息类 using System; using System.Collections.Generic; using System.Linq; using System.Te ...
- 【高级算法】遗传算法解决3SAT问题(C++实现)
转载请注明出处:http://blog.csdn.net/zhoubin1992/article/details/46910079 1 SAT问题描写叙述 命题逻辑中合取范式 (CNF) 的可满足性问 ...
- association 的使用
<resultMap id="wmsTaskMap" type="WmsTask"> <id column="ID" jd ...
- SpringMVC由浅入深day02_10拦截器
10 拦截器 Spring Web MVC 的处理器拦截器类似于Servlet 开发中的过滤器Filter,用于对处理器进行预处理和后处理. 10.1 拦截定义 定义拦截器,实现HandlerInte ...
- 有人在群里问关于SQL表组合数据问题
他的问题如下 如此我建表如下: 如果想根据用户进行分组后 又要显示所属门店在同一个字段中的话,这里需要用group_concat来显示 同时关联的时候可用find_in_set来处理 我设计的SQL如 ...
- ios开发之--新手引导页图片适配方案
1,图片适配,最早以前是自己命名规范,例如@1x,@2x,@3x等,3套图基本上就够用了 2,在iPhone X之后,需要适配的图就多了,因为分辨率增多了,屏幕尺寸也增多了 3,尺寸 :640*960 ...
- JavaScript之 for...in
for-in 可以用来枚举对象的属性,还有数组的索引,用法: 枚举对象属性 var o={name:'a',age:25,sex:'male'} for(var each in o){ console ...
- 使用node中的iconv-lite实现对“gbk”格式的转码
在window中,gbk和utf-8是最常见的两种格式,但是我们在显示的时候往往需要将GBK转换为UTF-8,我现在有一个同步读取文件的操作: const fs = require('fs'); co ...
- 解析pdb文件得到未导出变量地址(转)
程序要用到dbghelp.dll中的一些函数 http://msdn.microsoft.com/en-us/library/ms679291%28VS.85%29.aspx 要自己下载系统对应的符号 ...
- JS 验证URL
var strVal = $("#urlText").val(); var Expression = "^((https|http|ftp|rtsp|mms)?://)& ...