• 789. Escape from obstructors

Ghosts [][] prints false if the escapee arrives at target[] no later than [0,0], or true if it does not.

My first thought is to compare the distance s from target[] of every item in ghosts[][] to s2 from [0,0] to target[]. It is possible to create a circle with target[] as the center and s2 as the radius. Print false if the circle contains points t belonging to ghosts[][], and true otherwise.

After thinking about it, the idea is crazy, the implementation is too complicated. (Pythagorean theorem is also more complicated to figure out the distance of a straight line.)

There is a Manhattan distance between two points found in the data search, Manhattan distance can be quickly calculated and compared, the following is the implementation code.

var escapeGhosts = function(ghosts, target) {
    var md = Math.abs(target[0]) + Math.abs(target[1]) // math.abs (
    for (let ghost of ghosts) {
        let md2 = Math.abs(ghost[0] - target[0]) + Math.abs(ghost[1] - target[1])
        if (md2 <= md) {
            return false}}return true
}
Copy the code