Determine if a Cell Is Reachable at a Given Time - Problem

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

In an infinite 2D grid, you start at the cell (sx, sy). Each second, you must move to any of its adjacent cells.

Return true if you can reach cell (fx, fy) after exactly t seconds, or false otherwise.

A cell's adjacent cells are the 8 cells around it that share at least one corner with it. You can visit the same cell several times.

Input & Output

Example 1 — Cannot Reach in Time
$ Input: sx = 2, sy = 1, fx = 4, fy = 3, t = 3
Output: false
💡 Note: Minimum distance is max(|4-2|, |3-1|) = max(2,2) = 2. We need exactly 3 seconds, but after reaching in 2 seconds, we have 1 second left (odd). We can't waste exactly 1 second with back-and-forth movement.
Example 2 — Perfect Timing
$ Input: sx = 1, sy = 1, fx = 1, fy = 1, t = 3
Output: true
💡 Note: Start and target are the same. Since t=3 is odd and we need to move each second, we can move to an adjacent cell and return (takes 2 seconds), then move away and back again (1 more second).
Example 3 — Insufficient Time
$ Input: sx = 1, sy = 1, fx = 5, fy = 5, t = 3
Output: false
💡 Note: Minimum distance is max(|5-1|, |5-1|) = max(4,4) = 4 seconds. We only have 3 seconds available, so it's impossible to reach the target.

Constraints

  • 1 ≤ sx, sy, fx, fy ≤ 109
  • 0 ≤ t ≤ 109

Visualization

Tap to expand
Grid Movement: Start (2,1) → Target (4,3) in t=3 seconds2D Grid (8-directional movement)S(2,1)T(4,3)DiagonalMathematical Analysisdx = |4-2| = 2, dy = |3-1| = 2min_distance = max(2,2) = 2remaining_time = 3-2 = 1 (odd)Cannot waste 1 second!Result: false (impossible timing)
Understanding the Visualization
1
Input
Start position, target position, and exact time limit
2
Calculate
Find minimum distance using Chebyshev metric
3
Verify
Check if timing constraints can be satisfied
Key Takeaway
🎯 Key Insight: Chebyshev distance gives minimum time, remaining time must be even for back-and-forth movement
Asked in
Google 28 Microsoft 22 Apple 15 Meta 12
23.5K Views
Medium Frequency
~15 min Avg. Time
847 Likes
Ln 1, Col 1
Smart Actions
💡 Explanation
AI Ready
💡 Suggestion Tab to accept Esc to dismiss
// Output will appear here after running code
Code Editor Closed
Click the red button to reopen