题意:

  有一个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)的更多相关文章

  1. POJ 3254 - Corn Fields - [状压DP水题]

    题目链接:http://poj.org/problem?id=3254 Time Limit: 2000MS Memory Limit: 65536K Description Farmer John ...

  2. POJ 3254 Corn Fields (状压dp)

    题目链接:http://poj.org/problem?id=3254 给你n*m的菜地,其中1是可以种菜的,而菜与菜之间不能相邻.问有多少种情况. 状压dp入门题,将可以种菜的状态用一个数的二进制表 ...

  3. [ An Ac a Day ^_^ ] POJ 3254 Corn Fields 状压dp

    题意: 有一块n*m的土地 0代表不肥沃不可以放牛 1代表肥沃可以放牛 且相邻的草地不能同时放牛 问最多有多少种放牛的方法并对1e8取模 思路: 典型的状压dp 能状态压缩 能状态转移 能状态压缩的题 ...

  4. Poj - 3254 Corn Fields (状压DP)(入门)

    题目链接:https://vjudge.net/contest/224636#problem/G 转载于:https://blog.csdn.net/harrypoirot/article/detai ...

  5. POJ 3254 Corn Fields (状压入门)

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

  6. poj 3254 Corn Fields 状压dp入门

    题目链接 题意 在\(M\times N\)的\(0,1\)格子上放东西,只有标记为\(1\)的格子可以放东西,且相邻的格子不能同时放东西.问有多少种放法. 思路 参考:swallowblank. \ ...

  7. POJ 1684 Corn Fields(状压dp)

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

  8. 状压DP POJ 3254 Corn Fields

    题目传送门 /* 状态压缩DP:先处理硬性条件即不能种植的,然后处理左右不相邻的, 接着就是相邻两行查询所有可行的种数并累加 写错一个地方差错N久:) 详细解释:http://www.tuicool. ...

  9. poj - 3254 - Corn Fields (状态压缩)

    poj - 3254 - Corn Fields (状态压缩)超详细 参考了 @外出散步 的博客,在此基础上增加了说明 题意: 农夫有一块地,被划分为m行n列大小相等的格子,其中一些格子是可以放牧的( ...

  10. POJ 3254 Corn Fields(状压DP)

    Corn Fields Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 13732   Accepted: 7216 Desc ...

随机推荐

  1. Permutacja

    题意: 要求动态求问是否有一个 1~n 的排列,使得满足 $p_i \leq a_i$,给定 $a_i$ 初始值,接下来 m次单个位置修改,每次求问是否可以构造出来. 解法: 构造一序列使得 $cnt ...

  2. 11. 几点基于Web日志的Webshell检测思路

    摘要: Web日志记录了网站被访问的情况,在Web安全的应用中,Web日志常被用来进行攻击事件的回溯和取证.Webshell大多由网页脚本语言编写,常被入侵者用作对网站服务器操作的后门程序,网站被植入 ...

  3. MVC实用笔记

    ---------------------------- 渲染一个Action:@{Html.RenderAction("Rulelist", "AjaxReuqestD ...

  4. 通俗易懂,什么是.NET Core以及.NET Core能做什么

    作者:依乐祝 原文地址:https://www.cnblogs.com/yilezhu/p/10880884.html 我们都知道.NET Core是一个可以用来构建现代.可伸缩和高性能的跨平台软件应 ...

  5. 模板 - SPFA

    SPFA可以用来判断负环或者计算带负权的最短路. 其实带负权的最短路可以用带势Dijkstra计算-- 所以SPFA基本就拿来判负环了-- #include<bits/stdc++.h> ...

  6. MYSQL数据库设计规范11111

    MYSQL数据库设计规范       1.数据库命名规范         采用26个英文字母(区分大小写)和0-9的自然数(经常不需要)加上下划线'_'组成;         命名简洁明确(长度不能超 ...

  7. hdu1688(dijkstra求最短路和次短路)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1688 题意:第k短路,这里要求的是第1短路(即最短路),第2短路(即次短路),以及路径条数,最后如果最 ...

  8. UVA - 1330 City Game

    InputThe rst line of the input le contains an integer K | determining the number of datasets. Next l ...

  9. [Swift]LeetCode1078. Bigram 分词 | Occurrences After Bigram

    ★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★➤微信公众号:山青咏芝(shanqingyongzhi)➤博客园地址:山青咏芝(https://www.cnblogs. ...

  10. 我的省选 Day -15

    Day  -15 23:22:45 还有十几天就要去参加省选啦~,今天开始写日记记录一下,所以今天是第负十五天. 今天是阶段考的第二天(由于奥赛不用考试的我7:30才慢悠悠地到机房 早上学习动态DP, ...