789. Escape The Ghosts
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
.
Approach #1: Math. [Java]
class Solution {
public boolean escapeGhosts(int[][] ghosts, int[] target) {
int max = Math.abs(target[0]) + Math.abs(target[1]);
for (int[] ghost : ghosts) {
int dis = Math.abs(ghost[0] - target[0]) + Math.abs(ghost[1] - target[1]);
if (dis <= max) return false;
} return true;
}
}
Analysis:
The best tactic for any ghost is to reach the target before pacman and block the exit.
Note that we do not require that any ghost reaches pacman (which will never happen on an infinite grid for a single ghos and be much harder to determine for multiple ghost).
We only require that pacman can or cannot reach the target with optimal ghost strategy.
If any ghost has the same or lower distance to the target, then is can get there first and wait. Pacman cannot reach the target, although he would not necessarily be killed by a ghost.
If pacman is closer to the target than any ghost, he goes there along the most direct path.
Since we are working on a 2D grid, distances are measured as Manhattan distance.
Reference:
https://leetcode.com/problems/escape-the-ghosts/discuss/116511/Short-with-explanation-python
789. Escape The Ghosts的更多相关文章
- 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 逃离鬼魂
You are playing a simplified Pacman game. You start at the point (0, 0), and your destination is (ta ...
- LeetCode 789. Escape The Ghosts
题目链接:https://leetcode.com/problems/escape-the-ghosts/description/ You are playing a simplified Pacma ...
- 【LeetCode】789. Escape The Ghosts 解题报告(Python & C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 日期 题目地址:https://leetcode.c ...
- [LeetCode] Escape The Ghosts 逃离鬼魂
You are playing a simplified Pacman game. You start at the point (0, 0), and your destination is (ta ...
- [Swift]LeetCode789. 逃脱阻碍者 | 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 ...
- LeetCode All in One题解汇总(持续更新中...)
突然很想刷刷题,LeetCode是一个不错的选择,忽略了输入输出,更好的突出了算法,省去了不少时间. dalao们发现了任何错误,或是代码无法通过,或是有更好的解法,或是有任何疑问和建议的话,可以在对 ...
- leetcode 学习心得 (4)
645. Set Mismatch The set S originally contains numbers from 1 to n. But unfortunately, due to the d ...
随机推荐
- 都学Python了,C++难道真的用不着了吗?
本文首发 | 公众号:lunvey 人人都在学Python,我还学C++吗? 现在只要提及编程语言,得到的答复都是:学Python,有未来! 大家可能有一个误区,数据分析带火了Python,让人们 ...
- socket短连接太多,accept次数很多导致主线程CPU占满,工作线程CPU占用率低
1.使用epoll的ET模式: 2.开启reuseport方法: Linux 最新SO_REUSEPORT特性:http://www.mamicode.com/info-detail-2201958. ...
- Hi3559AV100 SDK的详细安装过程及问题解决方法
下面给出Hi3559AV100 SDK的安装的详细步骤(一些注意事项可以参照我之前写的随笔-<Hi3519 SDK搭建.问题总结及yolov3 RFCN的运行结果与测试 >): 1.开发环 ...
- jq日期与时间戳互相转换
方法1:$.extend({ myTime: { CurTime: function () { return Date.parse(new Date()) / 1000; }, DateToUnix: ...
- pytorch(10)transform模块(进阶)
图像变换 Pad 对图片边缘进行填充 transforms.Pad(padding,fill=0,padding_mode='constant') padding:设置填充大小,(a,b,c,d)左上 ...
- MySQL全面瓦解24:构建高性能索引(策略篇)
学习如果构建高性能的索引之前,我们先来了解下之前的知识,以下两篇是基础原理,了解之后,对面后续索引构建的原则和优化方法会有更清晰的理解: MySQL全面瓦解22:索引的介绍和原理分析 MySQL全面瓦 ...
- SHELL编程概念&变量剖析
一.shell软件概念和应用场景 1) 学习Linux技术,不是为了学习系统安装.命令操作.用户权限.配置IP.网络管理,学习Linux技术重点:基于Linux系统部署和维护各种应用软件.程序(Apa ...
- Prometheus自定义指标
1. 自定义指标 为了注册自定义指标,请将MeterRegistry注入到组件中,例如: public class Dictionary { private final List<String ...
- Android R 新特性分析及适配指南
Android R(Android 11 API 30)于2020年9月9日正式发布,随国内各终端厂商在售Android设备的版本更新升级,应用软件对Android R 版本的兼容适配已迫在眉睫. 对 ...
- 一个名叫Sentinel-Rules-SDK的组件,使得Sentinel的流控&熔断规则的配置更加方便
原文链接:一个名叫Sentinel-Rules-SDK的组件,使得Sentinel的流控&熔断规则的配置更加方便 1 Sentinel 是什么? 随着微服务的流行,服务和服务之间的稳定性变得越 ...