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 [题解]<基于连通性状态压缩的动态规划问题 ...
随机推荐
- CDH5.14操作指南
1.Cdh 5.14介绍 2.版本发布概要 3.安装要求 4.Cloudera Manager介绍 5.Cloudera 安装 6.Cloudera 管理 7.Cloudera Navigator C ...
- Python3基础笔记--生成器
目录: 一.列表生成器 二.生成器 三.迭代器 一.列表生成器 a = [x for x in range(10)] b= [y*2 for y in range(10)] def f(n) retu ...
- python(1)处理图像
一提到数字图像处理,可能大多数人就会想到matlab,但matlab也有自身的缺点: 1.不开源,价格贵 2.软件容量大.一般3G以上,高版本甚至达5G以上. 3.只能做研究,不易转化成软件. 因此, ...
- 在 yii2.0 框架中封装导出html 表格样式 Excel 类
在 vendor/yiisoft/yii2/helpers/ 创建一个 Excel.php <?php namespace yii\helpers; class Excel{ ...
- iOS开发——NSString小结
1.创建常量字符串. NSString *astring = @"This is a String!"; 2.创建空字符串,给予赋值. NSString *astring = ...
- (转载)详细图解mongodb下载、安装、配置与使用
记得在管理员模式下运行CMD,否则服务将启动失败 转载:http://blog.csdn.net/boby16/article/details/51221474 详细图解,记录 win7 64 安装m ...
- caioj 1072 动态规划入门(二维一边推5:最长公共子序列 LCSS加强版)
在51nod刷到过同样的题,直接秒杀 见https://blog.csdn.net/qq_34416123/article/details/81697683 #include<cstdio> ...
- 1193 Eason
Eason Acceteped : 57 Submit : 129 Time Limit : 1000 MS Memory Limit : 65536 KB Description 题目描述 ...
- linux虚拟机拓展大小
http://blog.csdn.net/wutong_login/article/details/40147057?utm_source=tuicool http://www.linuxidc.co ...
- Spring可扩展Schema标签
基于Spring可扩展Schema提供自己定义配置支持 http://blog.csdn.net/cutesource/article/details/5864562 WARN : org.sprin ...