Attack on Alpha-Zet

题目描述

Space pirate Captain Krys has recently acquired a map of the artificial and highly secure planet Alpha-Zet which he has been planning to raid for ages. It turns out the whole planet is built on a 2D plane with modules that serve as one room each. There is exactly one module at every pair of integer coordinates and modules are exactly 1 × 1 units big. Every module is bidirectionally connected to at least one adjacent module. Also, for any two modules there exists exactly one path between them. All in all the modules create a rectangular maze without any loops.

Figure A.1: Illustration of Sample Input 2

On the map Captain Krys has marked several modules he wants to visit in exactly the marked order. What he intends to do there is none of your business, but he promises you a fortune if you determine the number of modules he has to walk through along

the route (since there are no loops he will always take the direct route from one marked module to the next). The first marked module indicates where he starts his journey, the last where he wants to finish.

输入

The input consists of:

•one line with two integers h and w (2 ≤ h, w ≤ 1 000) describing the height and the width of the maze.

•h + 1 lines follow, describing the maze in ASCII, each line containing 2 · w + 1 characters.

The description always follows these rules:

–In every row, columns with odd index (starting at index 1) contain either vertical walls or spaces and columns with even index contain either horizontal walls or spaces.

–The first row describes the northern wall of the maze (which always consists only of horizontal walls). Every subsequent row describes a row of modules.

–A module is located at every even column index. Its western and eastern walls are located at the directly neighboring odd column indices respectively, its northern wall is located at the same column index but one row above and its southern wall can be found at its own position. If a wall is missing, the corresponding position contains a space instead.

•After the description of the maze, an integer m (2 ≤ m ≤ 104) is given.

•Each of the following m lines describes a marked module with two integer coordinates x and y (1 ≤ x ≤ h; 1 ≤ y ≤ w). The first pair of coordinates is the start point of the journey, the last pair the end point. Modules may appear multiple times but never twice or more in a row. (1, 1) is the top left module and (h, w) is the bottom right module.

It is guaranteed that the maze itself is enclosed. Furthermore it is guaranteed that exactly one path exists between any two modules.

输出

Output one integer, the number of modules Captain Krys has to travel through if he follows the route in the exact order given in the input.

样例输入

2 6
_ _ _ _ _ _
| _ _ _ _ _|
|_ _ _ _ _ _|
5
1 5
1 1
1 6
1 1
1 5

样例输出

18

树上距离,lca裸题

有空格的输入

#include <bits/stdc++.h>
#define ll long long
using namespace std;
const int maxn=1005;
const int maxm=2005;
char mp[maxn][maxm];
int n,m; struct Edge
{
int v,next;
}e[maxn*maxm*2];
int head[maxn*maxn],tol;
int dep[maxn*maxn];
int f[maxn*maxn][22];
void add(int u,int v){tol++;e[tol].v=v;e[tol].next=head[u];head[u]=tol;}
void input()
{
getchar();
for(int i=0;i<=n;i++){
gets(mp[i]);
}
}
int num(int i , int j)
{
i = i-1;
j = (j+1)/2;
return i*m+j;
} void build()
{
for(int i=1; i<=n; i++)
{
for(int j=1; j<=2*m; j+=2)
{
if(mp[i][j+1] != '|')
{
add(num(i,j) , num(i,j+2));
add(num(i,j+2) , num(i,j));
}
if(mp[i][j] != '_')
{
add(num(i,j) , num(i+1,j));
add(num(i+1,j) , num(i,j));
}
}
}
} void dfs(ll u,ll fa){//dfs建树
dep[u]=dep[fa]+1;
f[u][0]=fa;//初始化每个点的父节点
for(int i=head[u];i;i=e[i].next){
int v=e[i].v;
if(v!=fa){
dfs(v,u);
}
}
}
void rmq_init(int k)
{
for(int j=1;j<=19;j++)
for(int i=1;i<=k;i++)
if(f[i][j-1]) f[i][j] = f[f[i][j-1]][j-1];
}
int lca(int u,int v)
{
if(dep[u]<dep[v]) swap(u,v);//深度深的先处理
for(int i=19;i>=0;i--){
if(dep[u]>=dep[v]+(1<<i)){
u = f[u][i];
}
}
if(u==v){//跳到同一深度判断是否完成
return u;
}
for(int i=19;i>=0;i--){//一起跳
if(f[u][i]!=f[v][i]){
u=f[u][i];
v=f[v][i];
}
}
return f[u][0];
}
void solve()
{
ll res=0;int l,r;
int now,last,tmp;
int q;scanf("%d",&q);
for(int i=1;i<=q;i++){
scanf("%d%d",&l,&r);
now=num(l,r*2-1);
if(i!=1){
tmp=lca(last,now);
res+=(dep[now]-dep[tmp]+dep[last]-dep[tmp]);
}
last=now;
}
printf("%lld",res);
}
int main()
{
scanf("%d%d",&n,&m);
input();
build();
dfs(1,0);
rmq_init(n*m);
solve();
return 0;
}

【lca+输入】Attack on Alpha-Zet的更多相关文章

  1. LCA入门题集小结

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2586 题目: How far away ? Time Limit: 2000/1000 MS (Jav ...

  2. LaLeX数学公式

    启用数学公式: 需要插入公式时,用 $ 将公式包围.若需要输入多行,则用一对 $$ 包围. 例如: $$ \rho = \sqrt{(\Delta x)^{2}+(\Delta y)^{2}} \\ ...

  3. Python+OpenCV图像处理(十二)—— 图像梯度

    简介:图像梯度可以把图像看成二维离散函数,图像梯度其实就是这个二维离散函数的求导. Sobel算子是普通一阶差分,是基于寻找梯度强度.拉普拉斯算子(二阶差分)是基于过零点检测.通过计算梯度,设置阀值, ...

  4. UE4 材质Lerp节点解疑

    转自:http://www.manew.com/thread-46268-1-1.html 1.A是一个灰色,B是一个红色,Alpha是一个颜色图 A到B是0到1,也就是黑到白,所以,alpha图,黑 ...

  5. RETE算法介绍

    RETE算法介绍一. rete概述Rete算法是一种前向规则快速匹配算法,其匹配速度与规则数目无关.Rete是拉丁文,对应英文是net,也就是网络.Rete算法通过形成一个rete网络进行模式匹配,利 ...

  6. OpenCV——边缘检测(sobel算子、Laplacian算子、scharr滤波器)

    #include <opencv2/opencv.hpp> #include <iostream> using namespace cv; using namespace st ...

  7. Canvas 3D球形文字云动画特效

    Canvas 3D球形文字云动画特效 效果图: 代码如下,复制即可使用: (适用浏览器:360.FireFox.Chrome.Opera.傲游.搜狗.世界之窗. 不支持Safari.IE8及以下浏览器 ...

  8. 【python-opencv】18-图像梯度+图像边界

    效果图: *一阶导数与Soble算子 *二阶导数与拉普拉斯算子 定义:把图片想象成连续函数,因为边缘部分的像素值是与旁边像素明显有区别的,所以对图片局部求极值,就可以得到整幅图片的边缘信息了. 不过图 ...

  9. Android图形动画

    一.动画基础 本质 每帧绘制不同的内容. 基本过程 开始动画后,调用View的invalidate触发重绘.重绘后检查动画是否停止,若未停止则继续调用invalidate触发下一帧(下一次重绘),直到 ...

随机推荐

  1. 【每日Scrum】第九天冲刺

    一.计划会议内容 尝试数据库的连接与ui应用 二.任务看板 任务看板 已完成:登录与个人界面布局实现 进行中:连接数据库,地图主界面 待进行:功能整合 三.scrum讨论照片 四.产品的状态 无 五. ...

  2. VBE2019的下载、安装和使用(最新版2020.2.22)

    VBE2019可用于XP系统.Windows 7和Windows 10的32位.64位Office对应的VBA环境 安装包下载地址:VBE2019-Setup.zip 下载后解压缩,直接双击安装(请勿 ...

  3. maven常用配置setting.xml详解

    参考文章: https://www.cnblogs.com/hwaggLee/p/4579418.html 1.<localRepository/> 该值maven本地仓库的路径 < ...

  4. HDU_2604 矩阵快速幂 较难推的公式

    一个排队问题,f代表女,m代表男,f和m出现的几率相等.问一个长为L的队伍不能出现 fmf 和 fff这样的串总共有多少种. 这个题目的公式递推略难啊...我看了别人博客才想明白原来是这么递推出来的. ...

  5. python交互图

    花了时间, 记录一下 # -*- coding:utf-8 -*- import matplotlib.pyplot as plt from matplotlib.patches import Rec ...

  6. MySQL性能管理及架构设计:第2章 什么影响了MySQL性能

    第2章 什么影响了MySQL性能 2-1 影响性能的几个方面 1.服务器的硬件 2.服务器的操作系统 3.数据库的存储引擎 4.数据库的参数配置 5.数据库表结构设计和SQL语句的编写和优化 2-2 ...

  7. JS获取当前时间往后一天的时间

    (图片来自W3school) let myDate = new Date(); myDate.setDate(myDate.getDate() + 1); let year = myDate.getF ...

  8. java RSA 加密解密

    package com.rsa; import java.security.KeyFactory; import java.security.KeyPair; import java.security ...

  9. 2020 年最流行的 Java 开发技术

    不知不觉间,2020 年即将于十几天之后到来,作为技术圈中你,准备好迎接最新的变化了吗?在本文中,我们将以编程界最常用的编程语言 Java 为例,分享最为主流的技术与工具. 作者 | divyesh. ...

  10. Maven--mirror 和 repository

    参考:http://blog.csdn.net/isea533/article/details/22437511   http://www.cnblogs.com/xdouby/p/6502925.h ...