解法一:曼哈顿距离

参考官方题解对于曼哈顿距离在本题中使用的论证:https://leetcode-cn.com/problems/escape-the-ghosts/solution/tao-tuo-zu-ai-zhe-by-leetcode/

  1. class Solution {
  2. public boolean escapeGhosts(int[][] ghosts, int[] target) {
  3. int dis = distance(new int[]{0, 0}, target);
  4. for (int[] i : ghosts) {
  5. if (distance(i, target) <= dis) {
  6. return false;
  7. }
  8. }
  9. return true;
  10. }
  11. /**
  12. * 计算两点间曼哈顿距离
  13. *
  14. * @param x 点x
  15. * @param y 点y
  16. * @return 点x与点y之间的曼哈顿距离
  17. */
  18. private int distance(int[] x, int[] y) {
  19. return Math.abs(x[0] - y[0]) + Math.abs(x[1] - y[1]);
  20. }
  21. }