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. php-fpm 解析

    以下内容转自:https://blog.csdn.net/u010785091/article/details/78705690 有一些工具一直在用,却从来也不知道这些东西是什么. 现在想想还是梳理一 ...

  2. 洛谷P4097 [HEOI2013]Segment(李超线段树)

    题面 传送门 题解 调得咱自闭了-- 不难发现这就是个李超线段树,不过因为这里加入的是线段而不是直线,所以得把线段在线段树上对应区间内拆开之后再执行李超线段树的操作,那么复杂度就是\(O(n\log^ ...

  3. 【redis基础】

    基础知识 http://www.runoob.com/redis/redis-partitioning.html redis性能分析 https://www.cnblogs.com/mushroom/ ...

  4. 正则表达式,sed简单用法

      一. 正则表达式 1. 常见的正则表达式字符 [] 匹配字符集 grep "bl[lo]g" oldboy.txt 表示字符‘l’或者‘o’都可匹配 * 重复前面字符任意次 g ...

  5. JMeter—监听器

    用来显示JMeter取样器的测试结果,能够以树.表.图形形式显示,也可以以文件方式保存. 一.设置默认配置 初始化配置文件设置: 监听器默认保存哪些数据域,可以在jmeter.properties(或 ...

  6. 851 AlvinZH的鬼畜密码(背包DP大作战N)

    851 AlvinZH的鬼畜密码 思路 难题.动态规划. 先判断字符串是否合理(可翻译),然后分段处理,每一小段用动态规划求出解法数. dp[i]:字符串str[0~i]的解法数.通过判断str[i] ...

  7. dcoker做私有仓库

    在官网上拉取私有仓库的镜像.(官网提供的镜像为:registry) docker  pull  registry 启动镜像,并且映射端口.(启动时加-p参数即可) docker -d  -p 5000 ...

  8. JavaScript实现自定义alert弹框

    aaarticlea/png;base64,iVBORw0KGgoAAAANSUhEUgAAAh0AAAFkCAYAAACEpYlzAAAfj0lEQVR4nO3dC5BddZ0n8F93pxOQCO

  9. python迭代、可迭代对象、迭代器及生成器

    迭代 通常意义上的迭代是指:重复执行一系列运算,从前面的量依次推出后面的量的过程,每一次迭代的结果,会作为下一次迭代的初始值. 在c.c++.java等编程语言中的for循环语句,就是一个迭代过程,例 ...

  10. 基于JQuery easyui,gson的批量新增/修改和删除-servlet版

    最近项目需要用到在页面进行批量操作,做了一些这方面的学习,参照网上的资料写了个小例子,记录一下: 准备 引入gson-2.6.2.jar,这里使用gson而不使用json-lib,原因是json-li ...