【题目分析】

一直听说这是插头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. 屏幕旋转时 Activity 的生命周期 —— 测试与结论

    关于 Android 手机横竖屏切换时 Activity 的生命周期问题,网上有很多相似的文章,大多数都是说明在竖屏切换横屏时 Activity 会重启一次,而在横屏切换竖屏时 Activity 会重 ...

  2. github入门之分支操作--5

    1.显示分一览表 2.创建.切换分支 2.1.切换到feature-A分支并进行提交 2.1.1.执行下面的命令,创建名为feature-A的分支 实际上,执行以命令也能收到同样的效果,但是我习惯使用 ...

  3. Android程序初体验

    第一个程序的实现的最终功能是: 点击"正确"或者"错误"会得到一个是否正确的提示. 直接上效果图.     此次涉及代码编写的文件有4个: package co ...

  4. Spark中Java函数的使用方法笔记

    1: map 函数map是对RDD中的每个元素都执行一个指定的函数来产生一个新的RDD. 任何原RDD中的元素在新RDD中都有且只有一个元素与之对应. 2: mapPartitions函数</p ...

  5. restful十项规范

    1.协议 API与用户的通信都是通过HTTPS协议进行的 2.域名 应尽量将API部署在专有域名下:https://api.example.com 如果确定API很简单,不会有什么扩展,则可以放在主域 ...

  6. ios UnitTest 学习笔记1

    一.运行第一个单元测试: 1.在Xcode 5中新建一个工程默认自带一个单元测试的文件夹,IDE自动生成了一个实现XCTestCase的.m文件,里面有一个失败测试(早期版本中实现的是SenTestC ...

  7. decompressedResponseImageOfSize:completionHandler:]_block_invoke

    原因:   It turns out the linker error was caused by the CGImageSourceCreateWithData call. And the root ...

  8. JavaScript中valueOf函数与toString方法

    基本上,所有JS数据类型都拥有valueOf和toString这两个方法,null除外.它们俩解决javascript值运算与显示的问题,本文将详细介绍,有需要的朋友可以参考下   JavaScrip ...

  9. Mathematics-基础:散列函数

    一,概念: 散列(HASH)函数H也称哈希函数.是典型的多到一的函数,其输入为一可变长x(可以足够的长),输出一固定长的串h(一般为128位.160位,比输入的串短),该串h被称为输入x的Hash值. ...

  10. -bash: xx: command not found 在有yum源情况下处理

    -bash: xx: command not found 在有yum源情况下处理 yum provides "*/xx"  ###"xx"代表某命令 或者 yu ...