leet-code/src/main/java/leetcode/editor/cn/EscapeTheGhosts.java
huangge1199@hotmail.com aed899de13 789:逃脱阻碍者
2021-08-22 22:30:39 +08:00

92 lines
2.7 KiB
Java
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

//你在进行一个简化版的吃豆人游戏。你从 [0, 0] 点开始出发,你的目的地是 target = [xtarget, ytarget] 。地图上有一些阻碍者,
//以数组 ghosts 给出,第 i 个阻碍者从 ghosts[i] = [xi, yi] 出发。所有输入均为 整数坐标 。
//
// 每一回合,你和阻碍者们可以同时向东,西,南,北四个方向移动,每次可以移动到距离原位置 1 个单位 的新位置。当然,也可以选择 不动 。所有动作 同时 发生
//。
//
// 如果你可以在任何阻碍者抓住你 之前 到达目的地(阻碍者可以采取任意行动方式),则被视为逃脱成功。如果你和阻碍者同时到达了一个位置(包括目的地)都不算是逃脱
//成功。
//
// 只有在你有可能成功逃脱时,输出 true ;否则,输出 false 。
//
//
// 示例 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
//解释:阻碍者可以和你同时达到目的地。
//
//
// 示例 4
//
//
//输入ghosts = [[5,0],[-10,-2],[0,-5],[-2,-2],[-7,1]], target = [7,7]
//输出false
//
//
// 示例 5
//
//
//输入ghosts = [[-1,0],[0,1],[-1,0],[0,1],[-1,0]], target = [0,0]
//输出true
//
//
//
//
// 提示:
//
//
// 1 <= ghosts.length <= 100
// ghosts[i].length == 2
// -10⁴ <= xi, yi <= 10⁴
// 同一位置可能有 多个阻碍者 。
// target.length == 2
// -10⁴ <= xtarget, ytarget <= 10⁴
//
// Related Topics 数组 数学 👍 76 👎 0
package leetcode.editor.cn;
//789:逃脱阻碍者
class EscapeTheGhosts {
public static void main(String[] args) {
//测试代码
Solution solution = new EscapeTheGhosts().new Solution();
}
//力扣代码
//leetcode submit region begin(Prohibit modification and deletion)
/**
* 789:逃脱阻碍者
*/
class Solution {
public boolean escapeGhosts(int[][] ghosts, int[] target) {
int min = Integer.MAX_VALUE;
for (int[] ghost : ghosts) {
int sum = Math.abs(ghost[0] - target[0]) + Math.abs(ghost[1] - target[1]);
min = Math.min(min, sum);
}
return Math.abs(target[0]) + Math.abs(target[1]) < min;
}
}
//leetcode submit region end(Prohibit modification and deletion)
}