Corn Fields——POJ3254状态压缩Dp
Corn Fields
| Time Limit: 2000MS | Memory Limit: 65536K |
|---|
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
USACO 2006 November Gold
题意:在一个n*m的草场上,每一块草场都可以放一只牛,有的草场是贫瘠的,所以不能放牛。由于这些牛比较孤僻,所以不喜欢在自己吃草的地方周围有其他的牛(上下左右),问总共有多少种放法。
思路:在图上的每一个点都可能放牛(除去荒地),所以搜索的时间复杂度很高。
- 假设我们知道矩阵的n和m,我们用二进制表示在一行的放牛的状态(不考虑荒地),则所有的状态为[0,1 << m),状态是否符合可以根据(x&(x<<1)),返回值为0表示没有相邻的1,则符合条件,否则不符合条件,这样我们就记录在草场宽为m时的所有可以放的情况,记录在Sta数组中。
- 由于有荒地,所以对于每一行的第j个草地如果为荒地则为1,否则为零,这样就可以每一行有一个数表示他们的草地的状态,存在Map数组中,如果要判断在符合情况的方式中是不是有矛盾的,可以用Map[i]&Sta[j]==0(表示第i行草地与第j种方式),如果等于零则符合,不等于零则不符合(在纸上写写看看为什么)。
- 首先将第一行的所以状态初始化即Dp[1][j]=(Map[1]&Sta[j])==0?1:0,然后根据第一行的所有状态去枚举第二行看是否符合,即
for i=2 -> n
{
for j=0 -> num
{
if Map[i]&Sta[j] == 1
{
continue;
}
for k=0 -> num
{
if Map[i-1]&Sta[k] == 1
{
continue;
}
if Sta[j]&Sta[k]==0
{
Dp[i][j]+=Dp[i-1][k]
}
}
}
}具体情况见代码
#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <iostream>
#include <algorithm>
using namespace std;
const int Max = 1<<13;
const int Mod = 100000000;
int Dp[15][Max];
int Map[15];
int Sta[Max];
bool Judge(int x)
{
return (x&(x<<1));
}
bool Dec(int x,int y)
{
return (Map[x]&Sta[y]);
}
int main()
{
int n,m;
while(~scanf("%d %d",&n,&m))
{
memset(Map,0,sizeof(Map));
memset(Dp,0,sizeof(Dp));
memset(Sta,0,sizeof(Sta));
int data;
for(int i=1;i<=n;i++)
{
for(int j=1;j<=m;j++)
{
scanf("%d",&data);
if(data==0)
{
Map[i]+=(1<<(j-1)); //每一行的情况用一个数来表示
}
}
}
int num = 0 ;
for(int i=0;i<(1<<m);i++)//所有符合方式
{
if(!Judge(i))
{
Sta[num++] = i;
}
}
for(int i=0;i<num;i++)//第一行的所有状态初始化
{
if(!Dec(1,i))
{
Dp[1][i]=1;
}
}
for(int i=2;i<=n;i++)
{
for(int j=0;j<num;j++)
{
if(Dec(i,j))// 是否符合
{
continue;
}
for(int k=0;k<num;k++)
{
if(Dec(i-1,k))//i-1行是否符合第k种情况
{
continue;
}
if(!(Sta[j]&Sta[k])) //上下行之间也互相的满足条件
{
Dp[i][j]+=Dp[i-1][k];
}
}
}
}
int ans =0;
for(int i=0;i<num;i++)
{
ans+=Dp[n][i];
ans%=Mod;
}
printf("%d\n",ans);
}
return 0;
}
Corn Fields——POJ3254状态压缩Dp的更多相关文章
- Corn Fields poj3254(状态压缩DP)
Corn Fields Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 6081 Accepted: 3226 Descr ...
- 洛谷P1879 [USACO06NOV]玉米田Corn Fields (状态压缩DP)
题目描述 Farmer John has purchased a lush new rectangular pasture composed of M by N (1 ≤ M ≤ 12; 1 ≤ N ...
- POJ 3254 Corn Fields(状态压缩DP)
题目大意:给出一个M*N的矩阵,元素为0表示这个地方不能种玉米,为1表示这个地方能种玉米,现在规定所种的玉米不能相邻,即每行或者没列不能有相邻的玉米,问一共有多少种种植方法. 举个例子: 2 3 1 ...
- POJ 3254:Corn Fields(状态压缩DP)
题目大意:一个矩形的草地,分为多个格子,有的格子可以有奶牛(标为1),有的格子不可以放置奶牛(标为0),计算摆放奶牛的方案数. 分析: f[i,j]表示第i行状态为j的方案总数. 状态转移方程f[i, ...
- poj3254 Corn Fields 利用状态压缩求方案数;
Corn Fields 2015-11-25 13:42:33 Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 10658 ...
- poj - 3254 - Corn Fields (状态压缩)
poj - 3254 - Corn Fields (状态压缩)超详细 参考了 @外出散步 的博客,在此基础上增加了说明 题意: 农夫有一块地,被划分为m行n列大小相等的格子,其中一些格子是可以放牧的( ...
- poj3254 状态压缩dp
题意:给出一个n行m列的草地,1表示肥沃,0表示贫瘠,现在要把一些牛放在肥沃的草地上,但是要求所有牛不能相邻,问你有多少种放法. 分析:假如我们知道第 i-1 行的所有的可以放的情况,那么对于 ...
- POJ 3254 Corn Fields(状态压缩)
一道状态压缩的题,错了好多次....应该先把满足的情况预处理出来 #include<iostream> #include<cstdio> #include<cstring ...
- POJ3254 - Corn Fields(状态压缩DP)
题目大意 给定一个N*M大小的土地,土地有肥沃和贫瘠之分(每个单位土地用0,1来表示贫瘠和肥沃),要求你在肥沃的单位土地上种玉米,如果在某个单位土地上种了玉米,那么与它相邻的四个单位土地是不允许种玉米 ...
随机推荐
- Scala的模式匹配
1.概述 2.程序示例(普通的示例) 3.模式匹配(Array) 4.程序示例(Array) 5.模式匹配(List) 6.程序示例 7.遍历 8.模式匹配(case class) 9.程序示例(传统 ...
- QQ战场形势图
真是没什么可说,全面开战,无坚不摧,活脱脱一个中央帝国.只有极少的方向处于守势.本来对腾讯也没什么特别的感觉,但是看了这张图,真是让人热血沸腾. 如果美国的公司能像腾讯做的这么大相当于:faceboo ...
- oracle 关键字
Oracle 关键字(保留字) DBA账户下执行SQL语句:select * from v$reserved_words ; 可得到所有的关键字: 1 ! 1 2 & 1 3 ( 1 4 ...
- 5.首次登陆与在线求助man page
X Window与命令行模式的切换:通常我们也称命令行模式为终端界面(terminal或console),linux默认的情况下会提供6个Terminal来让用户登录,切换的方式为使用[Ctrl]+[ ...
- ASP.Net中的Web Resource
http://support.microsoft.com/kb/910442,这是中文的,机器翻译的,不太容易看懂,英文的是:http://support.microsoft.com/kb/91044 ...
- Leetcode: Valid Word Square
Given a sequence of words, check whether it forms a valid word square. A sequence of words forms a v ...
- Eclipse的安装与调试
1.首先进入eclipse官网下载eclipse 2.根据自己的系统选择相应版本进行下载.由于我的是64bit,所以我选择64bit版下载. 3.点开下载的程序,进行安装. 4.随后进入了安装页面.和 ...
- kindEditor完整认识 PHP上调用并上传图片说明/////////////////////////////z
最近又重新捣鼓了下kindeditor,之前写的一篇文章http://hi.baidu.com/yanghbmail/blog/item/c681be015755160b1d9583e7.html ...
- excel手机号码归属地批量公式查询 vlookup函数
Excel手机号码归属地 批量公式查询 vlookup函数 xls 手机号码 添加一列 地区归属地 使用 公式:=(VLOOKUP(LEFT(B2,7),号段数据库!B:D,2,0)& ...
- IP釋放、清除、以及刷新DNS
Windows 10 於桌面按住 Windows + X 按鍵. 選擇 Command Prompt (以管理員執行). 在彈跳視窗中輸入 ipconfig /release. 等待數秒回報此 IP ...