poj Corn Fields 状态压缩dp。
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
Output
Sample Input
2 3
1 1 1
0 1 0
Sample Output
9
Hint
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。的更多相关文章
- POJ Corn Fields 状态压缩DP基础题
题目链接:http://poj.org/problem?id=3254 题目大意(名称什么的可能不一样,不过表达的意思还是一样的): 种玉米 王小二从小学一年级到现在每次考试都是班级倒数第一名,他的爸 ...
- POJ 3254 Corn Fields(状态压缩DP)
Corn Fields Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 4739 Accepted: 2506 Descr ...
- POJ 3254 Corn Fields (状态压缩DP)
题意:在由方格组成的矩形里面种草,相邻方格不能都种草,有障碍的地方不能种草,问有多少种种草方案(不种也算一种方案). 分析:方格边长范围只有12,用状态压缩dp好解决. 预处理:每一行的障碍用一个状态 ...
- POJ3254 - Corn Fields(状态压缩DP)
题目大意 给定一个N*M大小的土地,土地有肥沃和贫瘠之分(每个单位土地用0,1来表示贫瘠和肥沃),要求你在肥沃的单位土地上种玉米,如果在某个单位土地上种了玉米,那么与它相邻的四个单位土地是不允许种玉米 ...
- POJ 3254. Corn Fields 状态压缩DP (入门级)
Corn Fields Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 9806 Accepted: 5185 Descr ...
- POJ 3254 Corn Fields 状态压缩DP (C++/Java)
id=3254">http://poj.org/problem? id=3254 题目大意: 一个农民有n行m列的地方,每一个格子用1代表能够种草地,而0不能够.放牛仅仅能在有草地的. ...
- POJ 3254 Corn Fields状态压缩DP
下面有别人的题解报告,并且不止这一个状态压缩题的哦···· http://blog.csdn.net/accry/article/details/6607703 下面是我的代码,代码很挫,绝对有很大的 ...
- poj - 3254 Corn Fields (状态压缩dp入门)
http://poj.org/problem?id=3254 参考:http://blog.csdn.net/accry/article/details/6607703 农夫想在m*n的土地上种玉米, ...
- 【poj3254】Corn Fields 状态压缩dp
AC通道:http://vjudge.net/problem/POJ-3254 [题目大意] 农夫约翰购买了一处肥沃的矩形牧场,分成M*N(1<=M<=12; 1<=N<=12 ...
随机推荐
- HTTP请求的两种方式get和post的区别
1,get从服务器获取数据:post向服务器发送数据: 2,安全性,get请求的数据会显示在地址栏中,post请求的数据放在http协议的消息体: 3,从提交数据的大小看,http协议本身没有限制数据 ...
- uC/OS-II 函数之邮箱管理相关函数
上文主要介绍了消息队列相关的函数,本文介绍邮箱管理相关的函数:OSMboxCreate()建立一个邮箱,OSMboxDel()删除一个邮箱,OSMboxPend()等待邮箱中的消息,OSMboxPos ...
- 吴裕雄 python 机器学习——K均值聚类KMeans模型
import numpy as np import matplotlib.pyplot as plt from sklearn import cluster from sklearn.metrics ...
- [BZOJ 5158][Tjoi2014]Alice and Bob
传送门 \(\color{green}{solution}\) 贪心 /************************************************************** P ...
- 矩阵快速幂--51nod-1242斐波那契数列的第N项
斐波那契额数列的第N项 斐波那契数列的定义如下: F(0) = 0 F(1) = 1 F(n) = F(n - 1) + F(n - 2) (n >= 2) (1, 1, 2, 3, 5, 8, ...
- CentOS&.NET Core初试-2-安装.NET Core SDK和发布网站
系列目录 CentOS的安装和网卡的配置 安装.NET Core SDK和发布网站 Nginx的安装和配置 安装守护服务(Supervisor) 安装.NET Core SDK 注册Microsoft ...
- 关于cg语言中求法向量 N=mul(worldMatrix_IT,normal); 的随笔
解释一下标题,N是变换到世界坐标后的法向量,worldMatrix_IT是变换矩阵worldMatrix的逆的转置矩阵,normal就是模型坐标的法向量. 对于点p,我们根据变换矩阵M(即worldM ...
- ubuntu 18 常用软件安装
主要内容 1.安装 Ubuntu 18.04 LTS 2.安装 Google Chrome 3.安装 OpenVPN Client 4.安装 Docker CE 5.安装 MySQL Server 转 ...
- jmeter调试脚本之变量参数化
前言 对于参数化,觉得用得最多的应该是csvread函数.csv data config以及用户自定义变量(前一篇文章已经进行了讲解)控制器这几个 案例:bugfree ,提交bug,参数bug名称和 ...
- goLang冒泡
// test project main.gopackage main import ( "fmt") func main() { var a = [10]int{1, ...