POJ 3254 Corn Fields (状压DP,轮廓线DP)
题意:
有一个n*m的矩阵(0<n,m<=12),有部分的格子可种草,有部分不可种,问有多少种不同的种草方案(完全不种也可以算1种,对答案取模后输出)?
思路:
明显的状压DP啦,只是怎样压缩状态?跟轮廓线DP一样,按格子为单位来设计状态,一个状态只需要表示到其上方和左方的格子,所以最多只需要保存min(n,m)个01状态就行了(可以尝试旋转一下矩阵),最多需要12位。用哈希表来做会比较快吧,不用去考虑无效的状态,比如出现相邻两个1。
//#include <bits/stdc++.h>
#include <iostream>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <map>
#include <algorithm>
#include <vector>
#include <iostream>
#define pii pair<int,int>
#define INF 0x7f3f3f3f
#define LL long long
#define ULL unsigned long long
using namespace std;
const double PI = acos(-1.0);
const int N=;
const int M=;
int g[N][N];
struct Hash_Map
{
static const int mod=;
static const int N=;
int head[mod]; //桶指针
int next[N]; //记录链的信息
int status[N]; //状态
int value[N]; //状态对应的DP值。
int size;
void clear() //清除哈希表中的状态
{
memset(head, -, sizeof(head));
size = ;
} void insert(int st, int val) //插入状态st的值为val
{
int h = st%mod;
for(int i=head[h]; i!=-; i=next[i])
{
if(status[i] == st) //这个状态已经存在,累加进去。
{
value[i] += val;
value[i]%=M;
return ;
}
}
status[size]= st; //找不到状态st,则插入st。
value[size] = val;
next[size] = head[h] ; //新插入的元素在队头
head[h] = size++;
}
}hashmap[]; int cal(int n,int m)
{
int cur=, mod=(<<m-)-;
hashmap[cur].clear();
hashmap[].insert(,);
for(int i=; i<=n; i++)
{
for(int j=; j<=m; j++)
{
cur^=;
hashmap[cur].clear();
for(int k=; k<hashmap[cur^].size; k++)
{
int s=hashmap[cur^].status[k];
int v=hashmap[cur^].value[k];
int t=(s&mod)<<; //去掉最高位
hashmap[cur].insert(t,v); //不放
if(g[i][j])
{
if( s&(<<m-) ) continue;
if( j!= && (s&) ) continue; //不能放
hashmap[cur].insert(t^,v);
}
}
}
}
int ans=;
for(int k=; k<hashmap[cur].size; k++)
{
ans+=hashmap[cur].value[k];
ans%=M;
}
return ans;
} int main()
{
//freopen("input.txt","r",stdin);
int n, m;
while(~scanf("%d%d",&n,&m))
{
for(int i=; i<=n; i++)
for(int j=; j<=m; j++)
scanf("%d",&g[i][j]);
printf("%d\n",cal(n,m));
}
return ;
}
AC代码
POJ 3254 Corn Fields (状压DP,轮廓线DP)的更多相关文章
- POJ 3254 - Corn Fields - [状压DP水题]
题目链接:http://poj.org/problem?id=3254 Time Limit: 2000MS Memory Limit: 65536K Description Farmer John ...
- POJ 3254 Corn Fields (状压dp)
题目链接:http://poj.org/problem?id=3254 给你n*m的菜地,其中1是可以种菜的,而菜与菜之间不能相邻.问有多少种情况. 状压dp入门题,将可以种菜的状态用一个数的二进制表 ...
- [ An Ac a Day ^_^ ] POJ 3254 Corn Fields 状压dp
题意: 有一块n*m的土地 0代表不肥沃不可以放牛 1代表肥沃可以放牛 且相邻的草地不能同时放牛 问最多有多少种放牛的方法并对1e8取模 思路: 典型的状压dp 能状态压缩 能状态转移 能状态压缩的题 ...
- Poj - 3254 Corn Fields (状压DP)(入门)
题目链接:https://vjudge.net/contest/224636#problem/G 转载于:https://blog.csdn.net/harrypoirot/article/detai ...
- POJ 3254 Corn Fields (状压入门)
Farmer John has purchased a lush new rectangular pasture composed of M by N (1 ≤ M≤ 12; 1 ≤ N ≤ 12) ...
- poj 3254 Corn Fields 状压dp入门
题目链接 题意 在\(M\times N\)的\(0,1\)格子上放东西,只有标记为\(1\)的格子可以放东西,且相邻的格子不能同时放东西.问有多少种放法. 思路 参考:swallowblank. \ ...
- POJ 1684 Corn Fields(状压dp)
描述 Farmer John has purchased a lush new rectangular pasture composed of M by N (1 ≤ M ≤ 12; 1 ≤ N ≤ ...
- 状压DP POJ 3254 Corn Fields
题目传送门 /* 状态压缩DP:先处理硬性条件即不能种植的,然后处理左右不相邻的, 接着就是相邻两行查询所有可行的种数并累加 写错一个地方差错N久:) 详细解释:http://www.tuicool. ...
- poj - 3254 - Corn Fields (状态压缩)
poj - 3254 - Corn Fields (状态压缩)超详细 参考了 @外出散步 的博客,在此基础上增加了说明 题意: 农夫有一块地,被划分为m行n列大小相等的格子,其中一些格子是可以放牧的( ...
- POJ 3254 Corn Fields(状压DP)
Corn Fields Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 13732 Accepted: 7216 Desc ...
随机推荐
- 11_listview入门
listview是在安卓开发当中很常用的API. 以垂直滚动的列表的方式展示条目的控件. ListAdapter是一个桥梁,给ListView提供数据的.数据是由适配器来进行提供的.Adapter是数 ...
- gSoap使用入门(2)----自定义接口头文件
摘自:http://blog.csdn.net/zhuzhihai1988/article/details/8131556 接口头文件的格式在向导中没有看到明确的说明性的内容,但通过看开发包中示例程序 ...
- OSP 与 Session
大家都知道,OSP是不支持session的,换句话说,登录有效期是永久的.一般网站,如果你不操作一段时间以后,必须重新登录.osp不是这样的,你一旦登录后,即便服务器重启了,你依然能访问服务器并不需要 ...
- Unity中场景异步加载
引入命名空间 using UnityEngine.UI; using UnityEngine.SceneManagement; using System.Collections; using Syst ...
- SQL Server 截取两个固定字符之间的字符串(案例)
网上的问题: 参考这篇<函数PARSENAME使用和截取字符串>https://www.cnblogs.com/insus/p/10958452.html 的方法: )='||MO21|T ...
- Golang : cobra 包简介
Cobra 是一个 Golang 包,它提供了简单的接口来创建命令行程序.同时,Cobra 也是一个应用程序,用来生成应用框架,从而开发以 Cobra 为基础的应用.本文的演示环境为 ubuntu 1 ...
- List Control控件中及时捕获checkbox被选中的消息的解决方案
转自:http://blog.csdn.net/vycode/article/details/7345073 我的功能需求是:用户可以在List Control里添加item,当无选项被选中(即Che ...
- PyCharm中导入第三方包
在Windows中的PyCharm中导入模块的方法 1.在file-->setting-->project interpreter中 2,点击右上角加号,搜索要添加的第三方库的名称,得到后 ...
- AI决策算法 之 GOAP (一)
http://blog.csdn.net/lovethrain/article/details/67632033 本系列文章内容部分参考自:http://gamerboom.com/archives/ ...
- java 多线程下载文件并实时计算下载百分比(断点续传)
多线程下载文件 多线程同时下载文件即:在同一时间内通过多个线程对同一个请求地址发起多个请求,将需要下载的数据分割成多个部分,同时下载,每个线程只负责下载其中的一部分,最后将每一个线程下载的部分组装起来 ...