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 ...
随机推荐
- Linux文件操作命令并举例说明其作用
ls ,常用于查看当前文件下有工作中需要的文件 cd, 常用于进行切换文件的位置 vim,常用于编辑软件系统相关的配置文件 ps –ef|grep jdk,常用语显示跟jdk有关的进程 |:表示 ...
- [hdu1028]整数拆分,生成函数
题意:给一个正整数n,求n的拆分方法数(不考虑顺序) 思路:不妨考虑用1~n来构成n.用多项式表示单个数所有能构成的数,用多项式表示,就相当于卷积运算了. 1 2 3 4 5 6 7 8 9 10 1 ...
- Fibonacci数列的性质
Fibonacci: 0, 1, 1, 2, 3, 5, 8, 13, .... F[0] = 0; 1: gcd(Fn, Fm) = F[gcd(n, m)]; 当n - m = 1 或 2时满足, ...
- 06JAVA面向对象之封装
一.面向对象 1.面向过程和面向对象 面向过程 分析问题的步骤,然后逐步实现. 面向对象 (1)面向对象基于面向过程 (2)面向对象是找出解决问题的人,然后分配职责. (3)对象中包含解决问题的步骤 ...
- jquery 扫码实现入库登记管理
刚才跟一位分享的大神讲了关于项目中的需求,因为是第一次做这样的需求,还是蒙蒙的,现在把设计图给大家看看,大家有没有做过这样的需求业务,请多多指教!
- Codeforces1138-A(D题)Sushi for Two
Arkady invited Anna for a dinner to a sushi restaurant. The restaurant is a bit unusual: it offers n ...
- day09作业01用户登录与验证
import timeLoginTime = time.asctime( time.localtime(time.time()) )print ("time %s" % Login ...
- python3.x 基础三:set集合
集合,set(),记住: 1个特点:去重,把列表变成集合,达到自动去重操作,无序 5个关系:测试两个列表的交差并子反向差集 方法: | add(...) 常用,已存在元素去重不生效 | A ...
- jquery 滚轴滚动 导航定位和锚点定位
自己写的,只测试了ie9+, firefox,chrome 以下js更好 var fixbar={ init:function(){ "use strict"; // 滚轴 导航位 ...
- PAT-1059 Prime Factors (素数因子)
1059. Prime Factors Given any positive integer N, you are supposed to find all of its prime factors, ...