POJ-325Corn Fields
链接:https://vjudge.net/problem/POJ-3254#author=freeloop
题意:
农场主John新买了一块长方形的新牧场,这块牧场被划分成M行N列(1 ≤ M ≤ 12; 1 ≤ N ≤ 12),每一格都是一块正方形的土地。John打算在牧场上的某几格里种上美味的草,供他的奶牛们享用。
遗憾的是,有些土地相当贫瘠,不能用来种草。并且,奶牛们喜欢独占一块草地的感觉,于是John不会选择两块相邻的土地,也就是说,没有哪两块草地有公共边。
John想知道,如果不考虑草地的总块数,那么,一共有多少种种植方案可供他选择?(当然,把新牧场完全荒废也是一种方案)
思路:
状压dp,二进制表示每一行的状态,储存时使用十进制。
具体思路注释。
代码:
#include <iostream>
#include <memory.h>
#include <string>
#include <istream>
#include <sstream>
#include <vector>
#include <stack>
#include <algorithm>
#include <map>
#include <queue>
#include <math.h>
#include <cstdio>
using namespace std; typedef long long LL;
const int MAXN = 15;
const int STATES = 600;
const int MOD = 1e9; //377 最多状态 int cur[MAXN];
int dp[MAXN][STATES];
int states[STATES];
int pos = 0; void Init(int w)
{
int total = (1 << w);
for (int i = 0;i < total;i++)
{
if(!(i&(i<<1)))//i往左位移以为,是否有两个1重叠,重叠不满足
states[++pos] = i;
}
} int main()
{
int n, m;
int flag;
scanf("%d%d", &m, &n);
Init(n);
for (int i = 1;i <= m;i++)
for (int j = 1;j <= n;j++)
{
scanf("%d", &flag);
if (!flag)//方便判断某种状态是否满足,0时加
cur[i] += (1 << (n - j));
} for (int i = 1;i <= pos;i++)
{
if ((cur[1] & states[i]) == 0)//状态为1表示种,同时行状态为1表示不能种
dp[1][i] = 1;
} for (int i = 2;i <= m;i++)
{
for (int j = 1;j <= pos;j++)
{
if (states[j] & cur[i])//枚举的状态是否冲突当前行
continue;
for (int k = 1;k <= pos;k++)
{
if (states[k] & cur[i - 1])//枚举的上一行状态是否冲突
continue;
if (states[j] & states[k])//枚举的上下两行状态是否冲突
continue;
dp[i][j] = (dp[i][j] + dp[i - 1][k]) % MOD;
}
}
} int res = 0;
for (int i = 1;i <= pos;i++)
res = (res + dp[m][i]) % MOD; printf("%d\n", res); return 0;
}
POJ-325Corn Fields的更多相关文章
- poj Corn Fields 状态压缩dp。
Corn Fields Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 5763 Accepted: 3052 Descr ...
- poj Muddy Fields
Muddy Fields 原题去我创的专题里找,在文件夹首页. 题目: 给出N*M矩阵.当中*表示泥土,.表示小草.要你用最少的木板把泥土覆盖. 木板长度不限.可是仅仅能水平和竖直. 行列式二分匹配配 ...
- POJ Corn Fields 状态压缩DP基础题
题目链接:http://poj.org/problem?id=3254 题目大意(名称什么的可能不一样,不过表达的意思还是一样的): 种玉米 王小二从小学一年级到现在每次考试都是班级倒数第一名,他的爸 ...
- POJ Muddy Fields 泥泞的牧场 二分图
Muddy Fields Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 13235 Accepted: 4879 汪星人 ...
- poj 3254Corn Fields (入门状压dp)
Farmer John has purchased a lush ≤ M ≤ ; ≤ N ≤ ) square parcels. He wants to grow some yummy corn fo ...
- POJ 3254. Corn Fields 状态压缩DP (入门级)
Corn Fields Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 9806 Accepted: 5185 Descr ...
- poj 3254 Corn Fields
http://poj.org/problem?id=3254 Corn Fields Time Limit: 2000MS Memory Limit: 65536K Total Submissio ...
- 状压DP POJ 3254 Corn Fields
题目传送门 /* 状态压缩DP:先处理硬性条件即不能种植的,然后处理左右不相邻的, 接着就是相邻两行查询所有可行的种数并累加 写错一个地方差错N久:) 详细解释:http://www.tuicool. ...
- POJ 3254 Corn Fields(状压DP)
Corn Fields Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 13732 Accepted: 7216 Desc ...
- poj 2226 Muddy Fields(最小覆盖点+构图)
http://poj.org/problem?id=2226 Muddy Fields Time Limit: 1000MS Memory Limit: 65536K Total Submissi ...
随机推荐
- Windows消息类型及说明
WM_ Window Message 窗口消息,一般用在SendMessage,PostMessage这样的消息函数中 SM_ Static Message 静态标签消息 SS_ Static Sty ...
- python实现路由追踪,并生成追踪图片
#!/usr/bin/env python # -*- coding: utf-8 -*- import os,sys,time,subprocess import warnings,logging ...
- hadoop运行测试命令遇到的问题
2017-02-16 09:46:14,926 INFO mapreduce.Job: Task Id : attempt_1487148856575_0001_m_000001_0, Status ...
- 【CQ18高一暑假前挑战赛3】标程
[A:LCM] #include<bits/stdc++.h> using namespace std; #define ll long long int main() { ll a,b, ...
- mysql : mysql 5.6.13 免安装版配置
前言:真正用到mysql是在公司的第二个项目下,具体的一些在之前的博客文章(http://www.cnblogs.com/zhengzeze/p/5623440.html)中也提到了,其中涉及到,免安 ...
- OGNL和类型转换
转载 JavaWeb -- Struts 数据传输:OGNL和类型转换 1. 数据传输:OGNL和类型转换 OGNL和struts2 OGNL:Object-Graph Navigation Lang ...
- 对比<input type="text" id="">和<asp:TextBox runat="server" ID="">
首先这两个都是对应文本输入框形式: <input type="text"class="form-control"id="txt_add_pro_ ...
- console.log是异步么?
让我们来看一个例子: var a = {}; console.log(a); a.foo = 'foo'; 4 console.log(a); 但是问题来了:在chorme跟firfox一样么? 结果 ...
- tinyxml一些应注意的问题
今天在对使用tinyxml库的程序调试的时候,出现的一些问题让人很纠结,特记以此... 在对TixmlDocument创建时我是用new创建的,然后在用完之后我用delete释放掉,可是用gdb调试 ...
- lightoj1004【基础DP】
从低端到顶端求个最大值: 思路: 基础DP,递推 #include<cstdio> #include<queue> #include<map> #include&l ...