Java实现 LeetCode 789 逃脱阻碍者(曼哈顿距离)
789. 逃脱阻碍者
你在进行一个简化版的吃豆人游戏。你从 (0, 0) 点开始出发,你的目的地是 (target[0], target[1]) 。地图上有一些阻碍者,第 i 个阻碍者从 (ghosts[i][0], ghosts[i][1]) 出发。
每一回合,你和阻碍者们可以同时向东,西,南,北四个方向移动,每次可以移动到距离原位置1个单位的新位置。
如果你可以在任何阻碍者抓住你之前到达目的地(阻碍者可以采取任意行动方式),则被视为逃脱成功。如果你和阻碍者同时到达了一个位置(包括目的地)都不算是逃脱成功。
当且仅当你有可能成功逃脱时,输出 True。
示例 1:
输入:
ghosts = [[1, 0], [0, 3]]
target = [0, 1]
输出:true
解释:
你可以直接一步到达目的地(0,1),在(1, 0)或者(0, 3)位置的阻碍者都不可能抓住你。
示例 2:
输入:
ghosts = [[1, 0]]
target = [2, 0]
输出:false
解释:
你需要走到位于(2, 0)的目的地,但是在(1, 0)的阻碍者位于你和目的地之间。
示例 3:
输入:
ghosts = [[2, 0]]
target = [1, 0]
输出:false
解释:
阻碍者可以和你同时达到目的地。
说明:
所有的点的坐标值的绝对值 <= 10000。
阻碍者的数量不会超过 100。
PS:
这个题表达的意思是,想进一切办法,比其他人先到终点,或者让其他人走过终点,
他是重复你走的方向,所以只需要判断一下曼哈顿距离(就是表示两个点在标准坐标系上的绝对轴距之和)就好了,有不懂的欢迎评论
class Solution {
public boolean escapeGhosts(int[][] ghosts, int[] target) {
int[] source = new int[]{0, 0};
for (int[] ghost: ghosts)
if (taxi(ghost, target) <= taxi(source, target))
return false;
return true;
}
public int taxi(int[] P, int[] Q) {
return Math.abs(P[0] - Q[0]) + Math.abs(P[1] - Q[1]);
}
}
PS:注意 最后 送大家一套2020最新Java架构实战教程+大厂面试题库, 点击此处 进来获取 一起交流进步哦!
Java实现 LeetCode 789 逃脱阻碍者(曼哈顿距离)的更多相关文章
- [LeetCode] 789. Escape The Ghosts 逃离鬼魂
You are playing a simplified Pacman game. You start at the point (0, 0), and your destination is (ta ...
- Java for LeetCode 216 Combination Sum III
Find all possible combinations of k numbers that add up to a number n, given that only numbers from ...
- Java for LeetCode 214 Shortest Palindrome
Given a string S, you are allowed to convert it to a palindrome by adding characters in front of it. ...
- Java for LeetCode 212 Word Search II
Given a 2D board and a list of words from the dictionary, find all words in the board. Each word mus ...
- Java for LeetCode 211 Add and Search Word - Data structure design
Design a data structure that supports the following two operations: void addWord(word)bool search(wo ...
- Java for LeetCode 210 Course Schedule II
There are a total of n courses you have to take, labeled from 0 to n - 1. Some courses may have prer ...
- Java for LeetCode 200 Number of Islands
Given a 2d grid map of '1's (land) and '0's (water), count the number of islands. An island is surro ...
- Java for LeetCode 188 Best Time to Buy and Sell Stock IV【HARD】
Say you have an array for which the ith element is the price of a given stock on day i. Design an al ...
- Java for LeetCode 154 Find Minimum in Rotated Sorted Array II
Suppose a sorted array is rotated at some pivot unknown to you beforehand. (i.e., 0 1 2 4 5 6 7 migh ...
随机推荐
- android实现计时器
新建布局文件activity_main.xml <?xml version="1.0" encoding="utf-8"?> <LinearL ...
- Keycloak & Asp.net core webapi 整合跳坑之旅
前言 之前,一直使用IdentityServer4作为.net core程序的外部身份认证程序,ID4的优点自不必说了,缺点就是缺乏完善的管理界面. 后来,学习java quarkus框架时,偶然遇到 ...
- Mysql 常用函数(1)- 常用函数汇总
Mysql常用函数的汇总,可看下面系列文章 Mysql常用函数有哪几类 数值型函数 字符串型函数 日期时间函数 聚合函数 流程控制函数 数值型函数 函数名称 作用 ABS 求绝对值 SQRT 求二次方 ...
- js 获取URL后面传的参数
function GetRequest() { var url = location.search; //获取url中"?"符后的字串 var theRequest = new O ...
- ql的python学习之路-day12
前言:这一节主要学习json和pickle 背景: 相信大家在日常生活中都有接触大型的网络游戏,打游戏的时候都是自己在电脑上操作,自己刷怪升级:当然也会碰到中午去吃饭然后挂机的情况,让电脑自动的刷怪, ...
- SWPU邮件登录界面的仿写(第二次作业)
(一).检查并下载网页元素 在需仿写的页面按F12,点击element,寻找需要的图片元素. (二). 分析网页的布局 查看网页源代码. (三).开始仿写 由于我们的目标是仿写网页,所以可以直接复制网 ...
- SQL拦截器
一.拦截SQL 1.slow log 2.general log 3.mycat sql拦截器 二.mycat sql拦截器 1.配置server.xml # 1.5可用 <system> ...
- [翻译]用于.NET Core的Windows窗体设计器发布
本文由微信公众号<开发者精选资讯>翻译首发,转载请注明来源 今天我们很高兴地宣布,.NET Core项目的Windows窗体设计器现在可以在 Visual Studio 2019 16.6 ...
- 基于vue+Django的简迩音乐用户界面实现
应这次软件工程课程要求,我们团队着力打造一个音乐播放器软件. 软件实现主要采用基于Vue.js+Python Django,前后端分离架构实现网页. 用户界面主要功能:呈现用户收藏歌单歌曲信息,并且提 ...
- Collection接口和list,set子类
Collection接口常用的子接口有:List接口.Set接口List接口常用的子类有:ArrayList类.LinkedList类Set接口常用的子类有:HashSet类.LinkedHashSe ...