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. redis在Linux上的部署和jedis简单使用

    一.redis的安装 这里演示的版本是Redis4.0.6,Linux系统是CentOS6.7,Jdk1.7,Jedis2.8.1 这是官方文档介绍的安装方式 下载,解压,编译: $ wget htt ...

  2. Your branch is ahead of 'origin/master' by 1 commit.

    git reset HEAD^ --soft git reset HEAD^ --hard --soft 表示保留当前commit,重新commit --hard 表示丢弃当前add,重新add.co ...

  3. 浏览器性能接口performance.timing说明

    原文来自于 https://dvcs.w3.org/hg/webperf/raw-file/tip/specs/NavigationTiming/Overview.html 下图描述了该接口的各个时间 ...

  4. Python爬取网上车市[http://www.cheshi.com/]的数据

    #coding:utf8 #爬取网上车市[http://www.cheshi.com/]的数据 import requests, json, time, re, os, sys, time,urlli ...

  5. 为什么int类型的数据可以存储超过9999?

    int占4字节,4*8=32位,10进制取值范围为 (-2^31-1)~(2^31-1):-2147483648~2147483647 package test; public class test1 ...

  6. 做开发,你少不了的淘宝镜像之--maven镜像

    maven阿里云中央仓库 修改maven根目录下的conf文件夹中的settings.xml文件,内容如下: <mirrors>    <mirror>      <id ...

  7. HTML DOM--基础概述

    DOM: Document Object Model(文档对象模型)的简写,那么,这是一种什么样的模型,简单点来说,就是将文档当成了一棵树.它独立于平台与语言,允许程序与脚本动态地访问.更新文档的结构 ...

  8. TX2之多线程读取视频及深度学习推理

    背景 一般在TX2上部署深度学习模型时,都是读取摄像头视频或传入视频文件进行推理,从视频中抽取帧进行目标检测等任务.对于大点的模型,推理的速度是赶不上摄像头或视频的帧率的,如果我们使用单线程进行处理, ...

  9. weex 编译vue成js

    cd 项目 1.开发 npm run web 开发过程中可以直接使用浏览器运行 npm run ios 2.打包 npm run build 会在 dist 文件夹 中生成js文件, 即可拖入ios ...

  10. KVC 原理及自定义实现

    一.  setValue: forKey: 赋值过程 1.首先寻找setter方法(两个) - setName: -setIsName: 2.然后再寻找成员变量 默认 + (BOOL)accessIn ...