This README provides a detailed, step-by-step explanation of how the solution for the "Highest Peak" problem works across multiple languages: C++, Java, JavaScript, Python, and Go. Each explanation dives into the logic while keeping the explanation language-agnostic.
-
Input Grid Initialization
- We are given a grid where cells are either
1
(water) or0
(land). - The goal is to determine the height of each cell such that:
- Water cells have a height of
0
. - Land cells have heights calculated based on their distance from the nearest water cell.
- Water cells have a height of
- We are given a grid where cells are either
-
Mark Water Cells
- Traverse the grid and identify all cells marked as water (
1
). - These cells are immediately set to a height of
0
. - Land cells are marked as "unvisited" or assigned a default value (e.g.,
-1
orInfinity
).
- Traverse the grid and identify all cells marked as water (
-
Breadth-First Search (BFS) Initialization
- Use a queue (or similar data structure) to perform a level-wise traversal (BFS).
- Initially, add all water cells into the queue as the starting points.
-
BFS Traversal to Calculate Heights
- Process each cell in the queue:
- For each cell, check all its neighbors (up, down, left, right).
- If the neighbor is unvisited (land), calculate its height as
current_cell_height + 1
. - Add the neighbor to the queue for further exploration.
- Process each cell in the queue:
-
Continue Until All Cells Are Processed
- BFS ensures that heights are calculated layer by layer, starting from the water cells.
- This guarantees that each cell's height is the shortest distance from the nearest water cell.
-
Return the Resulting Grid
- Once BFS completes, the updated grid contains the required heights for all cells.
-
Define the Input and Output Grid
- Use a 2D vector to represent the grid.
-
Initialize the Queue
- Use a queue to store coordinates of all water cells (
{x, y}
).
- Use a queue to store coordinates of all water cells (
-
BFS Traversal
- Iterate over all four possible directions (up, down, left, right) using a directions array.
- For each unvisited neighbor, update its height and push it into the queue.
-
Complete the Grid Update
- Continue the BFS until the queue is empty.
- The final grid contains heights for all cells.
-
Prepare the Input Grid
- Use a 2D array for the grid and a
Queue
to store coordinates of water cells.
- Use a 2D array for the grid and a
-
Add Water Cells to the Queue
- Iterate through the grid, marking water cells as
0
and land cells as-1
.
- Iterate through the grid, marking water cells as
-
Perform BFS Traversal
- Use a directions array to move in all four cardinal directions.
- For each neighbor, calculate the height (
current_height + 1
) and enqueue it.
-
Return the Updated Grid
- BFS ensures that all cells are visited in the shortest possible distance order.
-
Grid Initialization
- Represent the grid as a 2D array and use a
queue
to track BFS levels.
- Represent the grid as a 2D array and use a
-
Identify Water Cells
- Mark all water cells (
1
) with height0
and add them to the queue. - Land cells are initially marked as unvisited (
-1
).
- Mark all water cells (
-
BFS Traversal
- For each cell, explore its neighbors using predefined directions.
- Update the neighbor's height if it is unvisited, then add it to the queue.
-
Update the Grid
- Continue processing until all cells are visited and their heights are calculated.
-
Input Grid and Initialization
- Use a 2D list to represent the grid.
- Initialize a
deque
with all water cells and mark land cells as unvisited (-1
).
-
BFS Setup
- Store all water cells in the deque with height
0
. - Use directions to move up, down, left, and right during traversal.
- Store all water cells in the deque with height
-
Calculate Heights Using BFS
- For each cell, check all its neighbors.
- If the neighbor is unvisited, assign its height (
current_height + 1
) and add it to the deque.
-
Return the Result
- The final grid contains calculated heights for all cells.
-
Input Grid Representation
- Use a 2D slice to represent the grid.
-
Initialize the Queue
- Create a queue and add all water cells as starting points.
-
BFS Traversal
- Use a directions array to explore all neighbors of each cell.
- Update the height of each unvisited neighbor and push it to the queue.
-
Update and Return the Grid
- Continue BFS until all cells are processed.
- The resulting grid contains the required heights.
-
Time Complexity:
$$O(n \times m)$$ , where (n) and (m) are the dimensions of the grid. Each cell is processed once. -
Space Complexity:
$$O(n \times m)$$ for the BFS queue and auxiliary storage.
This problem emphasizes the importance of BFS for multi-source shortest path problems. The key takeaway is that BFS ensures layer-wise traversal, making it ideal for calculating distances in grid-based problems.