题目描述

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

Solution:
  
一道标准动规题,由n<=12很容易联想到状压,如果定义dp[i][j]表示i,j这个点左上部分的矩阵的方案数,但是不好转移,并且状压没有体现。
  这时候我们可以想,状压压什么,可以直接压一行种草的位置,种草即为1,不种草即为0,这时候可以定义dp[i][k] i表示到第i行,状态为k时前面的状态
  转移方程就很容易想了,dp[i][k]是由上一行状态转移过来。
  简单来说:dp[i][k]=∑(k'满足情况) dp[i-1][k']
  k'需要满足什么情况呢:
    1.满足输入矩阵规定的1与0,即判断每个位置种草是否合法
    2.满足不与上一行冲突,保证上一行状态为1的位置,这一行不为1,这就把两个状态&计算起来,即k&k' 若为0,即不冲突(可以好好理解下)
  然后一行一行dp计算就可以了

Code:
  
 //It is coded by Ning_Mew on 2.25
#include<iostream>
#include<cmath>
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<algorithm>
using namespace std;
const int MOD=;
int n,m,ans=;
int a[][];
int num[][];
int dp[][<<]; void dfs(int x,int y){
if(y>m){x++;y=;}
if(x==n+&&y==){ans++;ans%=MOD;return;}
if(a[x][y]){
if(num[x-][y]||num[x][y-]){
num[x][y]=;dfs(x,y+);
}
else{
num[x][y]=;dfs(x,y+);
num[x][y]=;dfs(x,y+);
}
}
else{num[x][y]=;dfs(x,y+);}
return;
}
bool check(int k,int l){
for(int i=;i<=m;i++){
int ii=+m-i;
//cout<<k<<' '<<ii<<' '<<((k>>(i-1))&1)<<endl;
if(a[l][ii]==&&((k>>(ii-))&)==)return false;
}
for(int i=;i<=m;i++){
int ii=+m-i;
if((k>>(ii-)&)==&&((k>>ii)&==))return false;
}
return true;
}
int main(){
scanf("%d%d",&n,&m);
for(int i=;i<=n;i++){
for(int j=;j<=m;j++){scanf("%d",&a[i][j]);}
}
if(n<=&&m<=){
dfs(,);
printf("%d\n",ans);return ;
} for(int i=;i<=(<<m)-;i++){
if(check(i,)){
//cout<<i<<endl;
dp[][i]=;
}
}
for(int i=;i<=n;i++){
for(int k=;k<=(<<m)-;k++){
if(!check(k,i))continue;
for(int kk=;kk<=(<<m)-;kk++){
if((k&kk)==){
dp[i][k]=dp[i][k]+dp[i-][kk];
dp[i][k]=dp[i][k]%MOD;
}
}
}
}
for(int i=;i<=(<<m)-;i++)ans=(ans+dp[n][i])%MOD;
printf("%d\n",ans);
return ;
}

  转载附上本蒟蒻的链接和我说一声就ok了~  http://www.cnblogs.com/Ning-Mew/p/8469408.html

 

【题解】 P1879 玉米田Corn Fields (动态规划,状态压缩)的更多相关文章

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

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

  2. 洛谷 P1879 玉米田Corn Fields 题解

    题面 一道思维难度不大的状态压缩,也并不卡常,但细节处理要格外注意: f[i][j]表示前i行最后一行状态是j的方案数 #include <bits/stdc++.h> #define p ...

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

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

  4. 状压DP【洛谷P1879】 [USACO06NOV]玉米田Corn Fields

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

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

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

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

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

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

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

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

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

  9. poj3254 Corn Fields 利用状态压缩求方案数;

    Corn Fields 2015-11-25 13:42:33 Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 10658   ...

随机推荐

  1. spring-boot dubbo项目使用docker方式部署

    项目结构 本项目采用maven构建,有三个模块,分别是pms-interfaces, pms-services, pms-portal. 模块 描述 pms-interfaces 接口层,只能存放实体 ...

  2. [Oracle]查看数据是否被移入 DataBuffer 的方法

    查看数据是否被移入 DataBuffer 的方法: 例如:表名为 tabxxx, 用户为U2: SQL> grant dba to u2 identified by u2;SQL> con ...

  3. 汇编 EAX,EBX,ECX,EDX,寄存器

    知识点: 寄存器EAX 寄存器AX 寄存器AH 寄存器AL 一.EAX与AX,AH,AL关系图 一格表示一字节 #include <Windows.h> int _tmain(int ar ...

  4. SSRS配置2:加密管理

    在初始化Reporting Service时,SSRS会自动创建数据库[ReportServer],用于存储报表元数据,报表订阅,以及凭证(Credential)和连接信息等身份验证信息,身份验证数据 ...

  5. [计算机视觉] 图像拼接 Image Stitching

    [计算机视觉] 图像拼接 Image Stitching 2017年04月28日 14:05:19 阅读数:1027 作业要求: 1.将多张图片合并拼接成一张全景图(看下面效果图) 2.尽量用C/C+ ...

  6. Redis基本数据类型介绍笔记

    Redis 数据类型 Redis支持五种数据类型:string(字符串),hash(哈希),list(列表),set(集合)及zset(sorted set:有序集合). String(字符串) st ...

  7. android 一些常用的功能方法代码块

    我们这些苦逼的程序员在工作中,每一个老板都希望我们都能把手头的工作做好的,而且是越快越好,那我们要怎么样才能快起来呢?对于开发中常用的代码块无限复做是我们工作中简省时间最有效的途径之一,而下面的这些代 ...

  8. docker 部署Spring Boot:Docker化Spring Boot应用程序

    第一章 1.创建项目存放目录 mkdir /root/sproot -p 2.准备好Spring Boot应用程序 jar 包 testrest.jar 第二章 1. 安装docker 在所有节点执行 ...

  9. 2014.8.23 Research Meeting Report

    Dear All: It was good talk yesterday. However, I want to emphasize that, finally it is the *work* an ...

  10. 镜像仓库管理:与Portus不得不说的那些事

    背景: 目前在做一个云计算相关的项目,其中有这样一个需求:每个平台用户都有自己的docker镜像仓库(docker registry),用户可以对自己的镜像仓库的push/pull权限进行管理,也就是 ...