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.

Runtime: 4 ms, faster than 98.34% of C++ online submissions for Escape The Ghosts.

没有任何障碍,平面上两个点的最短距离就是坐标差。

class Solution {
public:
int dist(const vector<int>& a, const vector<int>& b){
return abs(a[] - b[]) + abs(a[] - b[]);
} bool escapeGhosts(vector<vector<int>>& ghosts, vector<int>& target) {
int t = dist({,}, target);
for(int i=; i < ghosts.size(); i++){
if(dist(ghosts[i], target) <= t){
return false;
}
}
return true;
}
};

LC 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. LeetCode 789. Escape The Ghosts

    题目链接:https://leetcode.com/problems/escape-the-ghosts/description/ You are playing a simplified Pacma ...

  3. 789. Escape The Ghosts

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

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

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

  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. [Swift]LeetCode789. 逃脱阻碍者 | Escape The Ghosts

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

  7. 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 ...

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

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

  9. leetcode 学习心得 (4)

    645. Set Mismatch The set S originally contains numbers from 1 to n. But unfortunately, due to the d ...

随机推荐

  1. bisect:维护一个有序的列表

    介绍 bisect模块实现了一个算法来向列表中插入元素,同时仍然保证列表有序 有序插入 import bisect ''' 可以使用bisect.insort向一个列表中插入元素 ''' values ...

  2. linux学习笔记七

    #文件权限很重要,有些时候删除和新建文件没有权限根本操作不了,linux一切皆是文件,所以必须得了解下权限了. 文件的一般权限 简单的ls -ld 命令就能看到权限,dr-xr-x---补全应该是dr ...

  3. NFS +inotify+rsync 实现数据的远程挂载与实时增量备份

    NFS 网络文件系统 功能: 用户可以像访问自己的本地文件系统一样使用网络中的远端系统上的文件 原理: 用户进程-->RPC服务(portman)-->tcp/ip协议栈-->远端主 ...

  4. CAN学习方法(知乎)

    作者:心机之花链接:https://www.zhihu.com/question/26776219/answer/244433861来源:知乎著作权归作者所有.商业转载请联系作者获得授权,非商业转载请 ...

  5. PAT Basic 1056 组合数的和 (15 分)

    给定 N 个非 0 的个位数字,用其中任意 2 个数字都可以组合成 1 个 2 位的数字.要求所有可能组合出来的 2 位数字的和.例如给定 2.5.8,则可以组合出:25.28.52.58.82.85 ...

  6. selenium设置Chrome浏览器不出现通知,设置代理IP

    from selenium import webdriver PROXY = "" chrome_options = webdriver.ChromeOptions() prefs ...

  7. D - Yet Another Problem On a Subsequence CodeForces - 1000D (DP,组合数学)

    D - Yet Another Problem On a Subsequence CodeForces - 1000D The sequence of integers a1,a2,-,aka1,a2 ...

  8. div 可滚动但不显示滚动条

    (原) 首先有3个div, 第1个,固定大小是200*200(单位为px,下同) 第2个,不固定大小,其大小要用第3个div把个撑开,但是这个div必需要有滚动条, 第3个,固定大小与第1个div保持 ...

  9. 《Python基础教程》第五章:条件、循环和其他语句

    在Python中赋值运算和比较运算是可以连接的,运算符可以连在一起使用,如:0<age<100 ==运算符判定两个对象是否相等,is判定两者是否等同(同一个对象) 断言,在错误条件出现时直 ...

  10. solr介绍

    solr架构图: 以下是Apache Solr的主要构建块(组件) 请求处理程序 - 发送到Apache Solr的请求由这些请求处理程序处理.请求可以是查询请求或索引更新请求.根据这些请示的要求来选 ...