LeetCode 789. Escape The Ghosts
题目链接: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的更多相关文章
- [LeetCode] 789. Escape The Ghosts 逃离鬼魂
You are playing a simplified Pacman game. You start at the point (0, 0), and your destination is (ta ...
- LC 789. Escape The Ghosts
You are playing a simplified Pacman game. You start at the point (0, 0), and your destination is(tar ...
- 【LeetCode】789. Escape The Ghosts 解题报告(Python & C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 日期 题目地址:https://leetcode.c ...
- 789. Escape The Ghosts
You are playing a simplified Pacman game. You start at the point (0, 0), and your destination is (ta ...
- [LeetCode] Escape The Ghosts 逃离鬼魂
You are playing a simplified Pacman game. You start at the point (0, 0), and your destination is (ta ...
- 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 ...
- [Swift]LeetCode789. 逃脱阻碍者 | Escape The Ghosts
You are playing a simplified Pacman game. You start at the point (0, 0), and your destination is (ta ...
- Java实现 LeetCode 789 逃脱阻碍者(曼哈顿距离)
789. 逃脱阻碍者 你在进行一个简化版的吃豆人游戏.你从 (0, 0) 点开始出发,你的目的地是 (target[0], target[1]) .地图上有一些阻碍者,第 i 个阻碍者从 (ghost ...
- LeetCode All in One题解汇总(持续更新中...)
突然很想刷刷题,LeetCode是一个不错的选择,忽略了输入输出,更好的突出了算法,省去了不少时间. dalao们发现了任何错误,或是代码无法通过,或是有更好的解法,或是有任何疑问和建议的话,可以在对 ...
随机推荐
- Linux下MySQL在知道密码的情况下修改密码
1.在知道原密码的情况下,进入MySQL: mysql -u root -p 2.进入 mysql数据库,然后通过语句修改密码: 我的MySQL版本是:mysql Ver 14.14 Distrib ...
- 2019 Power BI最Top50面试题,助你面试脱颖而出系列<中>
敲黑板啦!!! 来来来 大家双眼看黑板 开始划重点啦 这篇大部分是"考试"必考题 你们一定要好好的牢记在心 一分都不要放过 刷题中... Power BI面试题目-DAX 9)什么 ...
- Linux报错之ping: www.baidu.com: Name or service not known
Linux报错之ping: www.baidu.com: Name or service not known 出现这个以后,首先去ping下主机Ip,发现能ping通,但是出现另一个问题Destina ...
- 记录一次axios请求造成的数组初始化失败
axios请求是一个异步的请求,简单来讲就是在做其他事情的时候可以把这个先放一边等其他的事情做完后再来做这件事件. 我之前这样调用了一个方法: mounted() { this.first() thi ...
- 在java中,异常抛出点后程序的执行情况
1.在throw语句,即自定义的抛出异常语句后面的代码并不会执行,会提示错误,编译器并不可以正常编译. 2.若在一个条件语句中抛出一个异常,程序可以编译,但不会运行(dead code). 3.若在一 ...
- 引擎设计跟踪(九.14.3.2) Deferred shading的后续实现和优化
最近完成了deferred shading和spot light的支持, 并作了一部分优化. 之前forward shading也只支持方向光, 现在也支持了点光源和探照光. 对于forward sh ...
- Java(常用排序算法)
冒泡排序 比较相邻的元素.如果第一个比第二个大,就交换他们两个,对每一对相邻元素作同样的工作,从开始第一对到结尾的最后一对.在这一点,最后的元素应该会是最大的数. 针对所有的元素重复以上的步骤,除了最 ...
- Centos搭建NFS服务及客户端访问
一.环境介绍 NFS服务端:192.168.200.101 NFS客户端:192.168.200.102 二.服务器端安装配置 1.查看rpcbind和nfs-utils安装包是否安装 [root@b ...
- 多个字符串有相同的hashcode(没见到大于8的时候转成红黑树)
public static void main(String[] a){ byte[] b1 = {33 , 123 ,124}; byte[] b2 = {33 , 124 , 93}; byte[ ...
- java中super关键字的作用
1.super关键字可以在子类的构造方法中显示地调用父类的构造方法,super()必须为子类构造函数中的第一行. 2.super可以用来访问父类的成员方法或变量,当子类成员变量或方法与父类有相同的名字 ...