Corn Fields
Time Limit: 2000MS   Memory Limit: 65536K
Total Submissions: 5763   Accepted: 3052

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

题意:在n*m的矩形里放东西,要求相邻的不能同时放。问有几种方式?

思路:用状态压缩,典型例题。

下面的书写,时间复杂度更高。

 #include<iostream>
#include<cstdio>
#include<cstring>
#include<cstdlib>
using namespace std;
int INF = ; int n,m;
int f[];
int dp[][<<]; bool panduan(int a,int b)
{
int i;
if( (f[a]&b) != b)//这个b不存在。
return false;
int x=;
for(i=; i<m; i++)
{
if( (b&x) ==x)//相邻的存在,矛盾了。
return false;
x=x<<;
}
return true;
}
int main()
{
int i,j,x,k,s;
while(scanf("%d%d",&n,&m)>)
{
for(i=; i<=n; i++)
{
f[i]=;
for(j=; j<=m; j++)
{
scanf("%d",&x);
f[i]=(f[i]<<)+x;
}
}
k=<<m;
memset(dp,,sizeof(dp));
dp[][]=;
for(i=; i<=n; i++) //枚举每一行
{
for(j=; j<k; j++)//该行的每一个状态。
{
if(panduan(i,j))//状态是否合法!!!
{
for(s=; s<k; s++)//枚举上一行的状态。
{
if( (j&s)> )continue;//是否合法。
dp[i][j]=dp[i][j]+dp[i-][s];
if(dp[i][j]>=INF)
dp[i][j]-=INF;
}
}
}
}
int num=;
for(i=; i<k; i++)
num=(num+dp[n][i])%INF;
printf("%d\n",num);
}
return ;
}

可以优化,先预处理一下。

 #include<stdio.h>
#include<string.h>
#include<stdlib.h> int state[],len;
int a[];
int dp[][];
void prepare()//预处理
{
int i,k=<<;
len=;
for(i=;i<k;i++)
{
if( (i&(i<<)) || (i&(i>>)) );
else state[len++]=i;
}
}
void solve(int n,int m)
{
int i,j,s;
memset(dp,,sizeof(dp));
dp[][]=;
for(i=;i<=n;i++)
{
for(j=;j<len;j++)
{
if( (a[i]&state[j])==state[j] )
for(s=;s<len;s++)
{
if( (state[j]&state[s])> );
else
{
dp[i][j]=(dp[i][j]+dp[i-][s])%;
}
}
}
}
for(j=,i=;i<len;i++)
if((state[i]&a[n])==state[i])
j=(j+dp[n][i])%;
printf("%d\n",j); }
int main()
{
int n,m;
int i,j,x;
prepare();
while(scanf("%d%d",&n,&m)>)
{
memset(a,,sizeof(a));
for(i=;i<=n;i++)
{
for(j=;j<=m;j++)
{
scanf("%d",&x);
a[i]=(a[i]<<)+x;
}
}//
solve(n,m);
}
return ;
}

poj Corn Fields 状态压缩dp。的更多相关文章

  1. POJ Corn Fields 状态压缩DP基础题

    题目链接:http://poj.org/problem?id=3254 题目大意(名称什么的可能不一样,不过表达的意思还是一样的): 种玉米 王小二从小学一年级到现在每次考试都是班级倒数第一名,他的爸 ...

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

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

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

    题意:在由方格组成的矩形里面种草,相邻方格不能都种草,有障碍的地方不能种草,问有多少种种草方案(不种也算一种方案). 分析:方格边长范围只有12,用状态压缩dp好解决. 预处理:每一行的障碍用一个状态 ...

  4. POJ3254 - Corn Fields(状态压缩DP)

    题目大意 给定一个N*M大小的土地,土地有肥沃和贫瘠之分(每个单位土地用0,1来表示贫瘠和肥沃),要求你在肥沃的单位土地上种玉米,如果在某个单位土地上种了玉米,那么与它相邻的四个单位土地是不允许种玉米 ...

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

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

  6. POJ 3254 Corn Fields 状态压缩DP (C++/Java)

    id=3254">http://poj.org/problem? id=3254 题目大意: 一个农民有n行m列的地方,每一个格子用1代表能够种草地,而0不能够.放牛仅仅能在有草地的. ...

  7. POJ 3254 Corn Fields状态压缩DP

    下面有别人的题解报告,并且不止这一个状态压缩题的哦···· http://blog.csdn.net/accry/article/details/6607703 下面是我的代码,代码很挫,绝对有很大的 ...

  8. poj - 3254 Corn Fields (状态压缩dp入门)

    http://poj.org/problem?id=3254 参考:http://blog.csdn.net/accry/article/details/6607703 农夫想在m*n的土地上种玉米, ...

  9. 【poj3254】Corn Fields 状态压缩dp

    AC通道:http://vjudge.net/problem/POJ-3254 [题目大意] 农夫约翰购买了一处肥沃的矩形牧场,分成M*N(1<=M<=12; 1<=N<=12 ...

随机推荐

  1. UITextInputMode

    An instance of the UITextInputMode class represents the current text-input mode. You can use this ob ...

  2. php中签名公钥、私钥(SHA1withRSA签名)以及AES(AES/ECB/PKCS5Padding)加密解密详解

    由于http请求是无状态,所以我们不知道请求方到底是谁.于是就诞生了签名,接收方和请求方协商一种签名方式进行验证,来取得互相信任,进行下一步业务逻辑交流. 其中签名用得很多的就是公钥私钥,用私钥签名, ...

  3. [转] iOS 在UILabel显示不同的字体和颜色

    在项目开发中,我们经常会遇到在这样一种情形:在一个UILabel 使用不同的颜色或不同的字体来体现字符串,在iOS 6 以后我们可以很轻松的实现这一点,官方的API 为我们提供了UILabel类的at ...

  4. Machine learning吴恩达第二周coding作业(选做)

    1.Feature Normalization: 归一化的处理 function [X_norm, mu, sigma] = featureNormalize(X) %FEATURENORMALIZE ...

  5. stark - 介绍

    总结下自己寒假所写的stark组件. 介绍: stark组件,是一个帮助开发者快速实现数据库表的增删改查+的组件. 目标: 1min 中完成实现一张表的增删改查等功能. 目录: stark - 1 ⇲ ...

  6. springcloud(二)-最简单的实战

    技术储备 Spring cloud并不是面向零基础开发人员,它有一定的学习曲线. 语言基础:spring cloud是一个基于Java语言的工具套件,所以学习它需要一定的Java基础.当然,sprin ...

  7. @PostConstruct和@PreConstruct注解

    @PostConstruct和@PreConstruct.这两个注解被用来修饰一个非静态的void()方法.而且这个方法不能有抛出异常声明. @PostConstruct //方式1 public v ...

  8. C/C++中创建(带头结点、不带头结点的)单链表

    1.带头结点的单链表(推荐使用带头结点的单链表)(采用尾插法) 了解单链表中节点的构成 从上图可知,节点包含数据域和指针域,因此,在对节点进行定义时,我们可以如下简单形式地定义: /* 定义链表 */ ...

  9. KafKa 启动

    Zookeeper 运行kafka需要使用Zookeeper,所以要先启动Zookeeper,如果没有Zookeeper,可以使用kafka自带打包和配置好的Zookeeper 1.进入kafka的b ...

  10. 2019美国大学生数学建模竞赛B题(思路)

    建模比赛已经过去三天了,但留校的十多天里,自己的收获与感受依然长存于心.下面的大致流程,很多并没有细化,下面很多情况都是在假设下进行的,比如假设飞机能够来回运送药品,运货无人机就只运货,在最大视距下侦 ...