BZOJ1814: Ural 1519 Formula 1(插头Dp)
Description
Input
Output
Sample Input
**..
....
....
....
Sample Output
2
解题思路:
发现一个格点一定有一进一出,就可以3进制表示括号序列轮廓线Dp就好了。
把该存的存入map/Hash表中,元素访问状态转移,要写好编码和解码。3进制改用4进制。
分类讨论一下,10种情况,画个图就好了。
方程只关心最后一个格点,可以滚动掉2维。
代码:(Hash挂链)
#include<cstdio>
#include<vector>
#include<cstring>
#include<algorithm>
typedef long long lnt;
typedef unsigned int uit;
typedef long long unt;
struct int_2{
uit x;
unt y;
int_2(){}
int_2(uit a,unt b){x=a;y=b;}
};
class Fast_map{
#define mod 2333
public:
bool find(uit x)
{
int y=x%mod;
for(int i=;i<to[y].size();i++)
if(to[y][i].x==x)
return true;
return false;
}
void ins(uit x,unt v)
{
int f=x%mod;
to[f].push_back(int_2(x,v));
return ;
}
void add(uit x,unt v)
{
int f=x%mod;
for(int i=;i<to[f].size();i++)
if(to[f][i].x==x)
{
to[f][i].y+=v;
return ;
}
return ;
}
void clear(void)
{
for(int i=;i<mod;i++)
to[i].clear();
return ;
}
void start(void)
{
jjj=;
iii=;
while(iii<mod&&!to[iii].size())
iii++;
return ;
}
bool going_on(void)
{ return iii<mod;
}
void go(void)
{
jjj++;
if(jjj<to[iii].size())
return ;
jjj=;
iii++;
while(iii<mod&&!to[iii].size())
iii++;
return ;
}
int_2 ite(void)
{
return to[iii][jjj];
}
void move(void)
{
for(int i=;i<mod;i++)
for(int j=;j<to[i].size();j++)
to[i][j].x<<=;
return ;
}
private:
std::vector<int_2>to[mod];
int iii,jjj;
#undef mod
}dp[];
int n,m;
int p,q;
int ei,ej;
unt ans;
char cmd[];
bool mp[][];
void update(int i,uit j,unt val)
{
if(!dp[i].find(j))
dp[i].ins(j,val);
else
dp[i].add(j,val);
return ;
}
uit change(uit x,int plc,uit val)
{
uit ans=x;
uit cut=0ul-1ul;
cut^=(<<(plc<<));
cut^=(<<(plc<<|));
ans&=cut;
ans|=(val<<(plc<<));
return ans;
}
int main()
{
memset(mp,,sizeof(mp));
scanf("%d%d",&n,&m);
for(int i=;i<=n;i++)
{
scanf("%s",cmd+);
for(int j=;j<=m;j++)
{
if(cmd[j]=='.')
{
mp[i][j]=true;
ei=i,ej=j;
}
}
}
p=,q=;
dp[].ins(,);
for(int i=;i<=n;i++)
{
for(int j=;j<=m;j++)
{
std::swap(p,q);
dp[p].clear();
for(dp[q].start();dp[q].going_on();dp[q].go())
{
int_2 state=dp[q].ite();
uit s=state.x;
unt v=state.y;
uit tl=(s>>((j-)<<))&;
uit tu=(s>>(j<<))&;
if(!mp[i][j])
{
if(tl|tu);else
update(p,s,v);
}else{
uit ts;
if(tl==)
{
if(tu==)
{
if(mp[i][j+]&&mp[i+][j]);else continue;
ts=change(s,j-,);
ts=change(ts,j,);
update(p,ts,v);
}else if(tu==)
{
if(mp[i+][j])
{
ts=change(s,j-,);
ts=change(ts,j,);
update(p,ts,v);
}
if(mp[i][j+])
{
ts=change(s,j-,);
ts=change(ts,j,);
update(p,ts,v);
}
}else{
if(mp[i+][j])
{
ts=change(s,j-,);
ts=change(ts,j,);
update(p,ts,v);
}
if(mp[i][j+])
{
ts=change(s,j-,);
ts=change(ts,j,);
update(p,ts,v);
}
}
}else if(tl==)
{
if(tu==)
{
if(mp[i+][j])
{
ts=change(s,j-,);
ts=change(ts,j,);
update(p,ts,v);
}
if(mp[i][j+])
{
ts=change(s,j-,);
ts=change(ts,j,);
update(p,ts,v);
}
}else if(tu==)
{
int cnt=;
for(int kt=j+;kt<=m;kt++)
{
if(((s>>(kt<<))&)==)
cnt++;
if(((s>>(kt<<))&)==)
cnt--;
if(!cnt)
{
ts=change(s,kt,);
break;
}
}
ts=change(ts,j-,);
ts=change(ts,j,);
update(p,ts,v);
}else{
if(i==ei&&j==ej)
ans+=v;
}
}else{
if(tu==)
{
if(mp[i+][j])
{
ts=change(s,j-,);
ts=change(ts,j,);
update(p,ts,v);
}
if(mp[i][j+])
{
ts=change(s,j-,);
ts=change(ts,j,);
update(p,ts,v);
}
}else if(tu==)
{
ts=change(s,j-,);
ts=change(ts,j,);
update(p,ts,v);
}else{
int cnt=-;
for(int kt=j-;kt>=;kt--)
{
if(((s>>(kt<<))&)==)
cnt++;
if(((s>>(kt<<))&)==)
cnt--;
if(!cnt)
{
ts=change(s,kt,);
break;
}
}
ts=change(ts,j-,);
ts=change(ts,j,);
update(p,ts,v);
}
}
}
}
}
dp[p].move();
}
printf("%lld\n",ans);
return ;
}
代码(map)
#include<map>
#include<cstdio>
#include<cstring>
#include<algorithm>
typedef long long lnt;
typedef unsigned int uit;
typedef long long unt;
using std::make_pair;
int n,m;
int p,q;
int ei,ej;
unt ans;
char cmd[];
bool mp[][];
std::map<uit,unt>dp[],ooo;
void update(int i,uit j,unt val)
{
if(dp[i].find(j)==dp[i].end())
dp[i][j]=val;
else
dp[i][j]+=val;
return ;
}
uit change(uit x,int plc,uit val)
{
uit ans=x;
uit cut=0ul-1ul;
cut^=(<<(plc<<));
cut^=(<<(plc<<|));
ans&=cut;
ans|=(val<<(plc<<));
return ans;
}
int main()
{
memset(mp,,sizeof(dp));
scanf("%d%d",&n,&m);
for(int i=;i<=n;i++)
{
scanf("%s",cmd+);
for(int j=;j<=m;j++)
{
if(cmd[j]=='.')
{
mp[i][j]=true;
ei=i,ej=j;
}
}
}
p=,q=;
dp[][]=;
for(int i=;i<=n;i++)
{
for(int j=;j<=m;j++)
{
std::swap(p,q);
dp[p].clear();
for(std::map<uit,unt>::iterator State=dp[q].begin();State!=dp[q].end();State++)
{
uit s=State->first;
unt v=State->second;
uit tl=(s>>((j-)<<))&;
uit tu=(s>>(j<<))&;
if(!mp[i][j])
{
if(tl|tu);else
update(p,s,v);
}else{
uit ts;
if(tl==)
{
if(tu==)
{
if(mp[i][j+]&&mp[i+][j]);else continue;
ts=change(s,j-,);
ts=change(ts,j,);
update(p,ts,v);
}else if(tu==)
{
if(mp[i+][j])
{
ts=change(s,j-,);
ts=change(ts,j,);
update(p,ts,v);
}
if(mp[i][j+])
{
ts=change(s,j-,);
ts=change(ts,j,);
update(p,ts,v);
}
}else{
if(mp[i+][j])
{
ts=change(s,j-,);
ts=change(ts,j,);
update(p,ts,v);
}
if(mp[i][j+])
{
ts=change(s,j-,);
ts=change(ts,j,);
update(p,ts,v);
}
}
}else if(tl==)
{
if(tu==)
{
if(mp[i+][j])
{
ts=change(s,j-,);
ts=change(ts,j,);
update(p,ts,v);
}
if(mp[i][j+])
{
ts=change(s,j-,);
ts=change(ts,j,);
update(p,ts,v);
}
}else if(tu==)
{
int cnt=;
for(int kt=j+;kt<=m;kt++)
{
if(((s>>(kt<<))&)==)
cnt++;
if(((s>>(kt<<))&)==)
cnt--;
if(!cnt)
{
ts=change(s,kt,);
break;
}
}
ts=change(ts,j-,);
ts=change(ts,j,);
update(p,ts,v);
}else{
if(i==ei&&j==ej)
ans+=v;
}
}else{
if(tu==)
{
if(mp[i+][j])
{
ts=change(s,j-,);
ts=change(ts,j,);
update(p,ts,v);
}
if(mp[i][j+])
{
ts=change(s,j-,);
ts=change(ts,j,);
update(p,ts,v);
}
}else if(tu==)
{
ts=change(s,j-,);
ts=change(ts,j,);
update(p,ts,v);
}else{
int cnt=-;
for(int kt=j-;kt>=;kt--)
{
if(((s>>(kt<<))&)==)
cnt++;
if(((s>>(kt<<))&)==)
cnt--;
if(!cnt)
{
ts=change(s,kt,);
break;
}
}
ts=change(ts,j-,);
ts=change(ts,j,);
update(p,ts,v);
}
}
}
}
}
ooo.clear();
for(std::map<uit,unt>::iterator j=dp[p].begin();j!=dp[p].end();j++)
if(!((j->first)>>(m<<)))
ooo[((j->first)<<)]=j->second;
dp[p].clear();
for(std::map<uit,unt>::iterator j=ooo.begin();j!=ooo.end();j++)
dp[p][j->first]=j->second;
}
printf("%lld\n",ans);
return ;
}
BZOJ1814: Ural 1519 Formula 1(插头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 ...
- 【BZOJ1814】Ural 1519 Formula 1 插头DP
[BZOJ1814]Ural 1519 Formula 1 题意:一个 m * n 的棋盘,有的格子存在障碍,求经过所有非障碍格子的哈密顿回路个数.(n,m<=12) 题解:插头DP板子题,刷板 ...
- 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 ...
- Ural 1519 Formula 1 插头DP
这是一道经典的插头DP单回路模板题. 用最小表示法来记录连通性,由于二进制的速度,考虑使用8进制. 1.当同时存在左.上插头的时候,需要判断两插头所在连通块是否相同,若相同,只能在最后一个非障碍点相连 ...
- bzoj 1814: Ural 1519 Formula 1 插头dp经典题
用的括号序列,听说比较快. 然并不会预处理,只会每回暴力找匹配的括号. #include<iostream> #include<cstdio> #include<cstr ...
- bzoj1814 Ural 1519 Formula 1(插头DP)
对插头DP的理解还不是很透彻. 先说一下肤浅的理解吧. 插头DP使用范围:指数级复杂度,且适用于解决网格图连通性问题,如哈密顿回路等问题.插头一般指每相邻2个网格的接口. 题目难度:一般不可做. 使用 ...
- 插头DP讲解+[BZOJ1814]:Ural 1519 Formula 1(插头DP)
1.什么是插头$DP$? 插头$DP$是$CDQ$大佬在$2008$年的论文中提出的,是基于状压$D$P的一种更高级的$DP$多用于处理联通问题(路径问题,简单回路问题,多回路问题,广义回路问题,生成 ...
- 【Ural】1519. Formula 1 插头DP
[题目]1519. Formula 1 [题意]给定n*m个方格图,有一些障碍格,求非障碍格的哈密顿回路数量.n,m<=12. [算法]插头DP [题解]<基于连通性状态压缩的动态规划问题 ...
随机推荐
- 如何知道 CPU 是否支持虚拟化技术(VT)
作者: Sk 译者: LCTT geekpi 我们已经知道如何检查你的 Linux 操作系统是 32 位还是 64 位以及如何知道你的 Linux 系统是物理机还是虚拟机.今天,我们将学习另一个有用的 ...
- 二、Fast-R-CNN
一.概括 Fast R-cnn的主要亮点有:Fast R-CNN将借助多任务损失函数,将物体识别和位置修正合成到一个网络中,不再对网络进行分步训练,不需要大量内存来存储训练过程中特征的数据:用RoI层 ...
- pip源
阿里云 http://mirrors.aliyun.com/pypi/simple/ 中国科技大学 https://pypi.mirrors.ustc.edu.cn/simple/ 豆瓣(do ...
- phpStudy出现You don't have permission to access / on this server.
原本用的 php 是<5.5.38版本的>,但是项目最低要求是<5.6>,所以就选择切换了版本,但是用原来的域名访问一直出现:You don't have permission ...
- [转载][来自csdn]RTS和CTS是什么意思?
原文链接: http://blog.csdn.net/zmq5411/article/details/6280332 这篇文章看着挺好,明白易懂,顺手转过来 34RTS和CTS是什么意思? 解释一:R ...
- mysql每个表总的索引大小
/* 指定的数据库 每个表的索引 不包含主键索引的大小*/ ,),,),'mb') as index_size from information_schema.tables where TABLE_S ...
- 洛谷 P1020 导弹拦截 (LIS)
第一问最长 不上升子序列,第二问最长上升子序列 套模板就好https://blog.csdn.net/qq_34416123/article/details/81358447 那个神奇的定理当作结论吧 ...
- 仿小米简约Calculator
上个星期的时候,我想教我朋友做一个简单的app.想来想去教什么比較好.当时看见小米的计算器认为比較美丽,就想这个简单.然后就開始动手做了.我以为能够一个小时能够搞定.没想到花了快一天的时间. 哎.突然 ...
- Sam做题记录
Sam做题记录 Hihocoder 后缀自动机二·重复旋律5 求一个串中本质不同的子串数 显然,答案是 \(\sum len[i]-len[fa[i]]\) Hihocoder 后缀自动机三·重复旋律 ...
- apiCloud手动检测更新
有时候需要给用户一个自主的权利,自主检测app是否是最新版本. 如何实现? 1.点击调用接口,检测是否有更新. 默认APICloud会自动检测版本更新,用户也可以在config.xml里配置autoU ...