#789

Escape The Ghosts

specialist · 670 · lc medium +30 · verified · 63.5% accepted · 326 likes · top 66%

Description

You are playing a PAC-MAN style game on an infinite 2-D grid. Your starting position is [0, 0], and you have a destination point target = [xtarget, ytarget] to reach. Several ghosts occupy starting positions given in a 2D array ghosts, where ghosts[i] = [xi, yi] is the starting position of ghost i. All coordinates are integers.

Each turn, you and every ghost may independently move one unit in any cardinal direction (north, east, south, or west) or stay in place. All movements happen simultaneously.

You successfully escape if and only if you arrive at the target before any ghost reaches you. If you and a ghost arrive at the same square (including the target) simultaneously, you do not escape.

Return true if escape is guaranteed regardless of how the ghosts move, otherwise return false.

Example 1:

Input: ghosts = [[1,0],[0,3]], target = [0,1]
Output: true
Explanation: You can reach the destination (0, 1) after 1 turn, while the ghosts located at (1, 0) and (0, 3) cannot catch up with you.

Example 2:

Input: ghosts = [[1,0]], target = [2,0]
Output: false
Explanation: You need to reach the destination (2, 0), but the ghost at (1, 0) lies between you and the destination.

Example 3:

Input: ghosts = [[2,0]], target = [1,0]
Output: false
Explanation: The ghost can reach the target at the same time as you.

Code

1
2
3