Leetcode Problem 2617. Minimum Number of Visited Cells in a Grid
2617. Minimum Number of Visited Cells in a Grid
Leetcode Solutions
Breadth-First Search (BFS) with Optimization
Initialize a queue to store the cells to be visited, starting with the top-left cell (0, 0).
Keep track of the number of cells visited using a counter.
While the queue is not empty, process the cells in the queue:
a. Increment the counter for each layer of cells processed.
b. For each cell, calculate the maximum reachable cells in both the rightward and downward directions.
c. Add these reachable cells to the queue if they haven't been visited yet.
d. If the bottom-right cell is reached, return the counter as the result.
If the queue is emptied and the bottom-right cell has not been reached, return -1 to indicate no valid path exists.