热门标签 | HotTags
当前位置:  开发笔记 > 编程语言 > 正文

开发笔记:AttackonAlphaZet

本文由编程笔记#小编为大家整理,主要介绍了AttackonAlpha-Zet相关的知识,希望对你有一定的参考价值。题目链接:https://nanti.jisuanke.c
本文由编程笔记#小编为大家整理,主要介绍了Attack on Alpha-Zet相关的知识,希望对你有一定的参考价值。


题目链接:https://nanti.jisuanke.com/t/28852



7998: Attack on Alpha-Zet


时间限制: 1 Sec  内存限制: 512 MB
提交: 28  解决: 9

题目描述


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

 


来源/分类


GCPC2018 

题意:给你一个n*m的矩阵,保证了两点之间一定只有一条路使之连通,再给你接下来要走的点的顺序,问最短需要多少的长度?

做法:首先我们需要处理输入,输入n*m的矩阵,实则输入的是一个(n+1)*(2*m+1)的矩阵,像是模拟一样的,给每个间隙hash一下编个号,于是根据原图的联通性可以将编号构成一棵无向树,知道了每一步的坐标,由上一步走到这一步,在无向树上一定是先走到lca再走到下一个节点最近,于是问题就转化成了树上两点之间最近距离,于是lca写一下就过了。

吐槽自己的lca模板 已撕掉

代码如下:


/*
_ _ _ _ _ _
| _ _ _ _ _|
|_ _ _ _ _ _|
0123456789*
5 5
_ _ _ _ _
|_ _ |_ |
| _| | _|
| |_ _| |
| _ _ |
|_|_ _ _|_|
7
4 4
1 4
3 1
4 5
1 2
2 2
5 4
n+1行 2*m+1列
2 4
_ _ _ _
| _ _ _|
|_ _ _ _|
2018.9.7
第一法超时是本人的LCA模板不够友好 于是Flower现在更换模板
*/
#include

#include
<iostream>
#include

#include
<string.h>
#include

#define rep(i , j , k) for(int i=j; i<=k; i++)
using namespace std;
const int maxn = 1005;
const int maxm = 2005;
int n , m;
char mp[maxn][maxm];
struct EDGE
{
int to , next;
}edge[maxn
*maxn*2];
int head[maxn*maxn];
int tot;
int father[20][maxn*maxn] , depth[maxn*maxn]; ///20是log(maxn)的大小
void init1()
{
memset(head ,
-1, sizeof(head));
tot
= 0;
}
void add(int u , int v)
{
edge[tot].to
= v;
edge[tot].next
= head[u];
head[u]
= tot++;
}
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(int u , int f)
{
father[
0][u] = f;
for(int i=head[u]; i!=-1; i=edge[i].next)
{
int v = edge[i].to;
if( v == f) continue;
depth[v]
= depth[u]+1;
dfs(v , u);
}
}
void init(int N)
{
depth[
1] = 1;
dfs(
1,-1);
for(int i=1; i<20; i++) ///20不带等号
{
for(int j=1; j<=N; j++)
{
father[i][j]
= father[i-1][father[i-1][j]];
}
}
}
int lca(int x , int y)
{
if(depth[x] > depth[y]) swap(x , y);
for(int i=0; i<20; i++)
{
if((depth[y]-depth[x])>>i&1)
y
= father[i][y];
}
if(x == y) return x;
for(int i=19; i>=0; i--)
{
if(father[i][x] != father[i][y])
{
x
= father[i][x];
y
= father[i][y];
}
}
return father[0][x];
}
void judge()
{
for(int i=1; i<=n*m; i++)
{
printf(
"%d..%d..
" , i , depth[i]);
}
}
void solve()
{
// judge();
long long res = 0;
int a , b , tmp1 , tmp2 , q , tmp3;
scanf(
"%d" , &q);
for(int i=1; i<=q; i++)
{
scanf(
"%d%d" , &a , &b);
tmp2
= num(a , b*2-1);
if(i != 1)
{
tmp3
= lca(tmp1 , tmp2); ///tmp3来记录tmp1与tmp2的LCA 现在图已经建好了 节点编号是1~n*m 链表存图 求LCA
res += ((depth[tmp1]-depth[tmp3])+(depth[tmp2]-depth[tmp3]));
}
tmp1
= tmp2;
}
printf(
"%lld
" , res);
}
void debug()
{
for(int i=0; i<=n*m; i++)
{
printf(
"%d+++++++++++
" , i);
for(int j=head[i]; j!=-1; j=edge[j].next)
{
printf(
"%d " , edge[j].to);
}
printf(
"
");
}
}
int main()
{
while( scanf("%d%d" , &n , &m) != EOF )
{
init1();
input();
build();
///到这里终于把无向树建出来了
init(n*m);
// debug();
solve();
}
return 0;
}

 

















推荐阅读
  • 在1995年,Simon Plouffe 发现了一种特殊的求和方法来表示某些常数。两年后,Bailey 和 Borwein 在他们的论文中发表了这一发现,这种方法被命名为 Bailey-Borwein-Plouffe (BBP) 公式。该问题要求计算圆周率 π 的第 n 个十六进制数字。 ... [详细]
  • 问题描述现在,不管开发一个多大的系统(至少我现在的部门是这样的),都会带一个日志功能;在实际开发过程中 ... [详细]
  • linux网络子系统分析(二)—— 协议栈分层框架的建立
    目录一、综述二、INET的初始化2.1INET接口注册2.2抽象实体的建立2.3代码细节分析2.3.1socket参数三、其他协议3.1PF_PACKET3.2P ... [详细]
  • 二维码的实现与应用
    本文介绍了二维码的基本概念、分类及其优缺点,并详细描述了如何使用Java编程语言结合第三方库(如ZXing和qrcode.jar)来实现二维码的生成与解析。 ... [详细]
  • 本文通过C++语言实现了一个递归算法,用于解析并计算数学表达式的值。该算法能够处理加法、减法、乘法和除法操作。 ... [详细]
  • 洛谷 P4009 汽车加油行驶问题 解析
    探讨了经典算法题目——汽车加油行驶问题,通过网络流和费用流的视角,深入解析了该问题的解决方案。本文将详细阐述如何利用最短路径算法解决这一问题,并提供详细的代码实现。 ... [详细]
  • 理解浏览器历史记录(2)hashchange、pushState
    阅读目录1.hashchange2.pushState本文也是一篇基础文章。继上文之后,本打算去研究pushState,偶然在一些信息中发现了锚点变 ... [详细]
  • Android与JUnit集成测试实践
    本文探讨了如何在Android项目中集成JUnit进行单元测试,并详细介绍了修改AndroidManifest.xml文件以支持测试的方法。 ... [详细]
  • 本文详细探讨了 Java 中 org.apache.gobblin.metrics.GobblinMetrics 类下的 getName() 方法的使用场景及其代码实现,提供了多个实际应用示例以加深理解。 ... [详细]
  • c语言二元插值,二维线性插值c语言
    c语言二元插值,二维线性插值c语言 ... [详细]
  • 本文探讨了如何通过优化 DOM 操作来提升 JavaScript 的性能,包括使用 `createElement` 函数、动画元素、理解重绘事件及处理鼠标滚动事件等关键主题。 ... [详细]
  • Logging all MySQL queries into the Slow Log
    MySQLoptionallylogsslowqueriesintotheSlowQueryLog–orjustSlowLog,asfriendscallit.However,Thereareseveralreasonstologallqueries.Thislistisnotexhaustive:Belowyoucanfindthevariablestochange,astheyshouldbewritteninth ... [详细]
  • 在Qt框架中,信号与槽机制是一种独特的组件间通信方式。本文探讨了这一机制相较于传统的C风格回调函数所具有的优势,并分析了其潜在的不足之处。 ... [详细]
  • 使用QT构建基础串口辅助工具
    本文详细介绍了如何利用QT框架创建一个简易的串口助手应用程序,包括项目的建立、界面设计与编程实现、运行测试以及最终的应用程序打包。 ... [详细]
  • 线段树详解与实现
    本文详细介绍了线段树的基本概念及其在编程竞赛中的应用,并提供了一个具体的线段树实现代码示例。 ... [详细]
author-avatar
艺卓显示、巴可投影
这个家伙很懒,什么也没留下!
PHP1.CN | 中国最专业的PHP中文社区 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved | 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有