【题目分析】

一直听说这是插头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. ZOJ 3627 Treasure Hunt II (贪心,模拟)

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

  2. codevs 4165 ​高精度求阶乘

    时间限制: 1 s  空间限制: 256000 KB  题目等级 : 白银 Silver 题目描述 Description 用高精度计算出S=n! 其中"!"表示阶乘,例如:5!= ...

  3. 怎样配置JDK开发环境

    (1)我们需要配置三个环境变量,分别是JAVA_HOME,CLASSPATH,Path (2)变量名输入:JAVA_HOME,变量值输入:D:\Java\jdk1.8.0_05点击确定. 需要特别注意 ...

  4. Nodejs + Jshint自动化静态代码检查

    1.   目的 提交代码前能够自动化静态代码检查,提高代码质量 2.   准备 1.    Nodejs安装: 官方地址:http://nodejs.org/ 安装说明:根据电脑配置下载对应的版本进行 ...

  5. Lesson2

    #ifdef __cplusplus #include <cstdlib> #else #include <stdlib.h> #endif #include <SDL/ ...

  6. 2018 北京区域赛 I - Palindromes (找规律)

    题目 HihoCoder - 1878 题目大意 给出k,让求出第k个回文数(k的“长度”不超过1e5) 题解 之前做过类似的题,是统计各阶段的数找到第K个回文数,但这里K太大,需要寻找新的方法. 打 ...

  7. QT +自定义控件-spin+slider

    动手实现自定义控件: 1.首先在ui界面中添加一个(Widget)容器类.如图中的1所示 2.在项目中添加一个SmallWidget类,如下: 3.接着在程序编辑界面进行程序编辑如下: #includ ...

  8. iOS开发各种证书问题

    引言 写在前面 一.App ID(bundle identifier)     二.设备(Device)     三.开发证书(Certificates) 四.供应配置文件(Provisioning ...

  9. POI把html写入word doc文件

    直接把Html文本写入到Word文件 获取查看页面的body内容和引用的css文件路径传入到后台. 把对应css文件的内容读取出来. 利用body内容和css文件的内容组成一个标准格式的Html文本. ...

  10. 单机简单搭建一个kafka集群(没有进行内核参数和JVM的调优)

    1.JDK安装 在我的部署单节点kafka的博客里有相关的方法.(https://www.cnblogs.com/ToBeExpert/p/9789486.html )zookeeper和kafka的 ...