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们发现了任何错误,或是代码无法通过,或是有更好的解法,或是有任何疑问和建议的话,可以在对 ...
随机推荐
- 《xxx系统》质量属性战术
<xxx系统>质量属性战术 可用性:重新引入 用户每填写一份表单,表单查看中即时更新所有信息. 易用性:系统主动 对于下拉框的选项较多时,用户可先进行部分输入,系统进行实时检索显示与用户输 ...
- SQL数据库中查询中加N'' 前缀是什么意思
It's declaring the string as nvarchar data type, rather than varchar You may have seen Transact-SQL ...
- SQLite 读取数据时,随机顺序
SELECT * FROM [数据表] ORDER BY random() 通过 random() 这个函数来排序
- 前端浏览器自动刷新神器:Browsersync
[安装] 1 npm install -g browser-sync [静态项目使用browsersync] 自己可以去browsersync官网查看,其实使用很简单,总结下就是: 1 browser ...
- 安装python的第三方Pillow库
方法/步骤 找到easy_install.exe工具.在windows下安装Python后,在其安装路径下的scripts文件中默认安装好了easy_install工具.完整路径如下例:D:\Pyth ...
- 作业一 :关于C语言
C语言是计算机专业的基础课,同时也是计算机专业的第一个入门语言,学好C语言母庸质疑.就目前来看,在C语言中已经学习的内容有:基本运算符及表达式.输入输出函数.选择 结构程序设计.循环结构程序设计.数组 ...
- centos7图形界面安装
系统笔者采用的是centos7 可以通过/etc/inittab文件看到 yum groupinstall "X Window System" -y # 首 ...
- Centos搭建NFS服务及客户端访问
一.环境介绍 NFS服务端:192.168.200.101 NFS客户端:192.168.200.102 二.服务器端安装配置 1.查看rpcbind和nfs-utils安装包是否安装 [root@b ...
- 记SCOI2019
离精英体验营结束已两周的,要哭的要笑的现在也一定释怀了.是时候冷静分析一下这次的考试了.时间序虽然有流水账的嫌疑,但这毕竟是OI界的流行风气. day0 早上坐学校包的商务车去了电子科技大学.走在来过 ...
- jQuery汇总
closest() 方法返回被选元素的第一个祖先元素. $("span").closest("ul")返回 <span> 的第一个祖先元素,是一个 ...