[LeetCode] Determine if a Cell Is Reachable at a Given Time

2849. Determine if a Cell Is Reachable at a Given Time

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.

1
2
3
4
5
6
7
8
9
10
11
12
13
14

class Solution {
public:
bool isReachableAtTime(int sx, int sy, int fx, int fy, int t) {
int x = abs(sx - fx);
int y = abs(sy - fy);
if(max(x,y) > t) return false;
if(t >= 2) return true;
if(t == 0) return x == 0 and y == 0;
if(t == 1) return max(x,y) == 1;
return true;
}
};

Author: Song Hayoung
Link: https://songhayoung.github.io/2023/09/10/PS/LeetCode/determine-if-a-cell-is-reachable-at-a-given-time/
Copyright Notice: All articles in this blog are licensed under CC BY-NC-SA 4.0 unless stating additionally.