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 [题解]<基于连通性状态压缩的动态规划问题 ...
随机推荐
- logsource and ALO
1.首先配置sourcedb上的nfs服务,oggstd上挂载sourcedb的online redo和archive log的目录 oggsource上配置: vi /etc/export ...
- Python对象引用的所有权
目录 引用所有权 传递引用的所有权--返回值 出借引用的所有权--返回值 占据引用的所有权--参数 出借引用的所有权--参数 引用所有权 谁持有对象引用的所有权,谁就要对对象负责. 引用的所有权对函数 ...
- Systemd曝3漏洞,大部分Linux将受到攻击
Linux 系统与服务管理工具 Systemd 被曝存在 3 大漏洞,影响几乎所有 Linux 发行版. Systemd 是 Linux 系统的基本构建块,它提供了对系统和服务的管理功能,以 PID ...
- neo4j nosql图数据库学习
neo4j 文档:https://neo4j.com/docs/getting-started/current/cypher-intro/ 1.索引 # 给某类标签节点的属性添加索引 CREATE I ...
- python IO编程-StringIO和BytesIO
链接:https://www.liaoxuefeng.com/wiki/0014316089557264a6b348958f449949df42a6d3a2e542c000/0014319187857 ...
- [SDOI2011]消防(树的直径)
[SDOI2011]消防 题目描述 某个国家有n个城市,这n个城市中任意两个都连通且有唯一一条路径,每条连通两个城市的道路的长度为zi(zi<=1000). 这个国家的人对火焰有超越宇宙的热情, ...
- Object-C,四则运算计算器
下面是是一个比较复杂的类. 定义一个四则运算计算器Caculator的接口和实现. 在main函数中,让用户输入四则运算表达式,比如a+b,a-b. 最后,在控制台输出结果. 用到的语法:接口.类的定 ...
- C++ 输出缓冲区的管理
在C++中,每个I/O对象管理一个缓冲区,用于存储程序读写的数据.本文将对输出缓冲区的管理进行简单的讲解. 举一个简单的例子: myOs << "Please enter a v ...
- mysql(for update)悲观锁总结与实践
悲观锁,正如其名,它指的是对数据被外界(包括本系统当前的其他事务,以及来自外部系统的事务处理)修改持保守态度,因此,在整个数据处理过程中,将数据处于锁定状态.悲观锁的实现,往往依靠数据库提供的锁机制( ...
- usb芯片调试经验
记录一下调试usb有关的芯片的一些经验. 1.有i2c的芯片.一般有i2c的地址选择. 检查地址选择是否正确,地址是多少.SCL和SDA上面是否有上拉电阻. 芯片的地址是几位的.I2c的时钟频率也是必 ...