luogu Eat the Trees
/*
用和模板类似的方法就行
但是实际上弱化版不用考虑匹配情况限制更加宽松, 只需要保存每个位置有无插头即可,
*/
#include<cstdio>
#include<algorithm>
#include<cstring>
#include<iostream>
#include<queue>
#include<cmath>
#define ll long long
#define M 13
#define mmp make_pair
using namespace std;
int read() {
int nm = 0, f = 1;
char c = getchar();
for(; !isdigit(c); c = getchar()) if(c == '-') f = -1;
for(; isdigit(c); c = getchar()) nm = nm * 10 + c - '0';
return nm * f;
}
const int hs = 299987;
int mp[M][M], ex, ey, now, last, bit[30], g[2][300010], tot[2], n, m;
ll ans, f[2][300010];
struct Note {
int nxt, to;
} note[300010];
int head[300010], cnt = 0;
void insert(int x, ll v) {
int key = x % hs;
for(int i = head[key]; i; i = note[i].nxt) {
if(g[now][note[i].to] == x) {
f[now][note[i].to] += v;
return;
}
}
tot[now]++;
g[now][tot[now]] = x;
f[now][tot[now]] = v;
note[++cnt].nxt = head[key];
head[key] = cnt;
note[cnt].to = tot[now];
}
void Dp() {
now = 1, last = 0;
tot[now] = 1;
f[now][1] = 1;
g[now][1] = 0;
for(int i = 1; i <= n; i++) {
for(int j = 1; j <= tot[now]; j++) g[now][j] <<= 1;
for(int j = 1; j <= m; j++) {
cnt = 0;
memset(head, 0, sizeof(head));
swap(now, last);
tot[now] = 0;
ll nowans;
int nowsta, sd, sr;
for(int k = 1; k <= tot[last]; k++) {
nowsta = g[last][k], nowans = f[last][k];
sd = (nowsta >> bit[j]) % 2, sr = (nowsta >> bit[j - 1]) % 2;
if(!mp[i][j]) {
if(!sd && !sr) insert(nowsta, nowans);
} else if(!sd && !sr) {
if(mp[i + 1][j] && mp[i][j + 1]) insert(nowsta + (1 << bit[j - 1]) + (1 << bit[j]), nowans);
} else if(!sd && sr) {
if(mp[i + 1][j]) insert(nowsta, nowans);
if(mp[i][j + 1]) insert(nowsta - (1 << bit[j - 1]) + (1 << bit[j]), nowans);
} else if(sd && !sr) {
if(mp[i + 1][j]) insert(nowsta - (1 << bit[j]) + (1 << bit[j - 1]), nowans);
if(mp[i][j + 1]) insert(nowsta, nowans);
} else {
insert(nowsta - (1 << bit[j]) - (1 << bit[j - 1]), nowans);
if(i == ex && j == ey) ans += nowans;
}
}
}
}
}
void init() {
ans = 0;
ex = 0, ey = 0;
memset(f, 0, sizeof(f));
memset(g, 0, sizeof(g));
memset(mp, 0, sizeof(mp));
}
int main() {
int T = read();
for(int i = 1; i <= 25; i++) bit[i] = i;
while(T--) {
init();
n = read(), m = read();
for(int i = 1; i <= n; i++) {
for(int j = 1; j <= m; j++) {
int x = read();
if(x == 1) mp[i][j] = 1, ex = i, ey = j;
}
}
Dp();
if(ex == 0 && ey == 0) ans++;
cout << ans << '\n';
}
return 0;
}
/*
2
2 4
1 1 1 1
1 1 1 1
6 3
1 1 1
1 0 1
1 1 1
1 1 1
1 0 1
1 1 1
1
3 3
0 0 0
0 0 0
0 0 0
*/
luogu 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 大概就是网格图上有些点不能走,现在要找到若干条不相交的哈密顿回路使得所有格子都恰好被走过一遍. 题解 这题的弱化版 ...
随机推荐
- [转]ArrayList的实现原理
1. ArrayList概述: ArrayList是List接口的可变数组的实现.实现了所有可选列表操作,并允许包括 null 在内的所有元素.除了实现 List 接口外,此类还提供一些方法来操作内部 ...
- 深入理解 Python yield
https://blog.csdn.net/lftaoyuan/article/details/78915518 python2和python3是不兼容的,通篇环境都是python3.6 简单的yie ...
- WCF入门教程(二)从零做起-创建WCF服务
通过最基本的操作看到最简单的WCF如何实现的.这是VS的SDK默认创建的样本 1.创建WCF服务库 2.看其生成结构 1)IService1.cs(协议) 定义了协议,具体什么操作,操作的参数和返回值 ...
- VGA图像显示组成模块分析
VGA图像显示组成模块分析 1.片上内存(FPGA RAM)充当存储器 2.静态内存(SRAM)充当存储器 3.将静态内存换为动态内存 动态内存容量大,但是即时能力不好,它无法立即响应VGA功能模块, ...
- Visual Studio搭建Python开发环境
一.搭建开发环境 1.创建工程: 2.下载环境: 创建好工作以后,点击运行,就会出现下面这个界面,然后点击下载,并安装 http://jingyan.baidu.com/article/fec4bce ...
- !important强制此css最强,其它被覆盖
有时我们引用别人写的插件不知道某个样式在哪儿,或者想要覆盖其它样式,就要用到 !importan; 例如以下是引用boostrap,默认bootstrap都是圆边框,我们不想用,例子中同一样式也出现多 ...
- jQuery 筛选器 链式编程操作
$('#i1').next() 下一个标签$('#i1').nextAll() 兄弟标签中,所有下一个标签$('#i1').nextUntil('#ii1') 兄弟标签中,从下一个标签到id为ii1的 ...
- C++Builder 网站。记住学习
http://www.ccrun.com/ C++Builder
- Python selenium —— selenium与自动化测试成神之路
From: https://blog.csdn.net/huilan_same/article/details/52559711 忽然想谈谈自动化的学习路径,因为发现很多人总是急于求成,不懂该如何学习 ...
- ALGO-22_蓝桥杯_算法训练_数的划分(DP)
问题描述 将整数n分成k份,且每份不能为空,任意两份不能相同(不考虑顺序). 例如:n=,k=,下面三种分法被认为是相同的. ,,; ,,; ,,; 问有多少种不同的分法. 输入格式 n,k 输出格式 ...