Description

Regardless of the fact, that Vologda could not get rights to hold the Winter Olympic games of 20**, it is well-known, that the city will conduct one of the Formula 1 events. Surely, for such an important thing a new race circuit should be built as well as hotels, restaurants, international airport - everything for Formula 1 fans, who will flood the city soon. But when all the hotels and a half of the restaurants were built, it appeared, that at the site for the future circuit a lot of gophers lived in their holes. Since we like animals very much, ecologists will never allow to build the race circuit over the holes. So now the mayor is sitting sadly in his office and looking at the map of the circuit with all the holes plotted on it. Problem Who will be smart enough to draw a plan of the circuit and keep the city from inevitable disgrace? Of course, only true professionals - battle-hardened programmers from the first team of local technical university!.. But our heroes were not looking for easy life and set much more difficult problem: "Certainly, our mayor will be glad, if we find how many ways of building the circuit are there!" - they said. It should be said, that the circuit in Vologda is going to be rather simple. It will be a rectangle N*M cells in size with a single circuit segment built through each cell. Each segment should be parallel to one of rectangle's sides, so only right-angled bends may be on the circuit. At the picture below two samples are given for N = M = 4 (gray squares mean gopher holes, and the bold black line means the race circuit). There are no other ways to build the circuit here. 一个 m * n 的棋盘,有的格子存在障碍,求经过所有非障碍格子的哈密顿回路个数

Input

The first line contains the integer numbers N and M (2 ≤ N, M ≤ 12). Each of the next N lines contains M characters, which are the corresponding cells of the rectangle. Character "." (full stop) means a cell, where a segment of the race circuit should be built, and character "*" (asterisk) - a cell, where a gopher hole is located.

Output

You should output the desired number of ways. It is guaranteed, that it does not exceed 2^63-1.

Sample Input

4 4
**..
....
....
....

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)的更多相关文章

  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

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

  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. Java调用Python遇到的一系列问题与解决方案

    首先,百度了几个方法 1.用jython里的一个jar包,jython.jar,里面封装了一个专门调用Python的类, 但是不知道为什么我用Java一调用就报错,因此放弃.   2.用runtime ...

  2. 服务器TCP连接中 TIME_WAIT 状态过多

    今天查看服务器的TCP连接数,发现其中 TIME_WAIT 状态的太多了: # netstat -n | awk '/^tcp/ {++S[$NF]} END {for(a in S) print a ...

  3. UI Framework-1: Aura Graphics Architecture

    Graphics Architecture Overview Each Aura Window owns a corresponding compositor layer. The layer tre ...

  4. Debian9.5 系统配置NFS配置说明

    NFS是Network File System的缩写,即网络文件系统.它的主要功能是通过网络(一般是局域网)让不同的主机系统之间可以共享文件或目录.NFS客户端可以通过挂载(mount)的方式将NFS ...

  5. parted---磁盘分区

    parted命令是由GNU组织开发的一款功能强大的磁盘分区和分区大小调整工具,与fdisk不同,它支持调整分区的大小.作为一种设计用于Linux的工具,它没有构建成处理与fdisk关联的多种分区类型, ...

  6. 【Henu ACM Round#19 E】 Om Nom and Candies

    [链接] 我是链接,点我呀:) [题意] 在这里输入题意 [题解] 紫书上的原题: 链接 [代码] #include <bits/stdc++.h> #define ll long lon ...

  7. 【UVa 12563】Jin Ge Jin Qu hao

    [Link]: [Description] KTV给你T秒的唱歌时间; 你有n首一定要唱的歌; 然后有一首很变态的歌有678s,你想在T秒结束之前唱一下这首歌; 因为这样的话,你能尽量晚地走出KTV( ...

  8. Java排序算法(二):简单选择排序

    [基本思想] 在要排序的一组数中.选出最小的一个数与第一个位置的数交换:然后在剩下的数中再找出最小的与第二个位置的数交换,如此循环至倒数第二个数和最后一个数比較为止. 算法关键:找到最小的那个数.并用 ...

  9. javascript对象如何使用

    javascript对象如何使用 一.总结 一句话总结:JavaScript 中的所有事物都是对象:字符串.数值.数组.函数... 因为函数是对象,所以自定义对象的创建中有种方法就是函数 1.js中的 ...

  10. 解决plsql遇到乱码的问题

    遇到问题: PLSQL中汉字显示乱码,并且SQL语句中如果有汉字,则执行时报错.其实并不是SQL语句写的有问题,而是系统不能识别汉字. 解决方法. 首先执行语句 select * from V$NLS ...