【题目分析】

一直听说这是插头DP入门题目。

难到爆炸。

写了2h,各种大常数,ural垫底。

【代码】

#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>
using namespace std; #define maxn 2000005
#define u64 unsigned long long
#define F(i,j,k) for (int i=j;i<=k;++i)
#define D(i,j,k) for (int i=j;i>=k;--i)
int tot,fac[15],can[50005],top=0,hash[maxn],a[12][12],n,m,ex,ey,b[15];
u64 dp[2][50005];
char s[15]; //void print(int x){F(j,0,m) printf("%d",(x%fac[j+1])/fac[j]);} void init()
{
scanf("%d%d",&n,&m);
F(i,0,n-1){scanf("%s",s);F(j,0,m-1)a[i][j]=s[j]=='*'?1:0;}
F(i,0,n-1)F(j,0,m-1) if (!a[i][j]) ex=i,ey=j;
// printf("The end is %d %d\n",ex,ey);
fac[0]=1;F(i,1,14)fac[i]=fac[i-1]*3;tot=fac[m+1]-1;
// printf("tot is %d\n",tot);
F(i,0,tot)
{
int bac=0,flag=1;
F(j,0,m)
{
if ((i%fac[j+1])/fac[j]==1) bac++;
else if ((i%fac[j+1])/fac[j]==2) bac--;
if (bac<0) {flag=0;break;}
}
if (flag&&bac==0) can[++top]=i,hash[i]=top;
}
// printf("All can is %d\n",top);
// F(i,1,top) {printf("%d is can ",can[i]);print(can[i]);printf("\n");}
} void recode(int x)
{F(i,0,m)b[i]=(x%fac[i+1])/fac[i];} int encode()
{
int ret=0;
F(i,0,m) ret+=b[i]*fac[i];
return ret;
} int main()
{
init();
int now=1,pre=0;
memset(dp[now],0,sizeof dp[now]);
dp[now][1]=1;
F(i,0,n-1)
F(j,0,m-1)
{
now^=1;pre^=1;
memset(dp[now],0,sizeof dp[now]);
if (a[i][j])
{
F(s,1,top) if (dp[pre][s])
{
// print(can[s]);
int tmp1=(can[s]%fac[j+1])/fac[j],tmp2=(can[s]%fac[j+2])/fac[j+1];
if (!tmp1&&!tmp2)
{
dp[now][s]+=dp[pre][s];
// printf(" to %d ",s); print(can[s]); printf("\n");
}
}
}
else
{
F(s,1,top) if (dp[pre][s])
{
// print(can[s]); printf("\n");
int tmp1=(can[s]%fac[j+1])/fac[j],tmp2=(can[s]%fac[j+2])/fac[j+1],tmp=can[s];
if (!tmp1&&!tmp2)
{
tmp+=1*fac[j];
tmp+=2*fac[j+1];
dp[now][hash[tmp]]+=dp[pre][s];
// printf(" to %d ",hash[tmp]); print(tmp); printf("\n");
}
else if (!tmp1)
{
dp[now][hash[tmp]]+=dp[pre][s];
// printf(" to %d ",hash[tmp]); print(tmp); printf("\n");
tmp-=tmp1*fac[j]; tmp-=tmp2*fac[j+1];
tmp+=tmp2*fac[j];
dp[now][hash[tmp]]+=dp[pre][s];
// printf(" to %d ",hash[tmp]); print(tmp); printf("\n");
}
else if (!tmp2)
{
dp[now][hash[tmp]]+=dp[pre][s];
// printf(" to %d ",hash[tmp]); print(tmp); printf("\n");
tmp-=tmp1*fac[j]; tmp-=tmp2*fac[j+1];
tmp+=tmp1*fac[j+1];
dp[now][hash[tmp]]+=dp[pre][s];
// printf(" to %d ",hash[tmp]); print(tmp); printf("\n");
}
else if (tmp1==2&&tmp2==2)
{
recode(tmp);
int pos=0,bac=0;
D(z,j-1,0)
{
if (b[z]==2) bac++;
if (b[z]==1) bac--;
if (b[z]==1&&bac==-1) {pos=z;break;}
}
b[j]=b[j+1]=0;
b[pos]=2;
tmp=encode();
dp[now][hash[tmp]]+=dp[pre][s];
// printf(" to %d ",hash[tmp]); print(tmp); printf("\n");
}
else if (tmp1==1&&tmp2==1)
{
recode(tmp);
int pos=0,bac=0;
F(z,j+2,n)
{
if (b[z]==1) bac++;
if (b[z]==2) bac--;
if (b[z]==2&&bac==-1) {pos=z;break;}
}
b[j]=b[j+1]=0;
b[pos]=1;
tmp=encode();
dp[now][hash[tmp]]+=dp[pre][s];
// printf(" to %d ",hash[tmp]); print(tmp); printf("\n");
}
else if (tmp1==2&&tmp2==1)
{
tmp-=tmp1*fac[j];
tmp-=tmp2*fac[j+1];
dp[now][hash[tmp]]+=dp[pre][s];
// printf(" to %d ",hash[tmp]); print(tmp); printf("\n");
}
else if (tmp1==1&&tmp2==2&&i==ex&&j==ey)
{
tmp-=tmp1*fac[j];
tmp-=tmp2*fac[j+1];
dp[now][hash[tmp]]+=dp[pre][s];
// printf(" to %d ",hash[tmp]); print(tmp); printf("\n");
}
}
}
if (j==m-1)
{
now^=1; pre^=1;
memset(dp[now],0,sizeof dp[now]);
F(s,1,top) if (dp[pre][s])
{
// print(can[s]);
recode(can[s]);
if (b[m]!=0)
{
// printf(" can't\n");
continue;
}
D(i,m,1) b[i]=b[i-1];
b[1]=0;
int tmp=encode();
if (!hash[tmp]) {continue;}
dp[now][hash[tmp]]+=dp[pre][s];
// printf(" rev to %d ",hash[tmp]); print(tmp); printf("\n");
}
}
// printf("\n");
}
printf("%llu\n",dp[now][1]);
}

  

URAL 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. 【Ural】1519. Formula 1 插头DP

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

  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. URAL1519 Formula 1 —— 插头DP

    题目链接:https://vjudge.net/problem/URAL-1519 1519. Formula 1 Time limit: 1.0 secondMemory limit: 64 MB ...

  7. URAL 1519 基础插头DP

    题目大意: 给定一个图,一部分点'*'作为障碍物,求经过所有非障碍点的汉密尔顿回路有多少条 基础的插头DP题目,对于陈丹琦的论文来说我觉得http://blog.sina.com.cn/s/blog_ ...

  8. [URAL1519] Formula 1 [插头dp入门]

    题面: 传送门 思路: 插头dp基础教程 先理解一下题意:实际上就是要你求这个棋盘中的哈密顿回路个数,障碍不能走 看到这个数据范围,还有回路处理,就想到使用插头dp来做了 观察一下发现,这道题因为都是 ...

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

随机推荐

  1. uvm.sv——UVM之道

    文件: $UVM_HOME/src/uvm.sv 类: 无   `include "uvm_pkg.sv"   Thus spake the UVM master programm ...

  2. CentOS 6.5 下,phpmyadmin 建库无权限

    阿里云主机.用root进入phpMyadmin,居然没有权限建库. 上网查到命令: grant all privileges on *.* to root@'%' identified by &quo ...

  3. ZOJ 3627 Treasure Hunt II (贪心,模拟)

    题意:有n个城市并排着,每个城市有些珠宝,有两个人站在第s个城市准备收集珠宝,两人可以各自行动,但两人之间的距离不能超过dis,而且每经过一个城市就需要消耗1天,他们仅有t天时间收集珠宝,问最多能收集 ...

  4. The Django Book 第三章 试图和URL配置

    之前自学Django也有一段时间了,再过一个月就要入职新公司了(Python Django开发),即使现在还在入门级徘徊,再好好把Django基础过一遍吧. The Django Book 第三章 试 ...

  5. RLock(递归锁)

    import threading, time def run1(): print("grab the first part data") lock.acquire()#进入大门后的 ...

  6. Java的jdbc调用SQL Server存储过程Bug201906131120

    如果要查询结果,第一行使用set nocount on;可能可以解决问题.

  7. Codeforces Round #275(Div. 2)-C. Diverse Permutation

    http://codeforces.com/contest/483/problem/C C. Diverse Permutation time limit per test 1 second memo ...

  8. Java8新特性Lambda表达式

    List<RoleDO> allRoles = roleService.list(); //获取角色中备注不是app的集合List<RoleDO> webRoles = all ...

  9. fckeditor配置详解

    使用配置设置: . FCKConfig.CustomConfigurationsPath = '' ; // 自定义配置文件路径和名称 . FCKConfigFCKConfig.EditorAreaC ...

  10. 分布式mysql 和 zk ( zookeeper )的分布式的区别 含冷热数据讨论

    zk ( zookeeper )的分布式仅仅指的是备份模式. 分布式 mysql 不仅仅要关注备份(从以往的半主,主主,到 paxos). (mysql 比 hbase 的region成熟, hdfs ...