Formula 1

题意

在\(n*m\)的矩阵中,有些格子有树,没有树的格子不能到达,找一条回路,吃完所有的树,求有多少种方法。

解法

因为只要一条回路,所以我们必须维护插头的连通性。

具体的可以参照 这位大佬的博客

代码

注意开long long。

#include <cstdio>
#include <cstdlib>
#include <iostream>
#include <cstring>
#include <cctype>
#define del(a,b) memset(a,b,sizeof(a))
using namespace std;
typedef long long ll;
template <typename T>
inline void read(T &x) {
x=0;char c=getchar();T k=1;
while(!isdigit(c)) {if(c=='-') k=-1;c=getchar();}
while(isdigit(c)) {x=x*10+c-'0';c=getchar();}x*=k;
} const int maxn=15;
const int maxhash=100000;
char G[maxn][maxn];
int _hash[maxhash];
ll sta[2][600000],sum[2][600000];
int cur,n,m,en,em;
int tot[2];
int jz[maxn]; void _init() {
read(n),read(m);
for(int i=1;i<=n;i++)
for(int j=1;j<=m;j++) {
cin>>G[i][j];
if(G[i][j]=='.') en=i,em=j;
}
for(int i=1;i<=m;i++) jz[i]=i<<1;
} void hash_insert(ll s,ll data) {
int pos=s%maxhash;
while(_hash[pos]) {
if(sta[cur][_hash[pos]]==s) {
sum[cur][_hash[pos]]+=data;
return;
}
if(++pos==maxhash) pos=0;
}
++tot[cur];
_hash[pos]=tot[cur];
sta[cur][tot[cur]]=s;sum[cur][tot[cur]]=data;
}
ll ans;
void work() {
tot[0]=1;sum[0][1]=1;
for(int i=1;i<=n;i++) {
for(int k=1;k<=tot[cur];k++)
sta[cur][k]=sta[cur][k]<<2;
for(int j=1;j<=m;j++) {
cur^=1;
tot[cur]=0;
del(_hash,0);
del(sta[cur],0);
del(sum[cur],0);
for(int k=1;k<=tot[1-cur];k++) {
ll s=sta[1-cur][k],data=sum[1-cur][k];
int x=(s>>jz[j-1])%4;
int y=(s>>jz[j])%4;
ll temp;
if(G[i][j]!='.') {
if(x==0&&y==0) hash_insert(s,data);
}
else {
if(x==0&&y==0) {
if(G[i][j+1]=='.'&&G[i+1][j]=='.') {
temp=s+1*(1<<jz[j-1])+2*(1<<jz[j]);
hash_insert(temp,data);
}
continue;
}
if(x==0&&y>0) {
if(G[i][j+1]=='.')
hash_insert(s,data);
if(G[i+1][j]=='.') {
temp=s-y*(1<<jz[j])+y*(1<<jz[j-1]);
hash_insert(temp,data);
}
continue;
}
if(x>0&&y==0) {
if(G[i+1][j]=='.')
hash_insert(s,data);
if(G[i][j+1]=='.') {
temp=s-x*(1<<jz[j-1])+x*(1<<jz[j]);
hash_insert(temp,data);
}
continue;
}
if(x==1&&y==1) {
int f=1;
for(int v=j+1;v<=m;v++) {
int fff=(s>>jz[v])%4;
if(fff==1) f++;
if(fff==2) f--;
if(!f) {
temp=s-2*(1<<jz[v])+1*(1<<jz[v]);
break;
}
}
temp=temp-1*(1<<jz[j-1])-1*(1<<jz[j]);
hash_insert(temp,data);
continue;
}
if(x==2&&y==2) {
int f=1;
for(int v=j-2;v>=1;v--) {
int fff=(s>>jz[v])%4;
if(fff==1) f--;
if(fff==2) f++;
if(!f) {
temp=s-1*(1<<jz[v])+2*(1<<jz[v]);
break;
}
}
temp=temp-2*(1<<jz[j-1])-2*(1<<jz[j]);
hash_insert(temp,data);
continue;
}
if(x==2&&y==1) {
temp=s-2*(1<<jz[j-1])-1*(1<<jz[j]);
hash_insert(temp,data);
continue;
}
if(x==1&&y==2) {
if(i==en&&j==em) {
ans+=data;
}
}
}
}
}
}
} int main() {
_init();
work();
printf("%lld\n",ans);
return 0;
}

bzoj 1814 Fornula 1的更多相关文章

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

  2. bzoj 1814 Ural 1519 Formula 1 ——插头DP

    题目:https://www.lydsy.com/JudgeOnline/problem.php?id=1814 普通的插头 DP .但是调了很久.注意如果合并两个 1 的话,不是 “把向右第一个 2 ...

  3. bzoj 1814: Ural 1519 Formula 1 插头dp经典题

    用的括号序列,听说比较快. 然并不会预处理,只会每回暴力找匹配的括号. #include<iostream> #include<cstdio> #include<cstr ...

  4. bzoj 1814: Ural 1519 Formula 1【插头dp】

    设f[i][j][s]为轮廓线推到格子(i,j),状态为s的方案数 括号表示一段线的左端和右端,表示成左括号和右括号,状压的时候用1和2表示,0表示已经闭合 下面的蓝线是黄色格子的轮廓线,dp转移要把 ...

  5. 插头DP题目泛做(为了对应WYD的课件)

    题目1:BZOJ 1814 URAL 1519 Formula 1 题目大意:给定一个N*M的棋盘,上面有障碍格子.求一个经过所有非障碍格子形成的回路的数量. 插头DP入门题.记录连通分量. #inc ...

  6. BZOJ 2127: happiness [最小割]

    2127: happiness Time Limit: 51 Sec  Memory Limit: 259 MBSubmit: 1815  Solved: 878[Submit][Status][Di ...

  7. BZOJ 3275: Number

    3275: Number Time Limit: 10 Sec  Memory Limit: 128 MBSubmit: 874  Solved: 371[Submit][Status][Discus ...

  8. BZOJ 2879: [Noi2012]美食节

    2879: [Noi2012]美食节 Time Limit: 10 Sec  Memory Limit: 512 MBSubmit: 1834  Solved: 969[Submit][Status] ...

  9. bzoj 4610 Ceiling Functi

    bzoj 4610 Ceiling Functi Description bzoj上的描述有问题 给出\(n\)个长度为\(k\)的数列,将每个数列构成一个二叉搜索树,问有多少颗形态不同的树. Inp ...

随机推荐

  1. select的option触发事件

    <!DOCTYPE html> <html lang="zh"> <head> <meta charset="UTF-8&quo ...

  2. JAVA基础总结【面试】

    前言 近间陆续面试了不少的求职的前(JAVA).后(WEB)端开发人员,包括实习生.应届毕业生.一两年工作经验的.也有三四年工作经验的,也算见过了比较多的开发人员,想在这里做个总结,本次主要讲一讲面试 ...

  3. HDU5130 Signal Interference

    /* HDU5130 Signal Interference http://acm.hdu.edu.cn/showproblem.php?pid=5130 计算几何 圆与多边形面积交 * */ #in ...

  4. SQLPlus在连接时通常有四种方式

    SQLPlus在连接时通常有四种方式 1. ? 1 sqlplus / as sysdba 操作系统认证,不需要数据库服务器启动listener,也不需要数据库服务器处于可用状态.比如我们想要启动数据 ...

  5. BA-siemens-insight-event builder使用

    event builder功能主要是用来给report使用的,作为一个独立的对象,这个对象的功能就是收集点位的信息,如果再使用report功能就可以显示或输出点位的信息.

  6. 解决 Mac OS X 10.11 安装 sip 没有权限的问题

    在搭建 PYQT 的过程中我遇上了一个非常恶心的问题,在安装 sip 的时候编译源代码之后的安装过程中一直提示我:Operation not permitted ,我甚至重装了系统也无济于事,终于通过 ...

  7. 上机题目(0基础)- Java网络操作-Socket实现client和server端通信(Java)

    非常多刚開始学习的人对于java网络通信不太熟悉.对相关概念也不太明确,这里我们主要实现一下socket通信,socket通信在java中应用十分广泛.比如QQ和MSN等都是基于socket通信的,什 ...

  8. esql开发总结

    1 定义或者声明方法 int method(char *arg1,char* arg2...);   实现方法 int method(char *arg1,char* arg2...)     EXE ...

  9. 关于部门后端所有转向java前初步设想

    Java服务有些什么形式?眼下来看主要是下面几类: 1.  执行在Web应用server的Servlet 2.  Thrift.PB.Avro等相似框架写的java服务 3.  WebService( ...

  10. SGU 531 - Bonnie and Clyde 预处理+二分

    Bonnie and Clyde Description Bonnie and Clyde are into robbing banks. This time their target is a to ...