luoguP5074 Eat the Trees
https://www.luogu.org/problemnew/show/P5074
插头 $ dp $ 入门题
如果你还不会插头 $ dp $ 请右转 洛谷插头dp题解
虽然是入门题但还是逃不过分类讨论的魔爪
这里采用了括号序列的方法
$ left $ 表示左插头的状态,$ up $ 表示右插头的状态
情况 1:这个格子不能放线
只有在 $ left == 0 $ && $ up == 0 $ 的时候才能转移
情况 2:$ left == 0 $ && $ up == 0 $
因为每个格子一定要放线,所以要给这个格子加一个下插头和一个右插头
情况 3:$ left == 0 $ && $ up != 0 $
有一个上插头下来,没有左插头,为了让这个插头形成回路,我们给这个插头加一个下插头或者右插头
情况 4:$ left != 0 $ && $ up == 0 $
跟情况 3 类似,这里不再赘述
情况 5:$ left == 1 $ && $ up == 1 $
一个左插头和一个上插头都是左括号,要到右边去找到一个右括号来匹配,并且将右括号变成左括号
个人认为这是最难理解的一种情况,强烈建议大家自己去画个图
情况 6:$ left == 2 $ && $ up == 2 $
根情况 5 类似,只不过变成了到左边找左括号匹配
情况 7:$ left == 2 $ && $ up == 1 $
相当于左插头和上插头抵消了,将这两个插头直接去掉后,左插头本来有一个左括号和它匹配,上插头有一个右括号和它匹配,仍然满足括号序列的性质
情况 8:$ left == 1 $ && $ up == 2 $
形成了一个回路,直接连接即可
当然,如果这个回路是最后一个回路的话要统计答案,判断是否是最后一个回路只需判断这个格子是不是最后一个可以放线的点
最后这个题目有一个小坑,就是当整张地图都不能放线的时候还是算有一种情况的
下面是简(sang)单(xin)明(bing)了(kuang)的代码
#include <bits/stdc++.h>
#define Fast_cin ios::sync_with_stdio(false), cin.tie();
#define For(i, a, b) for(register int i = a; i <= b; i++)
#define Forr(i, a, b) for(register int i = a; i >= b; i--)
#define DEBUG(x) cerr << "DEBUG" << x << " >>> " << endl;
using namespace std;
typedef unsigned long long ull;
typedef long long ll;
template <typename _T>
inline void read(_T &f) {
f = 0; _T fu = 1; char c = getchar();
while(c < '0' || c > '9') { if(c == '-') fu = -1; c = getchar(); }
while(c >= '0' && c <= '9') { f = (f << 3) + (f << 1) + (c & 15); c = getchar(); }
f *= fu;
}
template <typename T>
void print(T x) {
if(x < 0) putchar('-'), x = -x;
if(x < 10) putchar(x + 48);
else print(x / 10), putchar(x % 10 + 48);
}
template <typename T>
void print(T x, char t) {
print(x); putchar(t);
}
const int mod = 5801, N = mod + 100;
ll f[2][N], ans;
int a[15][15], tot[2], v[2][N], nxt[N], head[N], bin[15];
int T, n, m, now, end1, end2;
void ins(int zt, ll val) {
int u = zt % mod;
for(register int i = head[u]; i; i = nxt[i])
if(v[now][i] == zt) {
f[now][i] += val;
return;
}
nxt[++tot[now]] = head[u]; head[u] = tot[now];
f[now][tot[now]] = val; v[now][tot[now]] = zt;
}
void sol() {
tot[now] = 1; f[now][1] = 1; v[now][1] = 0;
for(register int i = 1; i <= n; i++) {
for(register int j = 1; j <= tot[now]; j++) v[now][j] <<= 2;
for(register int j = 1; j <= m; j++) {
now ^= 1; memset(head, 0, sizeof(head)); tot[now] = 0;
for(register int k = 1; k <= tot[now ^ 1]; k++) {
int zt = v[now ^ 1][k]; ll val = f[now ^ 1][k];
int t1 = (zt >> ((j << 1) - 2)) & 3, t2 = (zt >> (j << 1)) & 3;
if(a[i][j] == 0) {
if(t1 == 0 && t2 == 0) ins(zt, val);
} else if(t1 == 0 && t2 == 0) {
if(a[i][j + 1] && a[i + 1][j]) ins(zt ^ bin[j - 1] ^ (bin[j] << 1), val);
} else if(t1 == 0 && t2 != 0) {
if(a[i][j + 1]) ins(zt, val);
if(a[i + 1][j]) ins(zt ^ (bin[j] * t2) ^ (bin[j - 1] * t2), val);
} else if(t1 != 0 && t2 == 0) {
if(a[i + 1][j]) ins(zt, val);
if(a[i][j + 1]) ins(zt ^ (bin[j] * t1) ^ (bin[j - 1] * t1), val);
} else if(t1 == 1 && t2 == 1) {
int nowv = 1;
for(register int t = j + 1; t < m; t++) {
int t3 = (zt >> (t << 1)) & 3;
if(t3 == 1) nowv++;
if(t3 == 2) nowv--;
if(!nowv) { ins(zt ^ bin[j - 1] ^ bin[j] ^ (bin[t] * 3), val); break; }
}
} else if(t1 == 2 && t2 == 2) {
int nowv = 1;
for(register int t = j - 2; t > 0; t--) {
int t3 = (zt >> (t << 1)) & 3;
if(t3 == 1) nowv--;
if(t3 == 2) nowv++;
if(!nowv) { ins(zt ^ (bin[j - 1] << 1) ^ (bin[j] << 1) ^ (bin[t] * 3), val); break; }
}
} else if(t1 == 2 && t2 == 1) {
ins(zt ^ (bin[j - 1] << 1) ^ bin[j], val);
} else if(i == end1 && j == end2) {
ans += val;
} else ins(zt ^ bin[j - 1] ^ (bin[j] << 1), val);
}
}
}
}
int main() {
read(T); bin[0] = 1;
for(register int i = 1; i <= 11; i++) bin[i] = bin[i - 1] << 2;
for(register int Case = 1; Case <= T; Case++) {
memset(a, 0, sizeof(a));
read(n); read(m); ans = 0; end1 = end2 = 0;
for(register int i = 1; i <= n; i++) {
for(register int j = 1; j <= m; j++) {
read(a[i][j]);
if(a[i][j]) end1 = i, end2 = j;
}
}
if(!end1) { print(1, '\n'); continue; }
sol(); print(ans, '\n');
}
return 0;
}
luoguP5074 Eat the Trees的更多相关文章
- HDU 1693 Eat the Trees(插头DP、棋盘哈密顿回路数)+ URAL 1519 Formula 1(插头DP、棋盘哈密顿单回路数)
插头DP基础题的样子...输入N,M<=11,以及N*M的01矩阵,0(1)表示有(无)障碍物.输出哈密顿回路(可以多回路)方案数... 看了个ppt,画了下图...感觉还是挺有效的... 参考 ...
- 【HDU】1693 Eat the Trees
http://acm.hdu.edu.cn/showproblem.php?pid=1693 题意:n×m的棋盘求简单回路(可以多条)覆盖整个棋盘的方案,障碍格不许摆放.(n,m<=11) #i ...
- HDU 1693 Eat the Trees(插头DP)
题目链接 USACO 第6章,第一题是一个插头DP,无奈啊.从头看起,看了好久的陈丹琦的论文,表示木看懂... 大体知道思路之后,还是无法实现代码.. 此题是插头DP最最简单的一个,在一个n*m的棋盘 ...
- HDU 1693 Eat the Trees
第一道(可能也是最后一道)插头dp.... 总算是领略了它的魅力... #include<iostream> #include<cstdio> #include<cstr ...
- 【HDOJ】【1693】Eat The Trees
插头DP 插头dp模板题…… 这题比CDQ论文上的例题还要简单……因为不用区分左右插头(这题可以多回路,并不是一条哈密尔顿路) 硬枚举当前位置的状态就好了>_< 题解:http://blo ...
- Eat the Trees hdu 1693
Problem DescriptionMost of us know that in the game called DotA(Defense of the Ancient), Pudge is a ...
- HDU - 1693 Eat the Trees(多回路插头DP)
题目大意:要求你将全部非障碍格子都走一遍,形成回路(能够多回路),问有多少种方法 解题思路: 參考基于连通性状态压缩的动态规划问题 - 陈丹琦 下面为代码 #include<cstdio> ...
- HDU1693 Eat the Trees 插头dp
原文链接http://www.cnblogs.com/zhouzhendong/p/8433484.html 题目传送门 - HDU1693 题意概括 多回路经过所有格子的方案数. 做法 最基础的插头 ...
- 【HDU1693】Eat the Trees(插头dp)
[HDU1693]Eat the Trees(插头dp) 题面 HDU Vjudge 大概就是网格图上有些点不能走,现在要找到若干条不相交的哈密顿回路使得所有格子都恰好被走过一遍. 题解 这题的弱化版 ...
随机推荐
- 实用 Linux 命令行使用技巧集锦
最近在Quora上看到一个问答题目,关于在高效率Linux用户节省时间Tips.将该题目的回答进行学习总结,加上自己的一些经验,记录如下,方便自己和大家参考. 下面介绍的都是一些命令行工具,这些工具在 ...
- maven工程 ,通过maven更新后,jre恢复到1.5的解决方法
在maven setting.xml profiles节点下加入 <profile> <id>jdk-1.8</id> <activation> < ...
- nice & renice
[nice & renice & getpriority & setpriority] 1.nice & renice 参考:http://man.ddvip.com/ ...
- JAVA获取时间的方式
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 ...
- [原创]使用OPENCC库进行简繁转换(C++代码)
最近公司有一款游戏产品,字库存在问题,希望全自动进行简繁同屏自动转换的行为,减少工作量. 所以自己使用了WINDOWS自带的一些转换函数,但发现大量字出现异常,无法转换(测试iconv也发现无法转换) ...
- Python中的排序方法sort(),sorted(),argsort()等
python 列表排序方法sort.sorted技巧篇 转自https://www.cnblogs.com/whaben/p/6495702.html,学习参考. Python list内置sort( ...
- CentOS 最新版的下载地址 + 版本选择详解
CentOS 最新版的下载地址 + 版本选择详解 发现越来越多的机关单位.事业单位开始使用 Linux 作为主要服务器,毕竟,Linux的稳定性和高效性是众所周知的,所以我也打算把自己这一块技术加强一 ...
- CocoaPods:library not found for -lPods
This is my first shot to write a blog in English. Enjoy! ;) CocoaPods is a popular way to control iO ...
- [label][JavaScript] 自动填充内容的JavaScript 库
一个帮助你针对不同标签自动填入内容的轻量级javascript类库 - fixiejs http://www.gbtags.com/technology/javascript/20120802-fix ...
- EBS单实例上所有正在运行的并发请求以及请求目前的状态
--EBS单实例上所有正在运行的并发请求以及请求目前的状态---一个实例上运行的所有并发请求的总结和他们目前的状态以及等待状态 select w.seconds_in_wait "Se ...