解法一:曼哈顿距离
参考官方题解对于曼哈顿距离在本题中使用的论证:https://leetcode-cn.com/problems/escape-the-ghosts/solution/tao-tuo-zu-ai-zhe-by-leetcode/
class Solution {
public boolean escapeGhosts(int[][] ghosts, int[] target) {
int dis = distance(new int[]{0, 0}, target);
for (int[] i : ghosts) {
if (distance(i, target) <= dis) {
return false;
}
}
return true;
}
/**
* 计算两点间曼哈顿距离
*
* @param x 点x
* @param y 点y
* @return 点x与点y之间的曼哈顿距离
*/
private int distance(int[] x, int[] y) {
return Math.abs(x[0] - y[0]) + Math.abs(x[1] - y[1]);
}
}