poj3254状压DP入门
Crawling in process... Crawling failed Time Limit:2000MS Memory Limit:65536KB 64bit IO Format:%I64d & %I64u
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.
/*
poj3254
分类:状压DP
因为后一行的状态只与前一行有关,考虑使用DP做
每一行最多有12个格子,用0表示不取,1表示取总共有1<<12-1种情况
首先单独看某一行,合法的状态是没有两个1彼此相邻,用位运算来判断
i&(i<<1)为0则表示没有相邻的1,是合法状态。
先初始化出所有的不考虑某块地不能选择的情况,保存所有的合法状态,
然后再结合题目进行筛选,筛选范围时1~(1<<m),看那种合法状态是否
与草原实际相符,具体办法就是依次取合法状态的某一位,如果改位为
1但实际上这块草地不能取,这种状态就是不可行的,第一行若第j种状态可行
dp[i][j]=1,状态转移方程则是dp[i][j]+=dp[i-1][k],k代表上一层的可行状态
若不可行,dp[i-1][k]=0,所以初始化时要将dp数组初始化为0
*/
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;
const int maxn=(<<);//最多有12列
int v[maxn],dp[][maxn];
int a[][];//用来存图;
int n,m;
const long long mod=;
int tot;
void init()
{
tot=;
for(int i=;i<maxn;i++)//记录所有合法状态
{
if((i&(i<<))==)
v[tot++]=i;
}
}
bool check(int row,int p)
{
for(int i=;i<=m;i++)
{
if((p&(<<(i-)))&&!a[row][i])//这种状态不符合实际
return true;
}
return false;
}
int main()
{
init();
// for(int i=0;i<tot;i++)
// cout<<v[i]<<" ";
while(scanf("%d%d",&n,&m)!=EOF)
{
memset(dp,,sizeof(dp));
for(int i=;i<=n;i++)
for(int j=;j<=m;j++)
scanf("%d",&a[i][j]);
for(int i=;i<=n;i++)
{
for(int j=;v[j]<(<<m);j++)
{
if(check(i,v[j])) continue;
if(i==)
{
dp[i][j]=;
continue;
}
for(int k=;v[k]<(<<m);k++)
{
if((v[j]&v[k])==)
dp[i][j]+=dp[i-][k];
}
}
}
__int64 ans=;
for(int i=;v[i]<(<<m);i++)
{
ans=(ans+dp[n][i])%mod;
}
printf("%I64d\n",ans);
}
}
poj3254状压DP入门的更多相关文章
- 状压dp入门
状压dp的含义 在我们解决动态规划题目的时候,dp数组最重要的一维就是保存状态信息,但是有些题目它的具有dp的特性,并且状态较多,如果直接保存的可能需要三维甚至多维数组,这样在题目允许的内存下势必是开 ...
- poj2686 状压dp入门
状压dp第一题:很多东西没看懂,慢慢来,状压dp主要运用了位运算,二进制处理 集合{0,1,2,3,....,n-1}的子集可以用下面的方法编码成整数 像这样,一些集合运算就可以用如下的方法来操作: ...
- 状压DP入门详解+题目推荐
在动态规划的题型中,一般叫什么DP就是怎么DP,状压DP也不例外 所谓状态压缩,一般是通过用01串表示状态,充分利用二进制数的特性,简化计算难度.举个例子,在棋盘上摆放棋子的题目中,我们可以用1表示当 ...
- POJ:1185-炮兵阵地(状压dp入门)
炮兵阵地 Time Limit: 2000MS Memory Limit: 65536K Description 司令部的将军们打算在N*M的网格地图上部署他们的炮兵部队.一个N*M的地图由N行M列组 ...
- POJ3254 状压dp
Corn ...
- POJ 3254 & POJ 1185(状压DP入门)
Corn Fields Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 16773 Accepted: 8860 Desc ...
- poj 3254 状压dp入门题
1.poj 3254 Corn Fields 状态压缩dp入门题 2.总结:二进制实在巧妙,以前从来没想过可以这样用. 题意:n行m列,1表示肥沃,0表示贫瘠,把牛放在肥沃处,要求所有牛不能相 ...
- 状压dp入门第一题 poj3254
题目链接 http://poj.org/problem?id=3254 转自http://blog.csdn.net/harrypoirot/article/details/23163485 #inc ...
- poj3254(状压dp入门第一道题,很详细)
题目链接:http://poj.org/problem?id=3254 学习博客:https://blog.csdn.net/harrypoirot/article/details/23163485 ...
随机推荐
- Js正则表达式提取图片地址
JavaScript使用正则表达式和Replace两种方法提取IMG标签图片地址,代码如下: /正则表达式 <script language="javascript"> ...
- vim编辑器中撤销和恢复操作
在VIM编辑器下切换至命令行模式: 撤销: u 恢复: ctrl + r
- 使用django+mysql+scrapy制作的一个小说网站
小说网站用的程序都是千篇一律的,jieqi + guanguang,无聊时间学习python+django,也做了一个小说网站,下面说一说做这个网站一些过程, 制作这种采集站,最要紧的是要有一个好的采 ...
- c语言实现灰度图转换为二值图
将上篇得到的灰度图转换为二值图,读取像素数据,低于某一值置0,否则设置为255,为得到更好的效果不同图片应采用不同的值 /* 2015年6月2日11:16:22 灰度图转换为二值图 blog:http ...
- java之多线程的理解
线程的属性 (1)线程的状态 线程在它的生命周期中可能处于以下几种状态之一: New(新生):线程对象刚刚被创建出来: Runnable(可运行):在线程对象上调用start方法后,相应线程便 ...
- Linux C判断字符串是否为数字
Title:Linux C判断字符串是否为数字 --2013-10-14 15:54 #include <ctype.h> #include <string.h> int I ...
- zoj 3647 Gao the Grid
http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemId=4837 先求出从所有点随机找出三个点的组合数,然后去掉共线的,平行好去掉,斜线就 ...
- Acdream Mengzhu
http://acdream.info/problem?pid=1006 #include <cstdio> #include <cmath> #include <cst ...
- Qt自定义带游标的slider,在滑块正上方显示当前值(类似于进度条,用一个额外的QLabel冒充QSilder的一部分,然后move就行了)
首先自定义QSlider的子类MyCustomSlider,如下所示. mycustomslider.h #ifndef MYCUSTOMSLIDER_H #define MYCUSTOMSLIDER ...
- 【转】用串口登录Beaglebone Black、用usb共享电脑网络、内核模块的本地编译
原文网址:http://bbs.eeworld.com.cn/thread-431507-1-1.html 串口连接BBB使用usb线可以连接BBB和电脑,用ssh就可以登录BBB来进行操作.但有时候 ...