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.

  1. Example 1:
  2. Input:
  3. ghosts = [[1, 0], [0, 3]]
  4. target = [0, 1]
  5. Output: true
  6. Explanation:
  7. 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.
  1. Example 2:
  2. Input:
  3. ghosts = [[1, 0]]
  4. target = [2, 0]
  5. Output: false
  6. Explanation:
  7. You need to reach the destination (2, 0), but the ghost at (1, 0) lies between you and the destination.
  1. Example 3:
  2. Input:
  3. ghosts = [[2, 0]]
  4. target = [1, 0]
  5. Output: false
  6. Explanation:
  7. 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.

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

  1. class Solution {
  2. public:
  3. int dist(const vector<int>& a, const vector<int>& b){
  4. return abs(a[] - b[]) + abs(a[] - b[]);
  5. }
  6.  
  7. bool escapeGhosts(vector<vector<int>>& ghosts, vector<int>& target) {
  8. int t = dist({,}, target);
  9. for(int i=; i < ghosts.size(); i++){
  10. if(dist(ghosts[i], target) <= t){
  11. return false;
  12. }
  13. }
  14. return true;
  15. }
  16. };

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. 第十章、json和pickle模块

    目录 第十章.json和pickle模块 一.序列化 二.json 三.pickle模块 第十章.json和pickle模块 一.序列化 把对象(变量)从内存中变成可存储或传输的过程称之为序列化, 序 ...

  2. linux重装docker-compose后无法执行docker-compose命令

    背景 使用自动化脚本重装docker和docker-compose(但脚本中未对旧版本的docker-compose进行任何处理,比如卸载删除) 导致执行docker-compose命令时报了错,大多 ...

  3. navicat连接阿里云mysql

    1.服务器控制台在安全组配置3306端口 2.进入 /etc/ssh/sshd_config 在最下面 加入下面代码 KexAlgorithms diffie-hellman-group1-sha1, ...

  4. fastadmin中关联表时A为主表,想让B表和C表关联时怎么办?

    $sql = Db::connect('数据库')->table('C表')->where('status', 'normal')->field('字段 别称[不可与其他表重复]') ...

  5. How to: Compile Linux kernel 2.6

      Compiling custom kernel has its own advantages and disadvantages. However, new Linux user / admin ...

  6. Kernel boot options

    There are three ways to pass options to the kernel and thus control its behavior: When building the ...

  7. 使用Gallery制作图片浏览器

    MainActivity.class public class MainActivity extends AppCompatActivity implements AdapterView.OnItem ...

  8. python中的函数def和函数的参数

    '''函数: 1.减少代码重用性 2.易维护 3.可扩展性强 4.类型function 定义函数: def 函数变量名(): 函数的调用: 1.函数名加括号 2.函数如果没被调用,不会去执行函数内部的 ...

  9. 第十五届四川省省赛 SCU - 4439 Vertex Cover

    给你一个一般图 保证每条边的一端下标不大于30 问最小覆盖集的大小为多少 爆搜:枚举前30个点是否在覆盖集内 剪枝1:如果不在的话 那么他所连的下标大于30的点都必须选 剪纸2:最优解剪枝 #incl ...

  10. Summer training #11

    A:水 #include <bits/stdc++.h> #include <cstring> #include <iostream> #include <a ...