题目链接:https://leetcode.com/problems/escape-the-ghosts/description/

You are playing a simplified Pacman game. You start at the point (0, 0), and your destination is (target[0], target[1]). There are several ghosts on the map, the i-th ghost starts at (ghosts[i][0], ghosts[i][1]).

Each turn, you and all ghosts simultaneously *may* move in one of 4 cardinal directions: north, east, west, or south, going from the previous point to a new point 1 unit of distance away.

You escape if and only if you can reach the target before any ghost reaches you (for any given moves the ghosts may take.)  If you reach any square (including the target) at the same time as a ghost, it doesn't count as an escape.

Return True if and only if it is possible to escape.

Example 1:
Input:
ghosts = [[1, 0], [0, 3]]
target = [0, 1]
Output: true
Explanation:
You can directly reach the destination (0, 1) at time 1, while the ghosts located at (1, 0) or (0, 3) have no way to catch up with you.
Example 2:
Input:
ghosts = [[1, 0]]
target = [2, 0]
Output: false
Explanation:
You need to reach the destination (2, 0), but the ghost at (1, 0) lies between you and the destination.
Example 3:
Input:
ghosts = [[2, 0]]
target = [1, 0]
Output: false
Explanation:
The ghost can reach the target at the same time as you.

Note:

  • All points have coordinates with absolute value <= 10000.
  • The number of ghosts will not exceed 100.

这道题乍看之下有点摸不到头脑,而且很容易想到当年玩吃豆人的时候各种奇怪的corner case死法从而对题解想的过于复杂(比如DFS搜索解空间树什么的,可是想象一下每一个ghose每一步都可以有5个next state,要是有100个鬼就是TM 5^100 次方,做个P。。)。其实这道题很简单,因为ghose可以呆着不动,所以就可以直接奔着target走,如果能比你先到,那么就在target等着你就行,你就必输。所以归根结底就是算一下每一个ghose 到target的Manhattan Distance如果比[0,0]到target的距离小,那就是False,反之则为True。要注意coordinate可能是负数,所以要加abs。代码很好写:

class Solution(object):
def escapeGhosts(self, ghosts, target):
"""
:type ghosts: List[List[int]]
:type target: List[int]
:rtype: bool
"""
dist = abs(target[0]) + abs(target[1])
for x, y in ghosts:
if abs(x-target[0]) + abs(y-target[1]) <= dist:
return False
return True

LeetCode 789. Escape The Ghosts的更多相关文章

  1. [LeetCode] 789. Escape The Ghosts 逃离鬼魂

    You are playing a simplified Pacman game. You start at the point (0, 0), and your destination is (ta ...

  2. LC 789. Escape The Ghosts

    You are playing a simplified Pacman game. You start at the point (0, 0), and your destination is(tar ...

  3. 【LeetCode】789. Escape The Ghosts 解题报告(Python & C++)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 日期 题目地址:https://leetcode.c ...

  4. 789. Escape The Ghosts

    You are playing a simplified Pacman game. You start at the point (0, 0), and your destination is (ta ...

  5. [LeetCode] Escape The Ghosts 逃离鬼魂

    You are playing a simplified Pacman game. You start at the point (0, 0), and your destination is (ta ...

  6. 73th LeetCode Weekly Contest Escape The Ghosts

    You are playing a simplified Pacman game. You start at the point (0, 0), and your destination is(tar ...

  7. [Swift]LeetCode789. 逃脱阻碍者 | Escape The Ghosts

    You are playing a simplified Pacman game. You start at the point (0, 0), and your destination is (ta ...

  8. Java实现 LeetCode 789 逃脱阻碍者(曼哈顿距离)

    789. 逃脱阻碍者 你在进行一个简化版的吃豆人游戏.你从 (0, 0) 点开始出发,你的目的地是 (target[0], target[1]) .地图上有一些阻碍者,第 i 个阻碍者从 (ghost ...

  9. LeetCode All in One题解汇总(持续更新中...)

    突然很想刷刷题,LeetCode是一个不错的选择,忽略了输入输出,更好的突出了算法,省去了不少时间. dalao们发现了任何错误,或是代码无法通过,或是有更好的解法,或是有任何疑问和建议的话,可以在对 ...

随机推荐

  1. http error: "request body stream exhausted"

    'request body stream exhausted' after authentication challenge #661  Closed aburgel opened this issu ...

  2. php中的内存管理的介绍(转)

    本篇文章给大家带来的内容是关于php中的内存管理的介绍,有一定的参考价值,有需要的朋友可以参考一下,希望对你有所帮助. 一.php内存管理概述——Zend引擎 由于计算机的内存由操作系统进行管理,所以 ...

  3. Mysql远程访问报错2003

    如果没有权限新建一个mysql用户给添加远程连接的权限(推荐设置) 1.例如,你想admin使用123456从任何主机连接到mysql服务器的话. GRANT ALL PRIVILEGES ON *. ...

  4. DPDK - gsg文档(介绍与编译)

    && Hugepage 提前分配hugepage内存,可以减少虚拟页地址转换到物理地址所需的时间. 相关命令: 在虚拟机上运行时,在没有NUMA的机器上,执行“echo 1024 &g ...

  5. @EnableScheduling注解

    @EnableScheduling 开启对定时任务的支持       其中Scheduled注解中有以下几个参数: 1.cron是设置定时执行的表达式,如 0 0/5 * * * ?每隔五分钟执行一次 ...

  6. 前端自动化构建工具 gulp 学习笔记 一、

    一.我对gulp的初期理解 是一种前端辅助开发工具 可以帮你把js,css,img等文件 合并.压缩,图片好像是合并为精灵图,合并为精灵图之后,还会生成一个css样式表. 官方解说是:基于流的自动化构 ...

  7. 高通平台获取secure boot,串号等状态

    adb shell下 运行./system/bin/r address 其中address对应各个flag参数的地址,具体如下: 无法打开/dev/mem节点(没有该节点),这时只需在内核配置中选上C ...

  8. 学习笔记CB003:分块、标记、关系抽取、文法特征结构

    分块,根据句子的词和词性,按照规则组织合分块,分块代表实体.常见实体,组织.人员.地点.日期.时间.名词短语分块(NP-chunking),通过词性标记.规则识别,通过机器学习方法识别.介词短语(PP ...

  9. 学习笔记DL003:神经网络第二、三次浪潮,数据量、模型规模,精度、复杂度,对现实世界冲击

    神经科学,依靠单一深度学习算法解决不同任务.视觉信号传送到听觉区域,大脑听学习处理区域学会“看”(Von Melchner et al., 2000).计算单元互相作用变智能.新认知机(Fukushi ...

  10. 学习笔记TF057:TensorFlow MNIST,卷积神经网络、循环神经网络、无监督学习

    MNIST 卷积神经网络.https://github.com/nlintz/TensorFlow-Tutorials/blob/master/05_convolutional_net.py .Ten ...