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. django通过使用jwt模块实现状态保持

    第一步:安装jwt pip install djangorestframework-jwt 第二步:settings/dev的配置文件配置 REST_FRAMEWORK = { # 认证配置 'DEF ...

  2. 利用Python工具进行打包功能

    基于Python脚本 iOS 工程的自动打包 导入的库 import os import requests import webbrowser import subprocess import shu ...

  3. 10分钟教你用VS2017将代码上传到GitHub

    前言 关于微软的Visual Studio系列,真可谓是宇宙最强IDE了.不过,像小编这样的菜鸟级别也用不到几个功能.今天给大家介绍一个比较实用的功能吧,把Visual Studio 2017里面写好 ...

  4. 51nod2004 终结之时 (支配树+树剖+树链的并)

    link 我永远喜欢洛天依 给定一张图世末积雨云,你需要维护其支配树: 单点修改,子树修改,树链修改 子树求和,树链求和,多条树链的并集求和 撤销之前的操作 可以先用 Lengauer-Tarjan ...

  5. 游戏1:HTML5制作网页游戏围住神经猫--createjs

    游戏简介:点击小圆圈,是蓝色的小圆圈不跑出圆圈外,跑出则结束游戏 准备工作: 下载easejs  :下载地址:http://www.createjs.cc/easeljs    中文网站 效果: in ...

  6. 深入理解计算机系统10——系统级I/O

    系统级I/O 输入/输出 是在主存和外部设备之间拷贝数据的过程. 外部设备可以是:磁盘驱动器.终端和网络. 输入和输出都是相对于主存而言的. 输入是从I/O设备拷贝数据到主存.输出时从主存拷贝数据到I ...

  7. BZOJ - 3166 可持久化Trie 维护次大区间

    题意:给出\(a[1...n]\),找出一个连续区间\(a[l...r],r>l\),令该区间的次大值为\(a_k\),使得\(a_k⊕a_i,l≤i≤r\)最大,输出全局最优解 (这题意有点别 ...

  8. JVM虚拟机知识点

    java -version 显示JDK 版本 Java HotSpot Client:1.5版本之后,热点探测,对加载的class文件做标记,对于频繁使用的class即时编译JIT本地缓存,不再重新进 ...

  9. 解决kvm虚拟机启动之后,网卡eth0变为eth1问题

    2018-12-19 故障前提 kvm虚拟机迁移到其他服务器上之后,重新启动网卡会出现问题 例如原网卡名称为eth0,迁移重启之后会自动变为eth1 为什么eth0会变成eth1? 很多Linux d ...

  10. kafka监控服务搭建

    wget https://github.com/Morningstar/kafka-offset-monitor/releases/download/0.4.1/KafkaOffsetMonitor- ...