1814: Ural 1519 Formula 1

Time Limit: 1 Sec  Memory Limit: 64 MB
Submit: 942  Solved: 356
[Submit][Status][Discuss]

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

HINT

Source

题解:什么叫做哈密顿回路,就是从一个点出发,经过所有点一次仅一次,

所有可以知道路不能交叉。

具体还是看cdqhttps://wenku.baidu.com/view/9cfbb16e011ca300a6c390d5.html

 #pragma GCC optimize(2)
#pragma G++ optimize(2)
#include<iostream>
#include<algorithm>
#include<cmath>
#include<cstdio>
#include<cstring> #define sz 200005
#define ll long long
using namespace std; int hs[sz],tot[],sta[][sz],bit[];
int n,m,c,ex,ey,mp[][];
ll dp[][sz],ans;
char s[]; void add(int s,ll num)
{
int pos=s%sz;
while (hs[pos]!=-)
{
if (sta[c][hs[pos]]==s)
{
dp[c][hs[pos]]+=num;
return;
}
pos++;
if (pos==sz) pos=;
}
dp[c][++tot[c]]=num;
hs[pos]=tot[c];
sta[c][tot[c]]=s;
}
void DP()
{
for (int i=;i<=n;i++)
{
for (int k=;k<=tot[c];k++)
sta[c][k]<<=;
for (int j=;j<=m;j++)
{
c^=; tot[c]=;
memset(hs,-,sizeof(hs));
for (int k=;k<=tot[c^];k++)
{
int s=sta[c^][k];
ll num=dp[c^][k];
int p=(s>>bit[j-])&,q=(s>>bit[j])&;
if (!mp[i][j])
{
if (!p&&!q) add(s,num);
}
else if (!p&&!q)
{
if (!mp[i+][j]||!mp[i][j+]) continue;
s+=(<<bit[j-])+(<<(bit[j]+)) ;
add(s,num);
}
else if (!p&&q){
if (mp[i][j+]) add(s,num);
if (mp[i+][j]){
s+=(<<bit[j-])*q-(<<bit[j])*q;
add(s,num);
}
}
else if (!q&&p){
if (mp[i+][j]) add(s,num);
if (mp[i][j+]){
s+=(<<bit[j])*p-(<<bit[j-])*p;
add(s,num);
}
}
else if (p+q==){
int b=;
for (int t=j+;t<=m;t++){
int v=(s>>bit[t])&;
if (v==) b++;
if (v==) b--;
if (!b){
s-=(<<bit[t]);
break;
}
}
s-=(<<bit[j-])+(<<bit[j]);
add(s,num);
}
else if (p+q==){
int b=;
for (int t=j-;t>=;t--){
int v=(s>>bit[t])&;
if (v==) b++;
if (v==) b--;
if (!b){
s+=(<<bit[t]);
break;
}
}
s-=*(<<bit[j-])+*(<<bit[j]);
add(s,num);
}
else if (p==&&q==){
s-=(<<(bit[j-]+))+(<<bit[j]);
add(s,num);
}
else if (p==&&q==){
if (i==ex&&j==ey) ans+=num;
}
}
}
}
}
int main(){
for (int i=;i<=;i++)
bit[i]=i<<;
scanf("%d%d",&n,&m);
tot[]=; dp[c][]=;
for (int i=;i<=n;i++){
scanf("%s",s+);
for (int j=;j<=m;j++)
if (s[j]=='.') mp[i][j]=,ex=i,ey=j;
}
DP();
printf("%lld\n",ans);
}

那么对于这道题目,分析其特殊性质。

bzoj 1814 Ural 1519 Formula 1 插头DP的更多相关文章

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

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

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

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

  3. bzoj1814 Ural 1519 Formula 1(插头dp模板题)

    1814: Ural 1519 Formula 1 Time Limit: 1 Sec  Memory Limit: 64 MBSubmit: 924  Solved: 351[Submit][Sta ...

  4. 【BZOJ1814】Ural 1519 Formula 1 插头DP

    [BZOJ1814]Ural 1519 Formula 1 题意:一个 m * n 的棋盘,有的格子存在障碍,求经过所有非障碍格子的哈密顿回路个数.(n,m<=12) 题解:插头DP板子题,刷板 ...

  5. Ural 1519 Formula 1 插头DP

    这是一道经典的插头DP单回路模板题. 用最小表示法来记录连通性,由于二进制的速度,考虑使用8进制. 1.当同时存在左.上插头的时候,需要判断两插头所在连通块是否相同,若相同,只能在最后一个非障碍点相连 ...

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

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

  7. BZOJ1814: Ural 1519 Formula 1(插头Dp)

    Description Regardless of the fact, that Vologda could not get rights to hold the Winter Olympic gam ...

  8. 【Ural】1519. Formula 1 插头DP

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

  9. 【BZOJ1814】Ural 1519 Formula 1 (插头dp)

    [BZOJ1814]Ural 1519 Formula 1 (插头dp) 题面 BZOJ Vjudge 题解 戳这里 上面那个链接里面写的非常好啦. 然后说几个点吧. 首先是关于为什么只需要考虑三进制 ...

随机推荐

  1. CCF系列之图像旋转(201503-1)

    试题编号: 201503-1时间限制: 5.0s 内存限制: 256.0MB 问题描述 旋转是图像处理的基本操作,在这个问题中,你需要将一个图像逆时针旋转90度. 计算机中的图像表示可以用一个矩阵来表 ...

  2. CCF系列之日期计算(201509-2)

    试题编号: 201509-2 时间限制: 1.0s 内存限制: 256.0MB 问题描述 给定一个年份y和一个整数d,问这一年的第d天是几月几日? 注意闰年的2月有29天.满足下面条件之一的是闰年: ...

  3. 洛谷 P1231 教辅的组成

    P1231 教辅的组成 题目背景 滚粗了的HansBug在收拾旧语文书,然而他发现了什么奇妙的东西. 题目描述 蒟蒻HansBug在一本语文书里面发现了一本答案,然而他却明明记得这书应该还包含一份练习 ...

  4. Pandas(python)数据处理:只对某一列DataFrame数据进行归一化

    处理数据要用到Pandas,但是没有学过,不知道是否有直接对某一列归一化的方法调用.自己倒弄了下.感觉还是比较麻烦. 使用Pandas读取到数组之后想把其中的'MonthlyIncome'一列进行归一 ...

  5. SpringAOP简单入门

    注解形式 步骤一.定义一个interface public interface ArithmeticCalculator { double plus(int i, int j); double sub ...

  6. [Git] git log命令

    这是git的新系列,不常用的命令和其参数比较容易记不住,干脆将常用的记录下来,日后查查方便也是好的,一篇文章一个git命令,长短根据命令有所不同. git log命令主要用于查看提交历史,同时根据添加 ...

  7. 02_HTML5+CSS详解第一天

    视频来源:麦子学院 讲师:朱朝兵 HTML5概念:HTML即超文本标记语言(HyperText Makeup Language),是一种语法简单,结构清晰的解释型文档,不同于其他编程语言. HTML5 ...

  8. jenkins安装配置[二]

    标签(linux): jenkins 笔者Q:972581034 交流群:605799367.有任何疑问可与笔者或加群交流 安装依赖,如果本机已有java环境可跳过 yum install java- ...

  9. tomcat部署最佳实践(一)

    Tomcat部署最佳实践 标签: linux 笔者Q:972581034 交流群:605799367.有任何疑问可与笔者或加群交流 tomcat是玩web软件必会技能之一,今天我给大家介绍一下tomc ...

  10. auto和bool

    一.auto' 1.只要在函数内部定义变量,默认是auto int num 等价于  auto int num = 10; 2.C语言中的auto关键字就是自动分配自动释放 二.bool类型 1.头文 ...