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.

这道题就是经典的吃豆人游戏啦,不过是简化版,小人只能躲开鬼魂,并不能吃大力丸,反干鬼魂。小人在原点,有若干个鬼魂在不同的位置,给了一个目标点,问小人能不能安全到达目标点。这里的鬼魂的设定跟游戏中的一样,都是很智能的,会朝着你移动,而且这里设定了如果跟鬼魂同时到达目标点也算输。那么实际上这道题就是要求出小人到目标点的最短距离,注意这里的距离不是两点之间的 Euclidean 距离,而应该是曼哈顿距离,即横纵坐标分别求差的绝对值再相加。求出小人到目标点到最短距离后,还要求每个鬼魂到目标点的最短距离,如果有一个鬼魂到目标带你的最短距离小于等于小人到目标点到最短距的话,那么就返回 false,否则返回 true,参见代码如下:

解法一:

class Solution {
public:
bool escapeGhosts(vector<vector<int>>& ghosts, vector<int>& target) {
int dist = abs(target[]) + abs(target[]), mn = INT_MAX;
for (auto ghost : ghosts) {
int t = abs(ghost[] - target[]) + abs(ghost[] - target[]);
mn = min(mn, t);
}
return dist < mn;
}
};

我们可以对上面的解法进行一个小优化,就是其实并不需要算完每一个鬼魂到目标点到最短距离,而是每算一个就进行比较,只要小于等于小人到目标点的最短距离了,就直接返回 false。循环退出后返回 true,参见代码如下:

解法二:

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

Github 同步地址:

https://github.com/grandyang/leetcode/issues/789

参考资料:

https://leetcode.com/problems/escape-the-ghosts/

https://leetcode.com/problems/escape-the-ghosts/discuss/116507/Java-5-liner

https://leetcode.com/problems/escape-the-ghosts/discuss/116522/C%2B%2BJavaPython-Easy-and-Concise-Solution

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

[LeetCode] 789. Escape The Ghosts 逃离鬼魂的更多相关文章

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

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

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

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

  5. 789. Escape The Ghosts

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

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

  7. [Swift]LeetCode789. 逃脱阻碍者 | Escape The Ghosts

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

  8. Java实现 LeetCode 789 逃脱阻碍者(曼哈顿距离)

    789. 逃脱阻碍者 你在进行一个简化版的吃豆人游戏.你从 (0, 0) 点开始出发,你的目的地是 (target[0], target[1]) .地图上有一些阻碍者,第 i 个阻碍者从 (ghost ...

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

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

随机推荐

  1. Gin框架 - 数据绑定和验证

    概述 上篇文章分享了 Gin 框架使用 Logrus 进行日志记录,这篇文章分享 Gin 框架的数据绑定与验证. 有读者咨询我一个问题,如何让框架的运行日志不输出控制台? 解决方案: engine : ...

  2. c#的文本格式化形式展示

    假设你使用的是新版本的的c#语法 c#的格式化形式有如下几种 string text = "Hello World!"; Console.WriteLine("Hello ...

  3. 一个简单的利用 WebClient 异步下载的示例(三)

    继续上一篇 一个简单的利用 WebClient 异步下载的示例(二) 后,继续优化它. 1. 直接贴代码了: DownloadEntry: public class DownloadEntry { p ...

  4. Linux SELinux 介绍详解

    Linux SELinux 介绍详解 SElinux 简介 SElinux (Security Enhanced Linux)是由美国国家安全局(NSA)开发的.它已被植入到了Linux系统的内核当中 ...

  5. List的Clear方法与RemoveAll方法用法小结

    转自:https://blog.csdn.net/yl2isoft/article/details/17059093 结果分析 执行List的Clear方法和RemoveAll方法,List将清除指定 ...

  6. 微信和QQ可以关闭广告了,每次能关6个月

    微信和QQ可以关闭广告了,这次腾讯真的是良心了,虽然不能完全关闭,但是每次能关6个月,也能清静不少时间. 关闭地址:点击进入

  7. jmeter-分布式压测部署之负载机的设置

    本文分三个部分: 1.windows下负载机的配置 2.Linux下负载机的配置 3.遇到的问题 *************************************************** ...

  8. Java向服务器上传图片

    在比较绚丽多彩的网站或者业务逻辑比较丰富的程序设计过程中,图片的相关操作时必不少的,尤其时图片的上传.还没有彻底摆脱纸质办公可能需要将纸质的文件备份上传,网站的建设可能需要上传用户头像.图片描述等等, ...

  9. Linux中用postfix搭建邮件服务器实战详解

    Linux中用postfix搭建邮件服务器实战详解 postfix是Wietse Venema在IBM的GPL协议之下开发的MTA(邮件传输代理)软件.Postfix试图更快.更容易管理.更安全,同时 ...

  10. 基于JQuery可拖动列表格插件DataTables的踩坑记

    前言 最近项目中在使用能够拖动列调整列位置顺序的表格插件---DataTables,这也是目前我找到的唯一一种存在有这种功能的插件. 在查找使用方法的过程中发现可用案例并不多,且大多言语不详.本文将全 ...