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

You're navigating through an infinite 2D grid where you can move like a king in chess - in any of the 8 directions (horizontally, vertically, or diagonally) to adjacent cells.

Given your starting position (sx, sy) and target position (fx, fy), determine if you can reach the target in exactly t seconds.

Here's the catch: You must move every second - no standing still! However, you can revisit cells multiple times, allowing you to "waste" time by moving back and forth.

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

Input & Output

example_1.py โ€” Basic Movement
$ Input: sx = 2, sy = 4, fx = 7, fy = 7, t = 6
โ€บ Output: true
๐Ÿ’ก Note: Min distance = max(|7-2|, |7-4|) = max(5,3) = 5. Since t=6 โ‰ฅ 5 and (6-5)=1 is odd, but we can reach in 5 and waste 1 step by going back and forth... Wait, that's wrong. Actually (6-5)%2=1โ‰ 0, so answer should be false. Let me recalculate: we need exactly 6 steps, min is 5, extra is 1 (odd), so false.
example_2.py โ€” Same Position Edge Case
$ Input: sx = 1, sy = 1, fx = 1, fy = 1, t = 1
โ€บ Output: false
๐Ÿ’ก Note: Already at target but must move exactly 1 step. Cannot return to same position in 1 move (need 2 moves minimum for round trip).
example_3.py โ€” Perfect Timing
$ Input: sx = 1, sy = 1, fx = 3, fy = 3, t = 3
โ€บ Output: false
๐Ÿ’ก Note: Min distance = max(|3-1|, |3-1|) = max(2,2) = 2. We have t=3, so extra time = 3-2 = 1 (odd). Cannot waste exactly 1 step and return to target.

Visualization

Tap to expand
STARTTARGETSolution StepsCalculate min_time = max(|dx|, |dy|)Check if t โ‰ฅ min_timeVerify (t - min_time) % 2 == 0Handle special case: same pos, t=1Why Even Extra Time?โ€ข Move away from target: 1 stepโ€ข Move back to target: 1 stepโ€ข Total for round trip: 2 steps (even)โ€ข Odd extra time โ†’ can't return!๐ŸŽฏ Key Insight: Diagonal movement + even time wasting
Understanding the Visualization
1
Calculate Minimum Path
King moves diagonally when possible - distance is max(dx, dy)
2
Reach Target Optimally
Take shortest diagonal path to destination
3
Waste Extra Time
Move back and forth between adjacent cells
4
Return to Target
End exactly at target after wasting even number of moves
Key Takeaway
๐ŸŽฏ Key Insight: The elegant mathematical solution recognizes that diagonal movement minimizes distance, and any extra time must be even to allow back-and-forth movement while ending at the target.

Time & Space Complexity

Time Complexity
โฑ๏ธ
O(8^t)

8 possible moves at each of t time steps creates exponential explosion

n
2n
โœ“ Linear Growth
Space Complexity
O(t)

Recursion depth limited by time t, but exponential paths explored

n
2n
โœ“ Linear Space

Constraints

  • 1 โ‰ค sx, sy, fx, fy โ‰ค 109
  • 0 โ‰ค t โ‰ค 109
  • Grid is infinite - no boundary constraints
  • Must move every second - no staying in place
Asked in
Google 24 Microsoft 18 Meta 15 Amazon 12
28.6K Views
Medium Frequency
~15 min Avg. Time
892 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