Ural 1519 Formula 1 插头DP
这是一道经典的插头DP单回路模板题。
用最小表示法来记录连通性,由于二进制的速度,考虑使用8进制。
1、当同时存在左、上插头的时候,需要判断两插头所在连通块是否相同,若相同,只能在最后一个非障碍点相连;若不相同,则把这两个连通块连起来。
2、如果只存在左或上插头的时候,则要延续连通块。
3、若都不存在左和上插头的时候,就要新建一个连通块。
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <string>
#include <algorithm> using namespace std; #define REP(i, a, b) for (int i = (a), i##_end_ = (b); i <= i##_end_; ++i)
#define DWN(i, a, b) for (int i = (a), i##_end_ = (b); i >= i##_end_; --i)
#define mset(a, b) memset(a, b, sizeof(a))
typedef long long LL;
const int MAXD = , HASH = , STATE = ;
int n, m, maze[MAXD][MAXD], code[MAXD], ch[MAXD], end_x, end_y;
char str[MAXD];
struct HASHMAP
{
int head[HASH], nxt[STATE], siz; LL f[STATE], state[STATE];
void clear() { siz = , mset(head, -); }
void push(LL x, LL add)
{
int pos = x%HASH, i = head[pos];
for (; i != -; i = nxt[i])
if (state[i] == x) { f[i] += add; return ; }
state[siz] = x, f[siz] = add;
nxt[siz] = head[pos], head[pos] = siz++;
}
}hm[]; void in()
{
scanf("%d %d", &n, &m), mset(maze, ), end_x = end_y = -;
REP(i, , n)
{
scanf("%s", str+);
REP(j, , m)
if (str[j] == '.')
maze[i][j] = , end_x = i, end_y = j;
}
} void decode(LL x)
{
REP(i, , m) code[i] = x&, x >>= ;
} LL encode()
{
LL ret = ; int cnt = ;
mset(ch, -), ch[] = ;
DWN(i, m, )
{
if (ch[code[i]] == -) ch[code[i]] = ++cnt;
ret <<= , ret |= ch[code[i]];
}
return ret;
} void shift(int j)
{
if (j != m) return ;
DWN(i, m, ) code[i] = code[i-];
code[] = ;
} void dp_blank(int i, int j, int cur)
{
REP(k, , hm[cur].siz-)
{
decode(hm[cur].state[k]);
int lef = code[j-], up = code[j];
if (lef && up)
{
if (lef == up && !(i == end_x && j == end_y)) continue ;
REP(t, , m)
if (code[t] == up) code[t] = lef;
code[j-] = code[j] = , shift(j);
hm[cur^].push(encode(), hm[cur].f[k]);
}
else
if (lef || up)
{
int t = lef ? lef : up;
if (maze[i][j+])
{
code[j-] = , code[j] = t;
hm[cur^].push(encode(), hm[cur].f[k]);
}
if (maze[i+][j])
{
code[j-] = t, code[j] = , shift(j);
hm[cur^].push(encode(), hm[cur].f[k]);
}
}
else
if (maze[i][j+] && maze[i+][j])
{
code[j-] = code[j] = , shift(j);
hm[cur^].push(encode(), hm[cur].f[k]);
}
}
} void dp_block(int i, int j, int cur)
{
REP(k, , hm[cur].siz-)
{
decode(hm[cur].state[k]), shift(j);
hm[cur^].push(encode(), hm[cur].f[k]);
}
} void work()
{
int cur = ; LL ans = ;
hm[].clear(), hm[].clear(), hm[cur].push(, );
REP(i, , n)
REP(j, , m)
{
if (maze[i][j]) dp_blank(i, j, cur);
else dp_block(i, j, cur);
hm[cur].clear(), cur ^= ;
}
REP(i, , hm[cur].siz-) ans += hm[cur].f[i];
printf("%I64d\n", ans);
} int main()
{
in();
work();
return ;
}
Ural 1519 Formula 1 插头DP的更多相关文章
- 【BZOJ1814】Ural 1519 Formula 1 插头DP
[BZOJ1814]Ural 1519 Formula 1 题意:一个 m * n 的棋盘,有的格子存在障碍,求经过所有非障碍格子的哈密顿回路个数.(n,m<=12) 题解:插头DP板子题,刷板 ...
- bzoj1814 Ural 1519 Formula 1(插头dp模板题)
1814: Ural 1519 Formula 1 Time Limit: 1 Sec Memory Limit: 64 MBSubmit: 924 Solved: 351[Submit][Sta ...
- bzoj 1814 Ural 1519 Formula 1 插头DP
1814: Ural 1519 Formula 1 Time Limit: 1 Sec Memory Limit: 64 MBSubmit: 942 Solved: 356[Submit][Sta ...
- bzoj 1814 Ural 1519 Formula 1 ——插头DP
题目:https://www.lydsy.com/JudgeOnline/problem.php?id=1814 普通的插头 DP .但是调了很久.注意如果合并两个 1 的话,不是 “把向右第一个 2 ...
- BZOJ1814: Ural 1519 Formula 1(插头Dp)
Description Regardless of the fact, that Vologda could not get rights to hold the Winter Olympic gam ...
- bzoj 1814: Ural 1519 Formula 1 插头dp经典题
用的括号序列,听说比较快. 然并不会预处理,只会每回暴力找匹配的括号. #include<iostream> #include<cstdio> #include<cstr ...
- 【Ural】1519. Formula 1 插头DP
[题目]1519. Formula 1 [题意]给定n*m个方格图,有一些障碍格,求非障碍格的哈密顿回路数量.n,m<=12. [算法]插头DP [题解]<基于连通性状态压缩的动态规划问题 ...
- 【BZOJ1814】Ural 1519 Formula 1 (插头dp)
[BZOJ1814]Ural 1519 Formula 1 (插头dp) 题面 BZOJ Vjudge 题解 戳这里 上面那个链接里面写的非常好啦. 然后说几个点吧. 首先是关于为什么只需要考虑三进制 ...
- ural 1519 Formula 1(插头dp)
1519. Formula 1 @ Timus Online Judge 干了一天啊!!!插头DP入门. 代码如下: #include <cstdio> #include <cstr ...
随机推荐
- jQuery的validation插件(验证表单插件)
更完整的参考:http://www.runoob.com/jquery/jquery-plugin-validate.html 验证隐藏字段的使用(验证通过后ajax提交表单):http://www. ...
- 读书笔记 effective c++ Item 3 在任何可能的时候使用 const
Const可以修饰什么? Const 关键字是万能的,在类外部,你可以用它修饰全局的或者命名空间范围内的常量,也可以用它来修饰文件,函数和块作用域的静态常量.在类内部,你可以使用它来声明静态或者非 ...
- Dev Express 安装
Dev Express 安装 点击DevExpressUniversalTrialComplete-20151209.exe开始安装 选择需要安装的产品 选择需要安装的产品目录,这里设置为D盘 开 ...
- ansible command模块将返回值写入变量
ansible 中command模块支持 register参数将远程命令执行的输出结果存储在变量中,后续可以在when中对该变量进行检索确定下一步任务. --- - name: cat /etc/re ...
- java中参数传递--值传递,引用传递
java中的参数传递——值传递.引用传递 参数是按值而不是按引用传递的说明 Java 应用程序有且仅有的一种参数传递机制,即按值传递. 在 Java 应用程序中永远不会传递对象,而只传递对象引用. ...
- (三)Spring 之AOP 详解
第一节:AOP 简介 AOP 简介:百度百科: 面向切面编程(也叫面向方面编程):Aspect Oriented Programming(AOP),是软件开发中的一个热点,也是Spring框架中的一个 ...
- Django之进阶相关操作
一.QuerySet的特点 1.可切片 使用Python 的切片语法来限制查询集记录的数目 .它等同于SQL 的LIMIT 和OFFSET 子句. 1 >>> Entry.objec ...
- linux中使用vim编译C++程序
Vi三种模式详解 命令行模式 (command mode/一般模式) 任何时候,不管用户处于何种模式,只要按一下“ESC”键,即可使Vi进入命令行模式:我们在shell环境(提示符为$)下输入启动Vi ...
- scrapy 学习笔记2
本章学习爬虫的 回调和跟踪链接 使用参数 回调和跟踪链接 上一篇的另一个爬虫,这次是为了抓取作者信息 # -*- coding: utf-8 -*- import scrapy class Myspi ...
- Windows 8.1 操作系统常用快捷键
安装了 windows 8.1 有一段时间了,刚使用时有点儿不太习惯,后面知道了一些常用快捷键后,使用起来习惯多了.下面是一些常用的 Windows 8.1 快捷键: Ctrl + Tab: 访问所有 ...