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. nginx二级域名反向代理

    nginx二级域名反向代理 添加两个开发测试的域名 test.xxx.com :8088 testmobile.xxx.com: 8089 内网地址:127.0.0.1 外网地址:127.0.0.1 ...

  2. Appium 环境配置

    前言 Appium 作为移动端自动化测试工具在业界非常流行,特别是在当前移动互联网背景下,很多公司基于此框架来开展自动化测试.但 appium 的环境配置相对 selenium 来说复杂的多,让很多同 ...

  3. 博云 x 某农商行 | 银行信息化运维系统升级的最佳实践

    随着银行新一代信息化运维系统建设的推进,应用系统更新换代速度明显提升.数字化转型的发展对银行业务需求的敏捷性提出了越来越高的要求,促进敏捷开发和资源敏捷部署成为大势所趋. 背景 江苏某农村商业银行成立 ...

  4. 第二十二节:Asp.Net Core中使用托管服务实现后台任务

    1. 说明 BackgroundService 是用于实现长时间运行的 IHostedService 的基类,使用程序集:Microsoft.Extensions.Hosting. 2. 实现方式 继 ...

  5. ELK学习笔记之Elasticsearch和Kibana数据导出实战

    0x00 问题引出 以下两个导出问题来自Elastic中文社区. 问题1.kibana怎么导出查询数据?问题2:elasticsearch数据导出就像数据库数据导出一样,elasticsearch可以 ...

  6. .NET Core跨平台部署于Docker(Centos)- 视频教程

    (双击全屏播放) 往期教程: .NET开发框架(一)-框架介绍与视频演示 .NET开发框架(二)-框架功能简述 .NET开发框架(三)-高可用服务器端设计 .NET开发框架(四)-服务器IIS实践教程 ...

  7. JavaScript 设计模式分类

    设计模式的目的是为了提高代码的整洁性.降低代码的资源占用量. JS中的设计模式可分为以下三种: 1. 创建型设计模式 说明:专注于处理对象创建的机制,以合适的方式创建对象,以此来降低创建对象过程的复杂 ...

  8. Java面试题:Java中的集合及其继承关系

    关于集合的体系是每个人都应该烂熟于心的,尤其是对我们经常使用的List,Map的原理更该如此.这里我们看这张图即可: 1.List.Set.Map是否继承自Collection接口? List.Set ...

  9. 夜神模拟器怎么连接adb

    夜神模拟器连接不了adb的原因主要是adb的版本与夜神模拟器adb版本不一致造成的,具体的解决办法请看下面的操作步骤. 工具/原料   电脑 安装了夜神模拟器 方法/步骤   1 使用快捷键win + ...

  10. Linux用户和权限——用户和用户组管理

    Linux用户和权限——用户和用户组管理 摘要:本文主要介绍了Linux系统中的用户和用户组管理. 用户和用户组 含义 在使用Linux系统时,虽然输入的是自己的用户名和密码,但其实Linux并不认识 ...