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. inceptionnet

    inception发展历程 v1 mlp多层感知器层 上面两个观看孔径,尺寸不一样,可以抽取不同分辨率,不同尺度的邻域范围的信息作为特征,这样就可以观察到输入数据的不同层次,不同分辨率的特征 因为这个 ...

  2. 消除textarea的空格de长度值

    在项目中因为用到文本域textarea输入textarea的长度总是显示 25 那是还怀疑textarea自带有value长度? placeholder属性的长度? 那时候想到类似:ul无序列表li元 ...

  3. 运行npm start vue.js项目 出现 npm ERR! missing script: start 错误

    npm ERR! missing script: start 错误 有可能缺少依赖包,运行nmp install安装依赖(一般都依赖很多包,过程有点慢),安装完后发现多一个 node_modules文 ...

  4. Spring配置方式

    Spring配置方式 第一阶段:xml配置     在spring 1.x时代,使用spring开发满眼都是xml配置的bean,随着项目的扩大, 我们需要把xml配置文件分放到不同的配置文件中,那时 ...

  5. 国庆 day 3 下午

    a[问题描述] 你是能看到第一题的 friends 呢. ——hja 给你一个只有小括号和中括号和大括号的括号序列,问该序列是否合法.[输入格式] 一行一个括号序列.[输出格式] 如果合法,输出 OK ...

  6. 手动创建DataTable并添加数据

    DataTable dt=new DataTable(); DataColumn dc=dt.Columns.Add("OBJECTID",Type.GetType("S ...

  7. 介绍静态链接库和动态链接库的差别,及在VC++6.0中的建立和使用

    首先介绍一下链接库:链接库分为动态链接库和静态链接库两种 LIB是静态链接库,在程序编译连接的时候是静态链接,其相应的文件格式是.lib. 即当程序採用静态链接库的时候..lib文件里的函数被链接到终 ...

  8. UVa 11849 - CD

    题目:给你两个有序序列(每一个序列中元素不同),求两序列中都出现的元素个数. 分析:简单题. 合并排序合并过程. 设置两个指针.指向两序列当前元素.那个元素小指针向后移动.相同大则计数加一,同一时候后 ...

  9. Magento--判断checkout中是否使用了coupon code

    在checkout页面中,如果想判断顾客是否有使用coupon code,可以通过checkout session来进行判断.以下代码会返回checkout中使用的coupon code或者返回空(当 ...

  10. Announcing Zuul: Edge Service in the Cloud--转

    原文地址:http://techblog.netflix.com/2013/06/announcing-zuul-edge-service-in-cloud.html   The Netflix st ...