/*
用和模板类似的方法就行 但是实际上弱化版不用考虑匹配情况限制更加宽松, 只需要保存每个位置有无插头即可,
*/
#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的更多相关文章

  1. HDU 1693 Eat the Trees(插头DP、棋盘哈密顿回路数)+ URAL 1519 Formula 1(插头DP、棋盘哈密顿单回路数)

    插头DP基础题的样子...输入N,M<=11,以及N*M的01矩阵,0(1)表示有(无)障碍物.输出哈密顿回路(可以多回路)方案数... 看了个ppt,画了下图...感觉还是挺有效的... 参考 ...

  2. 【HDU】1693 Eat the Trees

    http://acm.hdu.edu.cn/showproblem.php?pid=1693 题意:n×m的棋盘求简单回路(可以多条)覆盖整个棋盘的方案,障碍格不许摆放.(n,m<=11) #i ...

  3. HDU 1693 Eat the Trees(插头DP)

    题目链接 USACO 第6章,第一题是一个插头DP,无奈啊.从头看起,看了好久的陈丹琦的论文,表示木看懂... 大体知道思路之后,还是无法实现代码.. 此题是插头DP最最简单的一个,在一个n*m的棋盘 ...

  4. HDU 1693 Eat the Trees

    第一道(可能也是最后一道)插头dp.... 总算是领略了它的魅力... #include<iostream> #include<cstdio> #include<cstr ...

  5. 【HDOJ】【1693】Eat The Trees

    插头DP 插头dp模板题…… 这题比CDQ论文上的例题还要简单……因为不用区分左右插头(这题可以多回路,并不是一条哈密尔顿路) 硬枚举当前位置的状态就好了>_< 题解:http://blo ...

  6. Eat the Trees hdu 1693

    Problem DescriptionMost of us know that in the game called DotA(Defense of the Ancient), Pudge is a ...

  7. HDU - 1693 Eat the Trees(多回路插头DP)

    题目大意:要求你将全部非障碍格子都走一遍,形成回路(能够多回路),问有多少种方法 解题思路: 參考基于连通性状态压缩的动态规划问题 - 陈丹琦 下面为代码 #include<cstdio> ...

  8. HDU1693 Eat the Trees 插头dp

    原文链接http://www.cnblogs.com/zhouzhendong/p/8433484.html 题目传送门 - HDU1693 题意概括 多回路经过所有格子的方案数. 做法 最基础的插头 ...

  9. 【HDU1693】Eat the Trees(插头dp)

    [HDU1693]Eat the Trees(插头dp) 题面 HDU Vjudge 大概就是网格图上有些点不能走,现在要找到若干条不相交的哈密顿回路使得所有格子都恰好被走过一遍. 题解 这题的弱化版 ...

随机推荐

  1. MyBatis 学习资料

    MyBatis 学习资料 table th:first-of-type { width: 90px; } table th:nth-of-type(2) { } table th:nth-of-typ ...

  2. 解决Ecipse和搜狗输入法快捷键冲突问题

    非常简单,关闭掉搜狗输入的所有快捷键!

  3. MySQL 5.7 修改root密码

    更新 MySQL 5.7 以后通过以下方法无法在修改root密码: ') where user='root'; 查看下MySQL的官方文档发现版本更新后原来user里的password字段已经变更为a ...

  4. php上传导入文件 nginx-502错误

    4. php程序执行时间过长而超时,检查nginx和fastcgi中各种timeout设置.(nginx 中的  fastcgi_connect_timeout 300;fastcgi_send_ti ...

  5. excel技巧--文本拆分合并

    如果像上图那样将一单元格内拆分成同等大小的字词,可用如下步骤: 1.将该单元格的宽度缩至拆分词的大小: 2.选择同列的适当的单元格,用于填充拆分的字符: 3.点击“开始”-->填充-->两 ...

  6. npm yarn安装包

  7. DS图遍历--广度优先搜索

    题目描述 代码框架如下: 输入 第一行输入t,表示有t个测试实例 第二行输入n,表示第1个图有n个结点 第三行起,每行输入邻接矩阵的一行,以此类推输入n行 第i个结点与其他结点如果相连则为1,无连接则 ...

  8. mysql 事务学习

    1.事务 逻辑上的一组操作,组成这组操作的各个逻辑单元要么一起成功,要么一起失败. 2.事务特性 原子性:强调事务的不可分割.一致性:强调的是事务的执行的前后,数据的完整性要保持一致.隔离性:一个事务 ...

  9. ALGO-120_蓝桥杯_算法训练_学做菜

    问题描述 涛涛立志要做新好青年,他最近在学做菜.由于技术还很生疏,他只会用鸡蛋,西红柿,鸡丁,辣酱这四种原料来做菜,我们给这四种原料标上字母A,B,C,D. 涛涛现在会做的菜有五种: . 西红柿炒鸡蛋 ...

  10. 使用keytool 生成数字keystore

    生成本地证书 在密钥库中生成本地数字证书:需要提供身份.加密算法.有效期等信息: keytool指令如下,产生的本地证书后缀名为:*.keystore keytool -genkeypair -key ...