题目链接: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. kafka工作原理介绍

    两张图读懂kafka应用: Kafka 中的术语   broker:中间的kafka cluster,存储消息,是由多个server组成的集群.  topic:kafka给消息提供的分类方式.brok ...

  2. word2vec 注意事项

    Hierarchical Softmax是一种对输出层进行优化的策略,输出层从原始模型的利用softmax计算概率值改为了利用Huffman树计算概率值.一开始我们可以用以词表中的全部词作为叶子节点, ...

  3. 1个多商户、多平台版 微信小程序(多商户、多平台版),影城行业、影业连锁 多商户、多平台版微信小程序。(基于多平台版,支持在业务上 可给 每个单独影城 分发定制单独的小程序版本)

    1个 影城行业 微信小程序(多商户.多平台版), 影业连锁 多商户.多平台版微信小程序.(基于多平台版,支持在业务上 可给 每个单独影城 分发定制单独的小程序版本) 资讯QQ: 876635409  ...

  4. django学习:整体思路与方向

    学习django的原因,主要是对于他的高效和兴趣,原先研究了一些,但是毫无头绪. 最近连续看了2-3天,似乎有些眉目.django主要是一个网页设计的工具就结构来说,分为project和app两个层级 ...

  5. js bind绑定事件

    <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...

  6. zuul源码(2)

    路由 路由是网关的核心功能,既然在spring的框架下,那就要按Spring的规矩来. 路由规则类:org.springframework.cloud.netflix.zuul.filters.Rou ...

  7. css常用选择器选择器

    tap选择器 ulclass选择器 .id选择器 #后代选择器 a b子代选择器 a>b兄弟选择器 a + b 以a为参考给b加样式属性选择器 input [type="text&qu ...

  8. 利用python破解sqlserver账号密码

    一.编写密码测试函数 在用python连接mssql数据库的时候,通常会使用pymssql模板中的connect函数,格式如下: connect(server,user,password,databa ...

  9. 自己写的C#三层代码生成器

    思来想去用T4生成代码要学习它的语法,C#本身能很简单地生成txt文件,为啥不直接批量替换模板方式自己写个的三层代码生成器.说干就干,2个小时搞定.当然各层还可以做的更精细,比如DAL层的Add方法I ...

  10. WinForm控件开发总结目录

    WinForm控件开发总结(一)------开篇 WinForm控件开发总结(二)------使用和调试自定义控件 WinForm控件开发总结(三)------认识WinForm控件常用的Attrib ...