题意:给一个n*m的矩阵,格子中是'*'则是障碍格子,不允许进入,其他格子都是必走的格子,所走格子形成一条哈密顿回路,问有多少种走法?

思路:

  本来是很基础的题,顿时不知道进入了哪个坑。这篇插头DP的文章够详细,推荐一看(最好不要照抄代码)。

  细节要小心,比如输出用I64d,必须用long long,判断是否无哈密顿回路,位操作等等。

  这次仍然用括号表示法,1表示(,2表示)。

 #include <bits/stdc++.h>
#define pii pair<int,int>
#define INF 0x3f3f3f3f
#define LL long long
using namespace std;
const int N=;
const int mod=;
const int NN=;
char g[N][N];
int cur, n, m, ex, ey;
struct Hash_Map
{
int head[mod]; //桶指针
int next[NN]; //记录链的信息
int status[NN]; //状态
LL value[NN]; //状态对应的DP值。
int size; void clear() //清除哈希表中的状态
{
memset(head, -, sizeof(head));
size = ;
} void insert(int st, LL val) //插入状态st的值为val
{
int h = st%mod;
for(int i=head[h]; i!=-; i=next[i])
if(status[i] == st) //这个状态已经存在,累加进去。
{
value[i] += val;
return ;
}
status[size]= st; //找不到状态st,则插入st。
value[size] = val;
next[size] = head[h] ; //新插入的元素在队头
head[h] = size++;
}
}hashmap[]; int getbit(int s,int pos) //取出状态s的第pos个插头
{
int bit=;
if(s&(<<(*pos+))) bit+=; //高位对应高位
if(s&(<<*pos)) bit+=;
return bit;
}
int setbit(int s,int pos,int bit) //将状态s的第pos个插头设置为bit
{
if(s&(<<*pos )) s^=<<(*pos);
if(s&(<<(*pos+))) s^=<<(*pos+);
return (s|(bit<<*pos));
} int Fr(int s,int pos,int bit) //寻找状态s的第pos个插头对应的右括号。
{
int cnt=;
for(pos+=; pos<m; pos++)
{
if(getbit(s, pos)==-bit) cnt++;
if(getbit(s, pos)==bit) cnt--;
if(cnt==-) return setbit(s, pos, -bit);
}
}
int Fl(int s,int pos,int bit) //寻找状态s的第pos个插头对应的左括号。
{
int cnt=;
for(pos--; pos>=; pos--)
{
if(getbit(s, pos)==-bit) cnt++;
if(getbit(s, pos)==bit) cnt--;
if( cnt==-) return setbit(s, pos, -bit);
}
} void DP(int i,int j) //非障碍空格
{
for(int k=,t; k<hashmap[cur^].size; k++)
{
int s=hashmap[cur^].status[k];
LL v=hashmap[cur^].value[k];
int R=getbit(s,j), D=getbit(s,j+);
if(g[i][j]=='*') //障碍格子
{
if( R== && D== ) hashmap[cur].insert(s, v);
continue ;
}
if(R && D) //两个括号
{
t=(setbit(s,j,)&setbit(s,j+,));
if(R==D) //同个方向的括号
{
if(R==) t=Fr(t, j, ); //要改
else t=Fl(t, j, );
hashmap[cur].insert(t, v);
}
if( R== && D== ) //不同的连通分量
hashmap[cur].insert(t, v);
if(i==ex && j==ey && R== && D== ) //终点时'('和')'才可以合并。
hashmap[cur].insert(t, v);
}
else if(R || D) //仅1个括号
{
hashmap[cur].insert(s,v);
if(R) t=setbit(setbit(s,j,),j+,R);
else t=setbit(setbit(s,j+,),j,D);
hashmap[cur].insert(t,v);
}
else //无括号
hashmap[cur].insert( setbit(s,j,)|setbit(s,j+,), v);
}
} void cal()
{
if(ex==-) return ; //无空格
for(int i=; i<n; i++)
{
cur^=;
hashmap[cur].clear();
for(int j=; j<hashmap[cur^].size; j++) //新行,需要左移一下状态。
if( getbit( hashmap[cur^].status[j], m)== ) //多余的状态需要去除
hashmap[cur].insert( hashmap[cur^].status[j]<<, hashmap[cur^].value[j] );
for(int j=; j<m; j++)
{
cur^=;
hashmap[cur].clear();
DP(i,j);
if(i==ex && j==ey) return ; //终点
}
}
} LL print()
{
for(int i=; i<hashmap[cur].size; i++) //寻找轮廓线状态为0的值。
if( hashmap[cur].status[i]== )
return hashmap[cur].value[i];
return ;
}
int main()
{
//freopen("input.txt", "r", stdin);
while(~scanf("%d%d",&n,&m))
{
ex=ey=-;
cur=;
for(int i=; i<n; i++) //输入矩阵
for(int j=; j<m; j++)
{
char c=getchar();
if(c=='.'||c=='*')
{
g[i][j]=c;
if( c=='.' ) ex=i,ey=j;//终点空格
}
else j--;
} hashmap[cur].clear();
hashmap[cur].insert(, ); //初始状态
cal();
cout<<print()<<endl;
}
return ;
}

AC代码

URAL 1519 Formula 1 (插头DP,常规)的更多相关文章

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

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

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

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

  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. BZOJ1814: Ural 1519 Formula 1(插头Dp)

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

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

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

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

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

  9. 【BZOJ1814】Ural 1519 Formula 1 (插头dp)

    [BZOJ1814]Ural 1519 Formula 1 (插头dp) 题面 BZOJ Vjudge 题解 戳这里 上面那个链接里面写的非常好啦. 然后说几个点吧. 首先是关于为什么只需要考虑三进制 ...

随机推荐

  1. window.open全屏

    window.open全屏   1. window.open(url,'资金计划项超支提醒','width='+(window.screen.availWidth-10)+',height='+(wi ...

  2. CCF 201509-1 数列分段 (水题)

    问题描述 给定一个整数数列,数列中连续相同的最长整数序列算成一段,问数列中共有多少段? 输入格式 输入的第一行包含一个整数n,表示数列中整数的个数. 第二行包含n个整数a1, a2, …, an,表示 ...

  3. 洛谷 - P1593 - 因子和 - 费马小定理

    类似的因为模数比较小的坑还有卢卡斯定理那道,也是有时候逆元会不存在,因为整除了.使用一些其他方法避免通过逆元. https://www.luogu.org/fe/problem/P1593 有坑.一定 ...

  4. 3d工具收集

    Poser 是Metacreations公司推出的一款三维动物.人体造型和三维人体动画制作的极品软件.用过Poser 2与Poser 3的朋友一定能感受到Poser的人体设计和动画制作是那么的轻松自如 ...

  5. luogu P3600 随机数生成器【dp】

    把期望改成方案数最后除一下,设h[i]为最大值恰好是i的方案数,那么要求的就是Σh[i]*i 首先包含其他区间的区间是没有意义的,用单调栈去掉 然后恰好不好求,就改成h[i]表示最大值最大是i的方案数 ...

  6. bzoj 2007: [Noi2010]海拔【最小割+dijskstra】

    上来就跑3e5的最大流--脑子抽了 很容易看出,每个地方的海拔都是0或1因为再高了没有意义,又,上去下来再上去没有意义,所以最后一定是从s连着一片0,剩下连着t一片1,然后有贡献的就是01交接的那些边 ...

  7. 阿里云物联网 .NET Core 客户端 | CZGL.AliIoTClient:1. 连接阿里云物联网

    文档目录: 说明 1. 连接阿里云物联网 2. IoT 客户端 3. 订阅Topic与响应Topic 4. 设备上报属性 4.1 上报位置信息 5. 设置设备属性 6. 设备事件上报 7. 服务调用 ...

  8. OFFICE 365 A1 Plus账号注册

    OFFICE365 A1 Plus账号注册 Office2019与Office365专业增强版之间的区别: Office2019是一次性购买,不会在购买后接收功能更新,但会根据需要接收质量和安全修补程 ...

  9. Hexo - Template render error unexpected token

    问题与分析 今天发现在使用hexo g时报错如下: FATAL Something's wrong. Maybe you can find the solution here: http://hexo ...

  10. Django (五) modeld进阶

    day 05 models进阶 1.models基本操作   django中遵循 Code Frist 的原则,即:根据代码中定义的类来自动生成数据库表. 对于ORM框架里: 我们写的类表示数据库的表 ...