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 ...
随机推荐
- php实现获取汉字的首字母实例
//取汉字的asc区间然后返回汉字首字母了,代码如下:header("Content-type: text/html; charset=utf-8"); function getf ...
- file_get_content、fsockopen和curl之间的优缺点
file_get_content 优点:在抓取单个文件上,效率很高,返回没有头信息的文件. 缺点:在抓取远程文件时,和fopen一样容易出错.在抓取多个跨域文件时,未对DNS进行缓存,所以效率上不不高 ...
- js new Date().Format
/** * * 对Date的扩展,将 Date 转化为指定格式的String * 月(M).日(d).小时(h).分(m).秒(s).季度(q) 可以用 1-2 个占位符 * 年(y)可以用 1-4 ...
- 我只能说,CDH5真的屌爆了!!!
参考URL http://blog.csdn.net/yangzhaohui168/article/details/34185579 http://blog.csdn.net/yangzhaohui1 ...
- LED驅動芯片對LED壽命的影響
5050年,領導作為一種新型節能光源在世界和中國有非常高的熱情和偉大的問題,不得不贏得市場占有率從室外到室內照明應用,中國也如雨后春筍般涌現在大型和小型LED照明企業.鑒于LED照明的主要原因是其促進 ...
- 如何把SKYPE的发送消息由enter改为ctrl+enter?
如果您的skype是tom-skype3.8正式版.您可以在skype面板中,选择"工具"-"选项"-"会话"-"会话设置&quo ...
- linux0.12 链接过程
终于编译OK了..可链接就是一大堆错误 问题1: boot/head.o: In function `startup_32': (.text+0x10): undefined reference to ...
- redis 网络流程图 <一>
本来一直想好好读下redis源码.可是每次读了一点就不读了. 主要是没坚持每天都读. 隔几天看.就忘记前面的流程.就越来越不想看了. 很是蛋疼.这个还是要坚持读完的.打算这段时间都源码的时候.都大 ...
- 我的eclipse插件推荐
1. ER图工具 ERMaster - http://ermaster.sourceforge.net/update-site/ 优点:可根据数据库生成ER图.支持生成转换成PNG,JavaDOC ...
- JFreeChart学习
一.步骤: 创建数据集(准备数据) 根据数据集生成JFreeChart对象,并对其做相应的设置(标题,图例,x轴,Y轴,对象渲染等) 将JFreeChart对象输出到文件或者Servlet输出流等 二 ...