Skip to main content
Back to problems
#2849
Medium Algorithms

Determine if a cell is reachable at a given time

Math
37.2% acceptance
Feb 25, 2026
839
770
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.

Solution

Rust
Time O(1)
Space O(1)
LeetCode
solution.rs
impl Solution {
  pub fn is_reachable_at_time(sx: i32, sy: i32, fx: i32, fy: i32, t: i32) -> bool {
    let dx = (sx - fx).abs();
    let dy = (sy - fy).abs();
    if dx == 0 && dy == 0 { return t != 1; }
    let min_time = dx.max(dy);
    t >= min_time
  }
}