热门标签 | 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;
}

 

















推荐阅读
  • 开发笔记:加密&json&StringIO模块&BytesIO模块
    篇首语:本文由编程笔记#小编为大家整理,主要介绍了加密&json&StringIO模块&BytesIO模块相关的知识,希望对你有一定的参考价值。一、加密加密 ... [详细]
  • GreenDAO快速入门
    前言之前在自己做项目的时候,用到了GreenDAO数据库,其实对于数据库辅助工具库从OrmLite,到litePal再到GreenDAO,总是在不停的切换,但是没有真正去了解他们的 ... [详细]
  • 本文讨论了微软的STL容器类是否线程安全。根据MSDN的回答,STL容器类包括vector、deque、list、queue、stack、priority_queue、valarray、map、hash_map、multimap、hash_multimap、set、hash_set、multiset、hash_multiset、basic_string和bitset。对于单个对象来说,多个线程同时读取是安全的。但如果一个线程正在写入一个对象,那么所有的读写操作都需要进行同步。 ... [详细]
  • Ihaveaworkfolderdirectory.我有一个工作文件夹目录。holderDir.glob(*)>holder[ProjectOne, ... [详细]
  • 在Android开发中,使用Picasso库可以实现对网络图片的等比例缩放。本文介绍了使用Picasso库进行图片缩放的方法,并提供了具体的代码实现。通过获取图片的宽高,计算目标宽度和高度,并创建新图实现等比例缩放。 ... [详细]
  • 本文讨论了在Windows 8上安装gvim中插件时出现的错误加载问题。作者将EasyMotion插件放在了正确的位置,但加载时却出现了错误。作者提供了下载链接和之前放置插件的位置,并列出了出现的错误信息。 ... [详细]
  • CSS3选择器的使用方法详解,提高Web开发效率和精准度
    本文详细介绍了CSS3新增的选择器方法,包括属性选择器的使用。通过CSS3选择器,可以提高Web开发的效率和精准度,使得查找元素更加方便和快捷。同时,本文还对属性选择器的各种用法进行了详细解释,并给出了相应的代码示例。通过学习本文,读者可以更好地掌握CSS3选择器的使用方法,提升自己的Web开发能力。 ... [详细]
  • 个人学习使用:谨慎参考1Client类importcom.thoughtworks.gauge.Step;importcom.thoughtworks.gauge.T ... [详细]
  • 开发笔记:Java是如何读取和写入浏览器Cookies的
    篇首语:本文由编程笔记#小编为大家整理,主要介绍了Java是如何读取和写入浏览器Cookies的相关的知识,希望对你有一定的参考价值。首先我 ... [详细]
  • 模板引擎StringTemplate的使用方法和特点
    本文介绍了模板引擎StringTemplate的使用方法和特点,包括强制Model和View的分离、Lazy-Evaluation、Recursive enable等。同时,还介绍了StringTemplate语法中的属性和普通字符的使用方法,并提供了向模板填充属性的示例代码。 ... [详细]
  • Java程序设计第4周学习总结及注释应用的开发笔记
    本文由编程笔记#小编为大家整理,主要介绍了201521123087《Java程序设计》第4周学习总结相关的知识,包括注释的应用和使用类的注释与方法的注释进行注释的方法,并在Eclipse中查看。摘要内容大约为150字,提供了一定的参考价值。 ... [详细]
  • 本文介绍了一个适用于PHP应用快速接入TRX和TRC20数字资产的开发包,该开发包支持使用自有Tron区块链节点的应用场景,也支持基于Tron官方公共API服务的轻量级部署场景。提供的功能包括生成地址、验证地址、查询余额、交易转账、查询最新区块和查询交易信息等。详细信息可参考tron-php的Github地址:https://github.com/Fenguoz/tron-php。 ... [详细]
  • 本文讨论了在VMWARE5.1的虚拟服务器Windows Server 2008R2上安装oracle 10g客户端时出现的问题,并提供了解决方法。错误日志显示了异常访问违例,通过分析日志中的问题帧,找到了解决问题的线索。文章详细介绍了解决方法,帮助读者顺利安装oracle 10g客户端。 ... [详细]
  • 单页面应用 VS 多页面应用的区别和适用场景
    本文主要介绍了单页面应用(SPA)和多页面应用(MPA)的区别和适用场景。单页面应用只有一个主页面,所有内容都包含在主页面中,页面切换快但需要做相关的调优;多页面应用有多个独立的页面,每个页面都要加载相关资源,页面切换慢但适用于对SEO要求较高的应用。文章还提到了两者在资源加载、过渡动画、路由模式和数据传递方面的差异。 ... [详细]
  • 本文介绍了在Android开发中使用软引用和弱引用的应用。如果一个对象只具有软引用,那么只有在内存不够的情况下才会被回收,可以用来实现内存敏感的高速缓存;而如果一个对象只具有弱引用,不管内存是否足够,都会被垃圾回收器回收。软引用和弱引用还可以与引用队列联合使用,当被引用的对象被回收时,会将引用加入到关联的引用队列中。软引用和弱引用的根本区别在于生命周期的长短,弱引用的对象可能随时被回收,而软引用的对象只有在内存不够时才会被回收。 ... [详细]
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社区 版权所有