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
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
💡
Explanation
AI Ready
💡 Suggestion
Tab
to accept
Esc
to dismiss
// Output will appear here after running code