1814: Ural 1519 Formula 1

Time Limit: 1 Sec  Memory Limit: 64 MB
Submit: 924  Solved: 351
[Submit][Status][Discuss]

Description

 一个 m * n 的棋盘,有的格子存在障碍,求经过所有非障碍格子的哈密顿回路个数

Input

The first line contains the integer numbers N and M (2 ≤ N, M ≤ 12). Each of the next N lines contains M characters, which are the corresponding cells of the rectangle. Character "." (full stop) means a cell, where a segment of the race circuit should be built, and character "*" (asterisk) - a cell, where a gopher hole is located.

Output

You should output the desired number of ways. It is guaranteed, that it does not exceed 2^63-1.

Sample Input

4 4
**..
....
....
....

Sample Output

2
分析:今天把插头dp学了一下,没想到dp还能写这么长......细节什么的也很多,在这里当一个模板吧.
#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm> using namespace std;
typedef long long ll; const int maxn = ;
const int pow[] = {,,,,,,,,,,,,};
int n,m,now,pre,tx,ty;
char map[][]; struct node
{
int head[maxn],nextt[maxn],tot;
ll sum[maxn],sta[maxn];
void clear()
{
memset(head,-,sizeof(head));
tot = ;
}
void push(ll x,ll v)
{
ll hashh = x % maxn;
for (int i = head[hashh]; i >= ; i = nextt[i])
{
if (sta[i] == x)
{
sum[i] += v;
return;
}
}
sta[tot] = x;
sum[tot] = v;
nextt[tot] = head[hashh];
head[hashh] = tot++;
}
} f[]; int turnleft(ll x,int k)
{
return x << pow[k];
} int get(ll x,int k)
{
return (x >> pow[k]) & ;
} ll del(ll x,int i,int j)
{
return x & (~( << pow[i])) & (~( << pow[j]));
} int findr(ll x,int pos)
{
int cnt = ;
for (int i = pos + ; i <= m; i++)
{
int k = get(x,i);
if (k == )
cnt++;
else if (k == )
cnt--;
if (!cnt)
return i;
}
} int findl(ll x,int pos)
{
int cnt = ;
for (int i = pos - ; i >= ; i--)
{
int k = get(x,i);
if (k == )
cnt++;
else if (k == )
cnt--;
if (!cnt)
return i;
}
} void solve2(int x,int y,int k)
{
int p = get(f[pre].sta[k],y - ); //右插头
int q = get(f[pre].sta[k],y); //下插头
ll staa = del(f[pre].sta[k],y - ,y); //将这两个插头删掉以后的状态
ll v = f[pre].sum[k];
if (!p && !q) //新建一个连通分量
{
if (map[x][y] == '*')
{
f[now].push(staa,v);
return;
}
if (x < n && y < n && map[x + ][y] == '.' && map[x][y + ] == '.')
f[now].push(staa | turnleft(,y - ) | turnleft(,y),v);
}
else if (!p || !q) //保持原来的连通分量
{
int temp = p + q;
if (x < n && map[x + ][y] == '.')
f[now].push(staa | turnleft(temp,y - ),v);
if (y < m && map[x][y + ] == '.')
f[now].push(staa | turnleft(temp,y),v);
}
else if (p == && q == ) //连接两个联通分量
f[now].push(staa ^ turnleft(,findr(staa,y)),v); //这里的异或实际上就是把1变成2,2变成1
else if (p == && q == )
f[now].push(staa ^ turnleft(,findl(staa,y - )),v);
else if (p == && q == )
f[now].push(staa,v);
else if (x == tx && y == ty)
f[now].push(staa,v);
} ll solve()
{
f[].clear();
f[].push(,);
now = ,pre = ; //滚动数组
for (int i = ; i <= n; i++)
{
pre = now;
now ^= ;
f[now].clear();
for (int k = ; k < f[pre].tot; k++)
f[now].push(turnleft(f[pre].sta[k],),f[pre].sum[k]); //左移一位,因为轮廓线下来的时候会少一个插头
for (int j = ; j <= m; j++)
{
pre = now;
now ^= ;
f[now].clear();
for (int k = ; k < f[pre].tot; k++)
solve2(i,j,k); //处理第k个状态
}
}
for (int i = ; i < f[now].tot; i++)
if (f[now].sta[i] == ) //没有插头了.
return f[now].sum[i];
return ;
} int main()
{
scanf("%d%d",&n,&m);
for(int i = ; i <= n; i++)
scanf("%s",map[i] + );
for (int i = ; i <= n; i++)
for (int j = ; j <= m; j++)
if (map[i][j] == '.')
tx = i,ty = j; //找右下角的非障碍点
if (!tx)
puts("");
else
printf("%lld\n",solve()); return ;
}

bzoj1814 Ural 1519 Formula 1(插头dp模板题)的更多相关文章

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

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

  2. bzoj 1814: Ural 1519 Formula 1 插头dp经典题

    用的括号序列,听说比较快. 然并不会预处理,只会每回暴力找匹配的括号. #include<iostream> #include<cstdio> #include<cstr ...

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

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

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

  5. Ural 1519 Formula 1 插头DP

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

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

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

  7. 插头DP讲解+[BZOJ1814]:Ural 1519 Formula 1(插头DP)

    1.什么是插头$DP$? 插头$DP$是$CDQ$大佬在$2008$年的论文中提出的,是基于状压$D$P的一种更高级的$DP$多用于处理联通问题(路径问题,简单回路问题,多回路问题,广义回路问题,生成 ...

  8. bzoj1814 Ural 1519 Formula 1(插头DP)

    对插头DP的理解还不是很透彻. 先说一下肤浅的理解吧. 插头DP使用范围:指数级复杂度,且适用于解决网格图连通性问题,如哈密顿回路等问题.插头一般指每相邻2个网格的接口. 题目难度:一般不可做. 使用 ...

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

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

随机推荐

  1. flask的模板

    flask用的是jinja2的模板 模板其实是一个包含响应文本的文件,其中用占位符(变量)表示动态部分,告诉模板引擎其具体的值需要从使用的数据中获取 使用真实值替换变量,再返回最终得到的字符串,这个过 ...

  2. 标记编码报错ValueError: bad input shape ()

    <Python机器学习经典实例>2.9小节中,想自己动手实践汽车特征评估质量,所以需要对数据进行预处理,其中代码有把字符串标记编码为对应的数字,如下代码 input_data = ['vh ...

  3. shell重温---基础篇(shell数组&数组操作)

    上篇博客已经分析重温了shell的运行方式以及其中的变量还有字符串,之后按照套路就是数组方面了,废话不多说,直接进入正题哈.(小白笔记,各位看官勿喷...)     bash shell呢,支持一位数 ...

  4. atlas+mysql主主集群实现读写分离

     atlas+mysql主主集群实现读写分离 前言: 目前线上系统数据库采用的是主主架构.其中一台主仅在故障时切换使用,(仅单台服务器对外提供服务,当一台出现问题,切换至另一台).该结构很难支撑较大并 ...

  5. springboot升级到2.x需要改动的地方

    由于需要跟进技术发展的脚步,对原有项目springboot进行2.0升级,但升级并不是说改一下版本就完事了,springboot2.0变动比较多,详细变化可以百度一下,下面针对升级springboot ...

  6. C# List集合去重操作注意点

    今天调试代码时发现list的distinct方法在对引用类型操作时并没有去重,后来查阅资料发现list去重操作对象集合时比较的是对象的一个个引用地址, 因为集合里的对象都是一个个单独的实例,所以并不会 ...

  7. 数据迁移的应用场景与解决方案Hamal

    本文来自网易云社区 作者:马进 跑男热播,作为兄弟团忠实粉丝,笔者也是一到周五就如打鸡血乐不思蜀. 看着银幕中一众演员搞怪搞笑的浮夸演技,也时常感慨,这样一部看似简单真情流露的真人秀,必然饱含了许许多 ...

  8. C++学习008-delete与delete[]的差别

    对于简单的数据类型,delete与delete[]是没啥差别的,就是等价的 例如 int main() { int *pdata = new int[20]; delete pdata; //dele ...

  9. 局部敏感哈希LSH

    之前介绍了Annoy,Annoy是一种高维空间寻找近似最近邻的算法(ANN)的一种,接下来再讨论一种ANN算法,LSH局部敏感哈希. LSH的基本思想是: 原始空间中相邻的数据点通过映射或投影变换后, ...

  10. Leetcode 672.灯泡开关II

    灯泡开关II 现有一个房间,墙上挂有 n 只已经打开的灯泡和 4 个按钮.在进行了 m 次未知操作后,你需要返回这 n 只灯泡可能有多少种不同的状态. 假设这 n 只灯泡被编号为 [1, 2, 3 . ...