http://poj.org/problem?id=3254

Corn Fields
Time Limit: 2000MS   Memory Limit: 65536K
Total Submissions: 9118   Accepted: 4843

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

Line 1: Two space-separated integers: M and N 
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

Line 1: One integer: the number of ways that FJ can choose the squares modulo 100,000,000.

Sample Input

2 3
1 1 1
0 1 0

Sample Output

9

Hint

Number the squares as follows:

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 。

农夫FJ有一块n行m列的矩形土地, 有的土地是肥沃的,可以在这些土地上放牛(用1表示),有的土地不能放牛(用0表示),而且相邻的可以放牛的格子不能同时有牛,问FJ一共有多少种放牛的方案(一头牛都不放也是一种方案)。

分析:以样例为例,我们可以一行一行的考虑。假如对于每一行,用1表示放牛,0表示不放牛,

第一行的状态为:000001010011(舍弃) 100101110(舍弃)111(舍弃)

符合题意的状态只有5个,所以第一行有5种方案。

第二行的状态为:000010

但是第二行中的010和第一行中的010冲突,所以如果第二行状态为010时,共有4种方案;状态为000时,有5种方案,所以一共有4+5=9种方案。

分析完我们会发现,对于每一行,都可以一个01串来表示这一行的状态,而这个状态可以用一个十进制整数来代替,也就是说,把这个状态压缩成了一个十进制整数,所以称为是状态压缩。

本题中,dp[i][j] 就表示第i行状态为j时的方案数。

状态压缩dp中最常见的就是位运算,因为位运算可以很方便的判断当前状态是否合法。

例如本题中判断第i行是不是有两块相邻的土地同时都有牛,假设当前状态为X,那么只需要判断X&(X>>1)或者X&(X<<1)的结果是不是0,

如果是0,说明没有相邻的,否则就说明有相邻的。

 
 
 
AC代码1:
 
 #include<stdio.h>
#include<string.h>
#include<vector>
using namespace std;
#define mod 100000000
const int N = << + ;
int dp[][N], Map[][];
int n, m;
vector<int> vec[];
int Pow(int a, int x) //2的X次方
{
int s = ;
for(int i = ; i <= x; i++)
s <<= ;
return s;
}
int fun(int x) //求第X行的土地状态,0表示可以放牛,1表示不能放牛
{
int s = ;
for(int i = ; i <= m; i++)
s += (!Map[x][i]) * Pow(, m-i);
return s;
}
int main()
{
int i, j;
while(~scanf("%d%d",&n,&m))
{
memset(dp, , sizeof(dp));
memset(vec, , sizeof(vec));
for(i = ; i <= n; i++)
for(j = ; j <= m; j++)
scanf("%d",&Map[i][j]);
vec[].push_back();
int k = <<m;
for(i = ; i < k; i++)
dp[][i] = ;
for(i = ; i <= n; i++)
{
int tmp = fun(i); //当前行的状态
for(j = ; j < k; j++)
{
if(j & (j>>)) continue; //j的二进制表示中有两个相邻的1
if(j & tmp) continue; //排除在当前行不符合条件的
vec[i].push_back(j);
}
for(j = ; j < vec[i].size(); j++) //排除和上一行冲突的
{
int u = vec[i][j];
for(int z = ; z < vec[i-].size(); z++)
{
int v = vec[i-][z];
if(u & v) continue;
dp[i][u] = (dp[i][u] + dp[i-][v]) % mod;
}
}
}
int ans = ;
for(i = ; i < k; i++)
ans = (ans + dp[n][i]) % mod;
printf("%d\n",ans);
}
return ;
}

AC代码2:

 /*   dp[i][j] 表示第i行状态为j时的合法状态数量 */

 #include <cstdio>
#include <cstring>
const int N = ;
#define mod 100000000
int dp[N][<<N];
int beg[N]; bool checkA(int x) { //判断本行是否合法
return !(x & (x >> ));
} bool checkB(int a, int b) { //判断和上一行是否冲突
return !(a & b);
} int main() {
int n, m, t;
while(~scanf("%d%d", &n, &m)) {
memset(dp, , sizeof(dp));
memset(beg, , sizeof(beg));
for(int i = ; i < n; i++) {
for(int j = ; j < m; j++) {
scanf("%d", &t);
if(t) beg[i] = beg[i] | ( << j);
}
} for(int i = ; i < (<<m); i++) //求出第一行的合法状态数目
if((beg[]|i) == beg[] && checkA(i))
dp[][i] = ; for(int i = ; i < n; i++) {
for(int j = ; j < (<<m); j++) { //枚举本行状态
if(((beg[i]|j) == beg[i]) && checkA(j)) {
for(int k = ; k < (<<m); k++) { //枚举上一行的状态
if(checkB(j, k)) //根据上一行递推出本行
dp[i][j] = (dp[i][j] + dp[i-][k]) % mod;
}
}
}
} int ans = ;
for(int i = ; i < (<<m); i++)
ans = (ans + dp[n-][i]) % mod;
printf("%d\n", ans);
}
return ;
}
AC代码3:
 
 #include<stdio.h>
#include<iostream>
#include<string.h>
using namespace std;
int n,m;
int a[][],dp[][<<],v[<<];
const int mod=;
void init()
{
int i,l=;
for(i=;i<(<<);i++)
{
if((i&i<<)==)//选出所有不相邻的状态
v[l++]=i;
}
}
int check(int x,int p)
{
for(int i=;i<=m;i++)
if((p&<<(i-))&&!a[x][i])//不能放的地方放了
return ;
return ;
}
int main()
{
int i,j,k;
init();
while((scanf("%d %d",&n,&m))!=EOF)
{
for(i=;i<=n;i++)
for(j=;j<=m;j++)
scanf("%d",&a[i][j]);
memset(dp,,sizeof(dp));
for(i=;i<=n;i++)
{
for(j=;v[j]<(<<m);j++)
{
if(check(i,v[j]))
continue;
if(i==)
{
dp[i][j]=;
continue;
}
for(k=;v[k]<(<<m);k++)
{
if((v[j]&v[k])==)//与上一行没有相邻的
dp[i][j]+=dp[i-][k];
}
}
}
__int64 ans=;
for(i=;v[i]<(<<m);i++)
ans=(ans+dp[n][i])%mod;
printf("%I64d\n",ans);
}
return ;
}

poj 3254 Corn Fields的更多相关文章

  1. 状压DP POJ 3254 Corn Fields

    题目传送门 /* 状态压缩DP:先处理硬性条件即不能种植的,然后处理左右不相邻的, 接着就是相邻两行查询所有可行的种数并累加 写错一个地方差错N久:) 详细解释:http://www.tuicool. ...

  2. poj - 3254 - Corn Fields (状态压缩)

    poj - 3254 - Corn Fields (状态压缩)超详细 参考了 @外出散步 的博客,在此基础上增加了说明 题意: 农夫有一块地,被划分为m行n列大小相等的格子,其中一些格子是可以放牧的( ...

  3. POJ 3254. Corn Fields 状态压缩DP (入门级)

    Corn Fields Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 9806   Accepted: 5185 Descr ...

  4. POJ 3254 Corn Fields(状压DP)

    Corn Fields Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 13732   Accepted: 7216 Desc ...

  5. POJ 3254 Corn Fields(状态压缩DP)

    Corn Fields Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 4739   Accepted: 2506 Descr ...

  6. [ACM] POJ 3254 Corn Fields(状态压缩)

    Corn Fields Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 8062   Accepted: 4295 Descr ...

  7. poj 3254 Corn Fields 国家压缩dp

    意甲冠军: 要在m行n陆行,有一些格您可以种树,别人做不到的.不相邻的树,我问了一些不同的共同拥有的法律. 分析: 从后往前种,子问题向父问题扩展,当种到某一格时仅仅有他和他后面的n-1个格子的情况对 ...

  8. POJ 3254 - Corn Fields - [状压DP水题]

    题目链接:http://poj.org/problem?id=3254 Time Limit: 2000MS Memory Limit: 65536K Description Farmer John ...

  9. POJ 3254 Corn Fields (状压dp)

    题目链接:http://poj.org/problem?id=3254 给你n*m的菜地,其中1是可以种菜的,而菜与菜之间不能相邻.问有多少种情况. 状压dp入门题,将可以种菜的状态用一个数的二进制表 ...

随机推荐

  1. java-Lambda表达式

    浏览以下内容前,请点击并阅读 声明 Lambda表达式与匿名类的作用类似,当实现的接口仅有一个方法时,使用lambda表达式能够减少代码的使用量. //此处定义一个仅含有一个抽象方法的功能接口 int ...

  2. WSDL项目---添加头信息和附件

    用于底层协议的SOAP请求是HTTP,可以添加两个自定义HTTP头(例如用于身份验证或会话)和附件. 让我们看一下这两个. 1. 自定义HTTP标头 直接添加自定义HTTP头: 我们已经添加了自定义内 ...

  3. 解决 卸载Mysql后,服务还在的问题

    HKEY_LOCAL_MACHINE\SYSTEM\ControlSet001\Services\Eventlog\Application\MySQL文件夹:删除HKEY_LOCAL_MACHINE\ ...

  4. 如何禁止root用户远程登陆

    如果不禁止root用户的远程登陆,就会将root用户暴露在网络环境中, 因为在缺省的安装中root用户是一定存在的,所以root用户容易受到攻击, 所以我们可以禁止root用户的远程登陆来实现保护ro ...

  5. ARM大学计划全球经理到访华清远见,深入交流教育合作

    来源:华清远见嵌入式学院 10月20日,ARM大学计划全球经理Khaled Benkrid,高级内容主编洪川博士在ARM大学计划亚太经理陈炜博士的陪同下到访华清远见,就最新嵌入式技术.ARM处理器在教 ...

  6. ZeroMQ接口函数之 :zmq_socket – 创建ZMQ套接字

    ZeroMQ API 目录 :http://www.cnblogs.com/fengbohello/p/4230135.html ZeroMQ 官方地址:http://api.zeromq.org/4 ...

  7. Java实现验证码制作之一Kaptcha验证码

    Kaptcha验证码 是google提供的验证码插件,使用起来相对简单,设置的干扰线以及字体扭曲不易让其他人读取破解. 这里我们需要 导入一个 kaptcha-2.3.jar  下载地址:http:/ ...

  8. *HDU1598 并查集

    find the most comfortable road Time Limit: 1000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K ...

  9. bzoj 3438: 小M的作物

    Description 背景 小M还是个特么喜欢玩MC的孩纸... 描述 小M在MC里开辟了两块巨大的耕地A和B(你可以认为容量是无穷),现在,小P有n中作物的种子,每种作物的种子有1个(就是可以种一 ...

  10. php实现网页trace方法

    // 记录内存初始使用和开始时间,在系统的入口记录 $beginTime= microtime(TRUE); $start_memory = memory_get_usage(); //die; ec ...