Leetcode Problem 1162. As Far from Land as Possible
1162. As Far from Land as Possible
Leetcode Solutions
Breadth-First Search (BFS)
Initialize a queue and add all land cell coordinates to it.
Initialize a variable maxDistance to -1 to keep track of the maximum distance.
While the queue is not empty, process each cell in the queue:
a. Remove the cell from the queue.
b. Check all four adjacent cells (up, down, left, right).
c. If an adjacent cell is water and has not been visited, mark it as visited, update its distance, and add it to the queue.
After processing all cells at the current distance, increment the distance counter.
Continue the BFS until the queue is empty.
Return maxDistance if it's greater than 0, otherwise return -1 if no water cell is found.