POJ3254Corn Fields (状态压缩or插头DP)
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
之前做了一下“铺地砖”的题,然后看别人用插头DP做,代码很简洁,于是也打算学习一下插头DP。
这个题应该是比较基础,反正1A了,爽歪歪。
不过第一次写的可能有些丑。
#include<cstdio>
#include<cstdlib>
#include<iostream>
#include<cmath>
using namespace std;
const int Mod=;
int dp[][<<],ans,a[][],m,n;
bool check(int u,int x,int y)
{
for(int i=;i<=m;i++) if((x&(<<(i-)))&&(x&(<<(i-)))) return false;
for(int i=;i<=m;i++) if((y&(<<(i-)))&&(y&(<<(i-)))) return false;
for(int i=;i<=m;i++) if(!a[u-][i]&&(x&(<<(i-)))) return false;
for(int i=;i<=m;i++) if(!a[u][i]&&(y&(<<(i-)))) return false;
for(int i=;i<=m;i++) if((x&(<<(i-)))&&(y&(<<(i-)))) return false;
return true;
}
int main()
{
int i,j,k;
while(~scanf("%d%d",&n,&m)){
for(i=;i<=n;i++)
for(j=m;j>=;j--)
scanf("%d",&a[i][j]);
dp[][]=;
for(i=;i<=n;i++){
for(j=;j<<<m;j++)
for(k=;k<<<m;k++)
if(check(i,j,k))
dp[i][k]=(dp[i][k]+dp[i-][j])%Mod;
}
for(i=;i<<<m;i++) ans=(ans+dp[n][i])%Mod;
printf("%d\n",ans);
}
return ;
}
POJ3254Corn Fields (状态压缩or插头DP)的更多相关文章
- POJ3254Corn Fields(状态压缩DP入门)
题目链接 题意:一个矩阵里有很多格子,每个格子有两种状态,可以放牧和不可以放牧,可以放牧用1表示,否则用0表示,在这块牧场放牛,要求两个相邻的方格不能同时放牛,即牛与牛不能相邻.问有多少种放牛方案(一 ...
- POJ2411Mondriaan's Dream(DP+状态压缩 or 插头DP)
问题: Squares and rectangles fascinated the famous Dutch painter Piet Mondriaan. One night, after prod ...
- POJ3254Corn Fields——状态压缩dp
题目:http://poj.org/problem?id=3254 1.枚举行: 2.把有影响的“放不放牛”加入参数中,用二进制数表示该位置放不放牛,再用十进制数表示二进制数: 3.优美的预处理lis ...
- POJ 3254. Corn Fields 状态压缩DP (入门级)
Corn Fields Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 9806 Accepted: 5185 Descr ...
- POJ 3254 Corn Fields(状态压缩DP)
Corn Fields Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 4739 Accepted: 2506 Descr ...
- poj Corn Fields 状态压缩dp。
Corn Fields Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 5763 Accepted: 3052 Descr ...
- POJ3254 - Corn Fields(状态压缩DP)
题目大意 给定一个N*M大小的土地,土地有肥沃和贫瘠之分(每个单位土地用0,1来表示贫瘠和肥沃),要求你在肥沃的单位土地上种玉米,如果在某个单位土地上种了玉米,那么与它相邻的四个单位土地是不允许种玉米 ...
- POJ 3254 Corn Fields (状态压缩DP)
题意:在由方格组成的矩形里面种草,相邻方格不能都种草,有障碍的地方不能种草,问有多少种种草方案(不种也算一种方案). 分析:方格边长范围只有12,用状态压缩dp好解决. 预处理:每一行的障碍用一个状态 ...
- POJ 3254 Corn Fields状态压缩DP
下面有别人的题解报告,并且不止这一个状态压缩题的哦···· http://blog.csdn.net/accry/article/details/6607703 下面是我的代码,代码很挫,绝对有很大的 ...
随机推荐
- PHP进阶之路 -- 01 PHP基础语法
PHP进阶之路 -- PHP基础语法 windows环境下php环境 php定界符 php变量 php数据类型 数据类型转换 检测数据类型 php中三种输出方式 php字符集设置 php常量 p ...
- 【.NET】Browser Link: Failed to deserialize JSON in Browser Link call
问题 VS2013中调试程序发现,在浏览器控制台输出如下截图代码:
- 微信小程序省市区联动,自定义地区字典
最近在做一个项目的时候遇到了这么一个问题,就是省市区的联动呢,我们需要自定义字典来设置,那么微信小程序自带的省市区选择就不能用了,经过三根烟的催化,终于写出来了.下面献上代码示例. 首先是在utils ...
- HTML中Data的数据类型
"data"类型的Url格式,是在RFC2397中提出的,目的对于一些“小”的数据,可以在网页中直接嵌入,而不是从外部文件载入. 例如对于img这个Tag,哪怕这个图片非常非常的小 ...
- Linux后台运行python程序并输出到日志文件
后台运行python程序并标准输出到文件 现在有test.py程序要后台部署, 里面有输出内容 使用命令: nohup python -u test.py > test.log 2>&am ...
- CornerNet 算法笔记
论文名称:CornerNet: Detecting Objects as Paired Keypoints 论文链接:https://arxiv.org/abs/1808.01244 代码链接:htt ...
- 手写LVQ(学习向量量化)聚类算法
LVQ聚类与k-means不同之处在于,它是有标记的聚类,设定带标签的k个原型向量(即团簇中心),根据样本标签是否与原型向量的标签一致,对原型向量进行更新. 最后,根据样本到原型向量的距离,对样本进行 ...
- 闲记Windows 取证艺术
是不是很好奇,别人能够在你电脑上查看你运行程序历史,文档使用痕迹,浏览器浏览历史种种历史痕迹,没错,通过简单的系统了解以及配合相对应的工具,这一切就是这么的简单,看起来很高大上的操作,其实是 ...
- Angular build编译内存溢出"JavaScript heap out of memory"的解决办法
关于最近使用angular build编译打包的时候,遇到内存溢出的突发情况,做一个简单记录 编译报错如下↓↓↓ 报错信息很直观地指出是内存溢出了.是什么导致了内存溢出呢?其根本原因在于 nodejs ...
- Angular项目里Js代码里如何获取Ts文件中的属性数据
基于之前实现的Angular+ngx-ueditor富文本编辑器做一个简单补充记录,我们在使用Angular开发过程中,难免会使用到调用外部插件Js的应用,但是有的时候又需要在Js文件中调用Ts文件里 ...