题目链接

URAL1519

题解

看题型显然插头\(dp\)

考虑如何设计状态



有这样一个方案

当我们决策到某个位置

轮廓线长这样



你会发现插头一定是相互匹配的

所以我们实际上可以把状态用括号序列表示

如上图就是(#)()

是一个三进制数

那么我们设\(f[i][j][s]\)表示决策到\((i,j)\),轮廓线状态为\(s\)的方案数

我们同时记\(0\)为空插头,\(1\)为表示左括号的插头,\(2\)为表示有括号的插头

先不管空间问题,我们考虑一下转移

有比较多的情况

我们记\(b1\),\(b2\)为\((i,j)\)的左、上插头

\(b1 = 0,b2 = 0\)

首先如果\((i,j)\)本身是障碍格,那么它右插头和下插头也为\(0\)

否则如果对应方向没有障碍,\((i,j)\)右下插头为\(12\)

\(b1\)和\(b2\)有一者为\(0\),那么转移的时候另一个括号的位置放哪里都可以,只需要判断有无障碍

\(b1\)和\(b2\)为同一种括号,我们只需往另一侧查找匹配的括号,改变方向

例如((#))变为###()

如图所示:



把左边两个连起来,右边两个插头就变成了匹配的括号

即由((#))变为###()

\(b1 = 2\)且\(b2 = 1\)

就是(#)(#)这种情况,可以变为(####)

\(b1 = 1\)且\(b2 = 2\)

除非是最后一个格子,否则不能贸然连起来,不然就会出现不连通的情况

具体实现的时候,可以使用四进制而结合位运算加快速度

由于空间比较小,我们需要滚动数组,并且使用\(hash\)表储存状态

#include<algorithm>
#include<iostream>
#include<cstring>
#include<cstdio>
#include<cmath>
#include<map>
#include<vector>
#define Redge(u) for (int k = h[u],to; k; k = ed[k].nxt)
#define REP(i,n) for (int i = 1; i <= (n); i++)
#define mp(a,b) make_pair<int,int>(a,b)
#define cls(s) memset(s,0,sizeof(s))
#define cp pair<int,int>
#define LL long long int
using namespace std;
const int maxn = 13,maxm = 5000000,INF = 1000000000,P = 201611;
inline int read(){
int out = 0,flag = 1; char c = getchar();
while (c < 48 || c > 57){if (c == '-') flag = -1; c = getchar();}
while (c >= 48 && c <= 57){out = (out << 3) + (out << 1) + c - 48; c = getchar();}
return out * flag;
}
int n,m,sx,sy;
char S[maxn][maxn];
int now,tot[2],h[2][P],nxt[2][maxm],num[2][maxm];
LL f[2][maxm],ans;
inline void add(int s,LL x){
int tmp = s % P;
for (int k = h[now][tmp]; k; k = nxt[now][k])
if (num[now][k] == s){f[now][k] += x; return;}
nxt[now][++tot[now]] = h[now][tmp]; h[now][tmp] = tot[now];
num[now][tot[now]] = s; f[now][tot[now]] = x;
}
inline bool isok(int x,int y){
return x >= 1 && x <= n && y >= 1 && y <= m && S[x][y] != '*';
}
void work(){
int las = 1,e,s,b1,b2;
LL x;
f[0][1] = tot[0] = 1; num[0][1] = 0;
for (int i = 1; i <= n; i++){
for (int k = 1; k <= tot[now]; k++) num[now][k] <<= 2;
for (int j = 1; j <= m; j++){
now ^= 1; las ^= 1;
cls(h[now]); tot[now] = 0;
for (int k = 1; k <= tot[las]; k++){
s = num[las][k]; x = f[las][k];
b1 = (s >> (j - 1 << 1)) & 3;
b2 = (s >> (j << 1)) & 3;
e = s ^ (b1 << (j - 1 << 1)) ^ (b2 << (j << 1));
if (b1 == 0 && b2 == 0){
if (S[i][j] == '*') add(e,x);
else if (isok(i + 1,j) && isok(i,j + 1))
add(e | (1 << (j - 1 << 1)) | (2 << (j << 1)),x);
}
else if (b1 == 0){
if (isok(i,j + 1)) add(s,x);
if (isok(i + 1,j)) add(e | (b2 << (j - 1 << 1)),x);
}
else if (b2 == 0){
if (isok(i + 1,j)) add(s,x);
if (isok(i,j + 1)) add(e | (b1 << (j << 1)),x);
}
else if (b1 == 1 && b2 == 1){
int cnt = 1;
for (int p = j + 1; p <= m + 1; p++){
if ((e >> (p << 1) & 3) == 1) cnt++;
if ((e >> (p << 1) & 3) == 2) cnt--;
if (!cnt){add(e ^ (3 << (p << 1)),x); break;}
}
}
else if (b1 == 2 && b2 == 2){
int cnt = 1;
for (int p = j - 2; ~p; p--){
if ((e >> (p << 1) & 3) == 2) cnt++;
if ((e >> (p << 1) & 3) == 1) cnt--;
if (!cnt){add(e ^ (3 << (p << 1)),x); break;}
}
}
else if (b1 == 2 && b2 == 1) add(e,x);
else if (i == sx && j == sy) ans += x;
}
}
}
printf("%lld\n",ans);
}
int main(){
n = read(); m = read();
REP(i,n) scanf("%s",S[i] + 1);
REP(i,n) REP(j,m) if (S[i][j] == '.') sx = i,sy = j;
work();
return 0;
}

URAL1519 Formula 1 【插头dp】的更多相关文章

  1. URAL1519 Formula 1 —— 插头DP

    题目链接:https://vjudge.net/problem/URAL-1519 1519. Formula 1 Time limit: 1.0 secondMemory limit: 64 MB ...

  2. [URAL1519] Formula 1 [插头dp入门]

    题面: 传送门 思路: 插头dp基础教程 先理解一下题意:实际上就是要你求这个棋盘中的哈密顿回路个数,障碍不能走 看到这个数据范围,还有回路处理,就想到使用插头dp来做了 观察一下发现,这道题因为都是 ...

  3. 【BZOJ1814】Ural 1519 Formula 1 插头DP

    [BZOJ1814]Ural 1519 Formula 1 题意:一个 m * n 的棋盘,有的格子存在障碍,求经过所有非障碍格子的哈密顿回路个数.(n,m<=12) 题解:插头DP板子题,刷板 ...

  4. 【Ural】1519. Formula 1 插头DP

    [题目]1519. Formula 1 [题意]给定n*m个方格图,有一些障碍格,求非障碍格的哈密顿回路数量.n,m<=12. [算法]插头DP [题解]<基于连通性状态压缩的动态规划问题 ...

  5. bzoj1814 Ural 1519 Formula 1(插头dp模板题)

    1814: Ural 1519 Formula 1 Time Limit: 1 Sec  Memory Limit: 64 MBSubmit: 924  Solved: 351[Submit][Sta ...

  6. bzoj 1814 Ural 1519 Formula 1 ——插头DP

    题目:https://www.lydsy.com/JudgeOnline/problem.php?id=1814 普通的插头 DP .但是调了很久.注意如果合并两个 1 的话,不是 “把向右第一个 2 ...

  7. Ural 1519 Formula 1 插头DP

    这是一道经典的插头DP单回路模板题. 用最小表示法来记录连通性,由于二进制的速度,考虑使用8进制. 1.当同时存在左.上插头的时候,需要判断两插头所在连通块是否相同,若相同,只能在最后一个非障碍点相连 ...

  8. URAL Formula 1 ——插头DP

    [题目分析] 一直听说这是插头DP入门题目. 难到爆炸. 写了2h,各种大常数,ural垫底. [代码] #include <cstdio> #include <cstring> ...

  9. 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 ...

  10. BZOJ1814: Ural 1519 Formula 1(插头Dp)

    Description Regardless of the fact, that Vologda could not get rights to hold the Winter Olympic gam ...

随机推荐

  1. DDD实战成绩管理---用户故事

    本次DDD实践选取我们都熟悉的高校成绩管理作为例子. (一).需求描述 每学期学校教务处老师会进行教学安排,具体就是建立教学班,指定该教学班代课教师,上课学生,然后进行排课(忽略此部分,这是另一个系统 ...

  2. web存储机制(localStorage和sessionStorage)

    web存储包括两种:sessionStorage 和 localStorage(都是限定在文档源级别,非同源文档间无法共享) 1.sessionStorage 数据放在服务器上(IE不支持) 严格用于 ...

  3. 用 Python 3 的 async / await 做异步编程

    前年我曾写过一篇<初探 Python 3 的异步 IO 编程>,当时只是初步接触了一下 yield from 语法和 asyncio 标准库.前些日子我在 V2EX 看到一篇<为什么 ...

  4. C++ STL 全排列

    摘自爱国师哥博客https://www.cnblogs.com/aiguona/p/7304945.html 一.概念 从n个不同元素中任取m(m≤n)个元素,按照一定的顺序排列起来,叫做从n个不同元 ...

  5. C# 钱数 小写 转 大写

    public class Rmb { /// <summary> /// 转换人民币大小金额 /// </summary> /// <param name="n ...

  6. 20181120-4 Beta阶段第2周/共2周 Scrum立会报告+燃尽图 01

    此作业要求参见https://edu.cnblogs.com/campus/nenu/2018fall/homework/2409 版本控制地址   https://git.coding.net/lg ...

  7. 第六周的PSP

    本周PSP: 本周进度条: 累积进度图:: 本周PSP饼状图:

  8. 学霸系统UI部分功能规格说明书

        发布人员:软件工程实践小队    发布内容:学霸系统UI部分功能规格说明书    版本:学霸V1.1版本         ◆Part 1:引言 1.1目的    本功能规格说明书的目的在于明确 ...

  9. 基于DPDK的高效数据包捕获技术分析与应用

    被NFV的论文折磨了两天,今天上午看了两篇DPDK的综述. 传统的包捕获机制 1. BPF 两个组成部分:转发部分和过滤部分. 转发部分负责从链路层提取数据包并转发给过滤部分. 过滤部分根据过滤规则, ...

  10. windows+ubuntu时间修改问题

    只需要在ubuntu系统数输入一行指令即可 timedatectl set-local-rtc 1 --adjust-system-clock