【BZOJ1814】Ural 1519 Formula 1

题意:一个 m * n 的棋盘,有的格子存在障碍,求经过所有非障碍格子的哈密顿回路个数。(n,m<=12)

题解:插头DP板子题,刷板子,附带题解链接

如何存放状态呢?可以采用hash,我们的hash表形如一个队列,每次新加入一个状态时,就沿着这个状态在队列中对应的hash值不断向后找,直到找到这个状态或者发现一个空位为止。

本题我的状态采用了4进制表示。

#include <cstdio>
#include <cstring>
#include <iostream>
using namespace std;
typedef long long ll;
const int limit=99991;
int n,m,k,tot[2],nn,mm;
bool v[20][20];
char str[20];
ll tag,ans;
int hs[limit],state[2][limit];
ll dp[2][limit];
inline void upd(ll S)
{
register int pos=S%limit;
while(hs[pos])
{
if(state[k][hs[pos]]==S)
{
dp[k][hs[pos]]+=tag;
return ;
}
pos++;
if(pos==limit) pos=0;
}
hs[pos]=++tot[k],state[k][tot[k]]=S,dp[k][tot[k]]=tag;
}
int main()
{
register int i,j,t,u,tmp,p,q,x,y,S,T;
scanf("%d%d",&n,&m);
for(i=1;i<=n;i++)
{
scanf("%s",str+1);
for(j=1;j<=m;j++)asdasd
{
v[i][j]=str[j]=='.';
if(v[i][j]) nn=i,mm=j;
}
}
tot[0]=1,state[0][1]=0,dp[0][1]=1;
for(i=1;i<=n;i++)
{
for(j=1;j<=m;j++)
{
k^=1;
memset(hs,0,sizeof(hs));
memset(state[k],0,sizeof(state[k][0])*(tot[k]+1));
memset(dp[k],0,sizeof(dp[k][0])*(tot[k]+1));
tot[k]=0;
for(t=1;t<=tot[k^1];t++)
{
S=state[k^1][t],tag=dp[k^1][t],y=j<<1,x=y-2,p=(S>>x)&3,q=(S>>y)&3,T=S^(p<<x)^(q<<y);
if(!v[i][j])
{
if(!p&&!q) upd(T);
continue;
}
if(p==0&&q==0&&v[i][j+1]&&v[i+1][j])
{
upd(T|(1<<x)|(2<<y));
continue;
}
if((p==0&&q==1)||(p==1&&q==0))
{
if(v[i+1][j]) upd(T|(1<<x));
if(v[i][j+1]) upd(T|(1<<y));
continue;
}
if((p==0&&q==2)||(p==2&&q==0))
{
if(v[i+1][j]) upd(T|(2<<x));
if(v[i][j+1]) upd(T|(2<<y));
continue;
}
if(p==2&&q==1)
{
upd(T);
continue;
}
if(p==1&&q==2&&i==nn&&j==mm)
{
ans+=tag;
continue;
}
if(p==1&&q==1)
{
for(tmp=0,u=y+2;u<=m+m&&tmp>=0;tmp+=((T>>u)&1)-((T>>(u+1))&1),u+=2);
u-=2,upd(T^(3<<u));
continue;
}
if(p==2&&q==2)
{
for(tmp=0,u=x-2;u>=0&&tmp>=0;tmp+=((T>>(u+1))&1)-((T>>u)&1),u-=2);
u+=2,upd(T^(3<<u));
continue;
}
}
}
for(t=1;t<=tot[k];t++) state[k][t]<<=2;
}
printf("%lld",ans);
return 0;
}

【BZOJ1814】Ural 1519 Formula 1 插头DP的更多相关文章

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

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

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

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

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

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

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

  5. Ural 1519 Formula 1 插头DP

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

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

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

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

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

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

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

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

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

随机推荐

  1. 安卓开发笔记——打造万能适配器(Adapter)

    为什么要打造万能适配器? 在安卓开发中,用到ListView和GridView的地方实在是太多了,系统默认给我们提供的适配器(ArrayAdapter,SimpleAdapter)经常不能满足我们的需 ...

  2. Markdown编辑器Editor.md使用方式

    摘要: 搭建个人博客时,涉及文章上传,文章展示,这里需要一个Markdown插件,mark一下. Editormd下载地址:http://pandao.github.io/editor.md/ 由于前 ...

  3. sar监控工具详解

    转自http://www.cnblogs.com/Amaranthus/p/3745680.html SAR NAME: SAR报告,收集,保存系统活动信息 语法: sar  [ -A ] [ -b ...

  4. zabbix监控系列(5)之通过trap模式监控网络设备

  5. Ubuntu下用matplotlib作图时显示中文

    之前在Ubuntu下用matplotlib作图的时候发现无法正常显示中文,查了一番以后发现是Ubuntu系统和matplotlib库没有共同可显示的中文字体库的原因.用此文章的方法可以解决这一问题. ...

  6. LINE@生活圈招募好友秘笈

    什么是「获得更多好友」页面? 您可从  LINE@ app >管理>获得更多好友  进入此页面. ▼ 「获得更多好友」新介面中,募集好友的四大秘诀 秘诀一.「以社群网站或电子邮件分享」 • ...

  7. SpringBoot------热部署(Springloaded)

    为啥要热部署: 在修改代码的时候,不需要重新启动程序,程序会自动进行编译 注意: 控制器中新增加的方法是不能进行热部署的 方法: 1.在pom.xml文件里面添加下面代码 <project> ...

  8. hive-数据模型

    hive支持四种数据模型 • external table• table• partition• bucket 为了避免table名称冲突,hive用database作为顶层域名,如果不设定datab ...

  9. FFMPEG AVRational

    FFMPEG的很多结构中有AVRational time_base;这样的一个成员,它是AVRational结构的 typedef struct AVRational{ int num; ///< ...

  10. Python学习(21):Python函数(5):变量作用域与闭包

    转自 http://www.cnblogs.com/BeginMan/p/3179040.html 一.全局变量与局部变量 一个模块中,最高级别的变量有全局作用域. 全局变量一个特征就是:除非被删除, ...