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. HX711初步处理记录

    参考文档为极客工坊大神记录 http://www.geek-workshop.com/forum.php?mod=viewthread&tid=2315&highlight=hx711 ...

  2. jinja2.exceptions.TemplateNotFound: home/index.html

    问题: 检查路由路径和模版渲染方式,其他文件路径都正确,可以返回字符串,就是无法返回定义的模版,为什么flask无法启找到这个模版? 那,问题原因在哪? 在flask中,目录有着严格的定义,模版目录必 ...

  3. .net Core学习笔记2 实现列表的条件筛选,排序,分页

    打开vs,完善上次"简单粗暴"的项目 发现上次的实体类的导航属性有点问题,这是更改后的 namespace ProductMvc.Models { public class Pro ...

  4. iterator_category

    /* * 迭代器类型 * 1. input ierator * 2. write iterator * 3. forward iterator 在迭代器所形成的区间上进行读写操作 * 4. bidir ...

  5. DispatcherServlet介绍

    <property name="features"> <list> <value>WriteMapNullValue</value> ...

  6. zabbix3.2_yum官方文档centos 7版

    Installation from packages 1 Repository installation 2 Server installation with MySQL database 3 Ser ...

  7. 【转】awk用法介绍

    1. 介绍 Awk是由Aho.Weinberger和Kernighan三位科学家开发的,特别擅长文本处理的linux 工具,该工具是 linux 下最常用的工具之一.Awk不是普通的工具,其实,也是一 ...

  8. Akamai CDN

    Akamai CDN中的几个重要组件 mapping system 调度系统(映射client到edge cluster,进而到edge server) edge server platform 边缘 ...

  9. 通过重写 class 的 ToString() 来简化获取 enum 的 DescriptionAttribute 值

    通过重写 class 的 ToString() 来简化获取 enum 的 DescriptionAttribute 值 目录 一.常见的 enum 类型 二.演变:class 版本的 enum 类型 ...

  10. a标签的伪元素的应用——link,hover,visited,active

    a标签应用中,一般有四个状态: 鼠标未移入前(link),鼠标移入时(hover), 鼠标点击时(active),鼠标点击后(visited). <!DOCTYPE html> <h ...