题目描述

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.

农场主John新买了一块长方形的新牧场,这块牧场被划分成M行N列(1 ≤ M ≤ 12; 1 ≤ N ≤ 12),每一格都是一块正方形的土地。John打算在牧场上的某几格里种上美味的草,供他的奶牛们享用。

遗憾的是,有些土地相当贫瘠,不能用来种草。并且,奶牛们喜欢独占一块草地的感觉,于是John不会选择两块相邻的土地,也就是说,没有哪两块草地有公共边。

John想知道,如果不考虑草地的总块数,那么,一共有多少种种植方案可供他选择?(当然,把新牧场完全荒废也是一种方案)

输入输出格式

输入格式:

第一行:两个整数M和N,用空格隔开。

第2到第M+1行:每行包含N个用空格隔开的整数,描述了每块土地的状态。第i+1行描述了第i行的土地,所有整数均为0或1,是1的话,表示这块土地足够肥沃,0则表示这块土地不适合种草。

输出格式:

一个整数,即牧场分配总方案数除以100,000,000的余数。

输入输出样例

输入样例#1:

2 3
1 1 1
0 1 0
输出样例#1:

9

【题解】
这是状态压缩必做题,状态压缩dp思想就是把一个状态压成一个二进制数,再运用位运算判断状态。
话不多说,直接上代码。
#include<cstdio>
#include<iostream>
#include<cstring>
#include<cmath>
#include<string>
#include<algorithm>
using namespace std;
const int mod=;
int n,m;
int a[][];
int q[];//µÚ[i]ÐеÄÍÁµØ×´Ì¬
int f[][<<];//f[i][j]ǰ[i]ÐеÄ״̬ΪjʱµÄºÏ·¨·½°¸Êý
bool pd[<<];
void init()
{
scanf("%d%d",&m,&n);
for(int i=;i<=m;i++)
for(int j=;j<=n;j++)
{
scanf("%d",&a[i][j]);
q[i]=(q[i]<<)+a[i][j];
}
for(int i=;i<(<<n);i++)
pd[i]=(!(i&(i<<)))&&(!(i&(i>>)));
}
void dp()
{
f[][]=;
for(int i=;i<=m;i++)
{
for(int j=;j<(<<n);j++)
{
if(pd[j]&&((j&q[i])==j))//ʹjÓëq[i]Ïàͬ±£Ö¤ÊÇÔÚ·ÊÎÖÍÁµØÉÏÖÖ²Ý
{
for(int k=;k<(<<n);k++)
{
if((k&j)==) //ÉÏÒ»ÐÐÓëÕâÒ»ÐвݵØÎÞ¹«¹²±ß,ºÏ·¨
f[i][j]=(f[i][j]+f[i-][k])%mod;
}
}
}
}
}
int main()
{
init();
dp();
int ans=;
for(int i=;i<(<<n);i++)
ans=(ans+f[m][i])%mod;
printf("%d\n",ans);
return ;
}

洛谷 P1879 [USACO06NOV]玉米田Corn Fields的更多相关文章

  1. 洛谷P1879 [USACO06NOV]玉米田Corn Fields(状压dp)

    洛谷P1879 [USACO06NOV]玉米田Corn Fields \(f[i][j]\) 表示前 \(i\) 行且第 \(i\) 行状态为 \(j\) 的方案总数.\(j\) 的大小为 \(0 \ ...

  2. C++ 洛谷 P1879 [USACO06NOV]玉米田Corn Fields

    没学状压DP的看一下 合法布阵问题  P1879 [USACO06NOV]玉米田Corn Fields 题意:给出一个n行m列的草地(n,m<=12),1表示肥沃,0表示贫瘠,现在要把一些牛放在 ...

  3. 洛谷 P1879 [USACO06NOV]玉米田Corn Fields 题解

    P1879 [USACO06NOV]玉米田Corn Fields 题目描述 Farmer John has purchased a lush new rectangular pasture compo ...

  4. 洛谷P1879 [USACO06NOV]玉米田Corn Fields (状态压缩DP)

    题目描述 Farmer John has purchased a lush new rectangular pasture composed of M by N (1 ≤ M ≤ 12; 1 ≤ N ...

  5. 洛谷P1879 [USACO06NOV]玉米田Corn Fields【状压DP】题解+AC代码

    题目描述 Farmer John has purchased a lush new rectangular pasture composed of M by N (1 ≤ M ≤ 12; 1 ≤ N ...

  6. [洛谷P1879][USACO06NOV]玉米田Corn Fields

    题目大意:有一个$n\times m$的矩阵,$(1 \leq m \leq 12; 1 \leq n \leq 12)$,想在其中的一些格子中种草,一些格子不能种草,且两块草地不相邻.问有多少种种植 ...

  7. 【洛谷P1879】玉米田Corn Fields

    玉米田Corn Fields 题目链接 此题和互不侵犯状压DP的做法类似 f[i][j]表示前i行,第i行种植(1)/不种植(0)构成的二进制数为j时的方案数 首先我们可以预处理出所有一行中没有两个相 ...

  8. 洛谷 P1879 [USACO06NOV]玉米田 解题报告

    P1879 [USACO06NOV]玉米田Corn Fields 题目描述 农场主\(John\)新买了一块长方形的新牧场,这块牧场被划分成\(M\)行\(N\)列\((1 ≤ M ≤ 12; 1 ≤ ...

  9. P1879 [USACO06NOV]玉米田Corn Fields(状压dp)

    P1879 [USACO06NOV]玉米田Corn Fields 状压dp水题 看到$n,m<=12$,肯定是状压鸭 先筛去所有不合法状态,蓝后用可行的状态跑一次dp就ok了 #include& ...

随机推荐

  1. /etc删了怎么办

    实施一个哥们一个手抖,把/etc删掉了:别人无法ssh到上面,除了他.怎么办? 从类似的OK机器中打包一个etc.tar,然后将etc.tar放到OK机器www服务器目录里面:然后在问题机器上面通过w ...

  2. 使用Json Template在Azure China创建ARM类型的虚拟机

    前面几篇文章介绍过Azure的两种VM的模式,包括ASM和ARM.并且介绍了如何用Azure CLI和PowerShell创建虚拟机.本文将介绍如何采用Json的Template来创建基于ARM的VM ...

  3. CentOS7网卡设置为桥接模式静态IP配置方法详解

    备份网络文件 [root@localhost network-scripts]# cd /etc/sysconfig/network-scripts/ [root@localhost network- ...

  4. 第二届PHP全球开发者大会(含大会的PPT)

    PHP全球开发者大会于2016年5月14日至15日在北京召开 更多现场图片请猛击: http://t.cn/RqeP7y9 ,  http://t.cn/RqD8Typ 最后,这次大会的PPT可以在这 ...

  5. EMC (电磁兼容性)

    电磁兼容性EMC(Electro Magnetic Compatibility),是指设备或系统在其电磁环境中符合要求运行并不对其环境中的任何设备产生无法忍受的电磁干扰的能力.因此,EMC包括两个方面 ...

  6. HDOJ1010 (DFS+奇偶剪枝)

    Tempter of the Bone Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Othe ...

  7. request_mem_region,ioremap 和phys_to_virt()

    转载: request_mem_region,ioremap 和phys_to_virt()   Linux在头文件include/linux/ioport.h中定义了三个对I/O内存资源进行操作的宏 ...

  8. 【转】IMSI和IMEI

    国际移动客户识别码(International Mobile Subscriber Identification Number) 为了在无线路径和整个GSM移动通信网上正确地识别某个移动客户,就必须给 ...

  9. java代码异常普通的====

    总结:对于各种流类, package com.da; //包括运行异常,和非运行异常 import java.io.*; public class ryl { public static void m ...

  10. java中内部类的讲解

    java中有一个内部类的概念,由于之前一直比较忙,没有单独拿出时间总结一下,今天我就把内部类的相关知识进行一下汇总,如果有不足之处,欢迎批评指正. 1)java内部类的概念.       在一个类的的 ...