bzoj 1814 Ural 1519 Formula 1 插头DP
1814: Ural 1519 Formula 1
Time Limit: 1 Sec Memory Limit: 64 MB
Submit: 942 Solved: 356
[Submit][Status][Discuss]
Description
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
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
Sample Input
**..
....
....
....
Sample Output
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的更多相关文章
- bzoj 1814 Ural 1519 Formula 1 ——插头DP
题目:https://www.lydsy.com/JudgeOnline/problem.php?id=1814 普通的插头 DP .但是调了很久.注意如果合并两个 1 的话,不是 “把向右第一个 2 ...
- bzoj 1814: Ural 1519 Formula 1 插头dp经典题
用的括号序列,听说比较快. 然并不会预处理,只会每回暴力找匹配的括号. #include<iostream> #include<cstdio> #include<cstr ...
- bzoj1814 Ural 1519 Formula 1(插头dp模板题)
1814: Ural 1519 Formula 1 Time Limit: 1 Sec Memory Limit: 64 MBSubmit: 924 Solved: 351[Submit][Sta ...
- 【BZOJ1814】Ural 1519 Formula 1 插头DP
[BZOJ1814]Ural 1519 Formula 1 题意:一个 m * n 的棋盘,有的格子存在障碍,求经过所有非障碍格子的哈密顿回路个数.(n,m<=12) 题解:插头DP板子题,刷板 ...
- Ural 1519 Formula 1 插头DP
这是一道经典的插头DP单回路模板题. 用最小表示法来记录连通性,由于二进制的速度,考虑使用8进制. 1.当同时存在左.上插头的时候,需要判断两插头所在连通块是否相同,若相同,只能在最后一个非障碍点相连 ...
- bzoj 1814: Ural 1519 Formula 1【插头dp】
设f[i][j][s]为轮廓线推到格子(i,j),状态为s的方案数 括号表示一段线的左端和右端,表示成左括号和右括号,状压的时候用1和2表示,0表示已经闭合 下面的蓝线是黄色格子的轮廓线,dp转移要把 ...
- BZOJ1814: Ural 1519 Formula 1(插头Dp)
Description Regardless of the fact, that Vologda could not get rights to hold the Winter Olympic gam ...
- 【Ural】1519. Formula 1 插头DP
[题目]1519. Formula 1 [题意]给定n*m个方格图,有一些障碍格,求非障碍格的哈密顿回路数量.n,m<=12. [算法]插头DP [题解]<基于连通性状态压缩的动态规划问题 ...
- 【BZOJ1814】Ural 1519 Formula 1 (插头dp)
[BZOJ1814]Ural 1519 Formula 1 (插头dp) 题面 BZOJ Vjudge 题解 戳这里 上面那个链接里面写的非常好啦. 然后说几个点吧. 首先是关于为什么只需要考虑三进制 ...
随机推荐
- winform webbrowser如何强制使用ie11内核?
webkit.net ,cefsharp,openwebkit.net等这些基于谷歌或者基于firfox内核的浏览器有个共同点,就是必须指定winform为x86的才能使用, 而且使用过程中也是各种坑 ...
- 史上最全的判断android,ios还是ipad访问,附上多种语言的实现方式
js判断: (function(a,b){if(/(android|bb\d+|meego).+mobile|avantgo|bada\/|blackberry|blazer|compal|elain ...
- shopnc IM配置过程
im配置windows下,修改chat下和data下的config,安装node,覆盖node下文件即可
- JavaScript的三种类型检测typeof , instanceof , toString比较
1.typeof typeof是js的一个操作符,在类型检测中,几乎没有任何用处. typeof 返回一个表达式的数据类型的字符串,返回结果为javascript中的基本数据类型,包括:number. ...
- Python实现一个简单的微信跳一跳辅助
1. 前言 微信的跳一跳相信大家都很熟悉了,而且现在各种外挂.辅助也是满天飞,反正本人的好友排行榜中已经是八九百都不足为奇了.某宝上一搜一堆结果,最低的居然只要3块多,想刷多少分就刷多少分,真是离谱 ...
- mysql-关联查询
MySQL关联查询的三种写法: SELECT * FROM film JOIN film_actor ON (film.film_id = film_actor.film_id);SELECT * F ...
- JQuery判断是否为空
//有如下三种判断 var A=$("#**).val(); if(A==null||A!=undefined||A==""){ //处理 } //参考文章1说下面方法效 ...
- 一步步使用BMC Atrium Orchestrator Vmware Infrastructure Event Monitor
本教程将一步步演示怎么使用BMC Atrium Orchestrator (BAO) Vmware Infrastructure Event Monitor来监控VSphere Webservice的 ...
- Servlet--ServletInputStream类,ServletOutputStream类
ServletInputStream类 定义 public abstract class ServletInputStream extends InputStream 这个类定义了一个用来读取客户端的 ...
- javascript 中的console.log有什么作用啊?
相比alert他的优点是: 他能看到结构话的东西,如果是alert,淡出一个对象就是[object object],但是console能看到对象的内容. console不会打断你页面的操作,如果用al ...