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. POJ-3629 模拟

    A - Card Stacking Time Limit:1000MS     Memory Limit:65536KB     64bit IO Format:%I64d & %I64u S ...

  2. docker创建redis容器

    1.拉取最新的redis镜像 docker pull redis; 2.创建存放redis数据的目录 mkdir /redis/data 3.查询redis镜像id docker images; RE ...

  3. MyBatis:一对多、多对一处理

    多对一的处理 多对一的理解: 多个学生对应一个老师 如果对于学生这边,就是一个多对一的现象,即从学生这边关联一个老师! 数据库设计 CREATE TABLE `teacher` ( `id` INT( ...

  4. 【SpringBoot】SpringBoot Web开发(八)

    本周介绍SpringBoot项目Web开发的项目内容,及常用的CRUD操作,阅读本章前请阅读[SpringBoot]SpringBoot与Thymeleaf模版(六)的相关内容 Web开发 项目搭建 ...

  5. c 循环左移

    char b[11] = {'1', '2', '3', '4', '5', '6', '7', '8', '9', '0'}; const int iShift = 4; for (int j = ...

  6. pytorch安装及基本用法

    20180425更新  安装pytorch0.4.0: conda uninstall pytorch # 如果是CUDA版本的话 conda uninstall cuda80 cuda90 # 如果 ...

  7. maven工具引入lib下的jar文件

    <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot ...

  8. MySQL中间件介绍

    360 Atlas Atlas是由 Qihoo 360, Web平台部基础架构团队开发维护的一个基于MySQL协议的数据中间层项目.它是在mysql-proxy 0.8.2版本的基础上,对其进行了优化 ...

  9. 吴裕雄--天生自然 JAVASCRIPT开发学习:函数调用

    <!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title> ...

  10. jquery 第一节 什么是jQuery

    简单来说,jQuery就是javascript的一个框架,也可以说是javascript的一个库.