#2849

Determine if a Cell Is Reachable at a Given Time

expert · 1105 · lc medium +32 · verified · 37.2% accepted · 839 likes · top 15%

Description

Four integers sx, sy, fx, fy, and a non-negative integer t are given.

On an infinite 2D grid you begin at (sx, sy). Each second you must move to one of the 8 adjacent cells (cells sharing at least one corner).

Return true if you can reach (fx, fy) in exactly t seconds, or false otherwise. You may revisit cells.

Example 1:

Input: sx = 2, sy = 4, fx = 7, fy = 7, t = 6
Output: true
Explanation: Starting at cell (2, 4), we can reach cell (7, 7) in exactly 6 seconds by going through the cells depicted in the picture above.

Example 2:

Input: sx = 3, sy = 1, fx = 7, fy = 3, t = 3
Output: false
Explanation: Starting at cell (3, 1), it takes at least 4 seconds to reach cell (7, 3) by going through the cells depicted in the picture above. Hence, we cannot reach cell (7, 3) at the third second.

Code

1
2
3