Solution:Minimum Time Takes to Reach Destination Without Drowning

Let’s solve the Minimum Time Takes to Reach Destination Without Drowning problem using the Matrices pattern.

Statement

Given a m x n grid of the string land. It consists of the following types of cells:

  • S: Source cell where you are standing initially.

  • D: Destination cell where you have to reach.

  • .: These cells are empty.

  • X: These cells are stone.

  • *: These cells are flooded.

Each second, you can move to a neighboring cell directly next to your current one. At the same time, any empty cell next to a flooded cell also becomes flooded. There are two challenges in your path:

  1. You can’t step on stone cells.

  2. You can’t step on flooded cells or cells that will flood right when you try to step on them because you’ll drown.

Return the minimum time it takes you to reach the destination from the source in seconds, or 1-1 if no path exists between them.

Note: The destination will never be flooded.

Constraints:

  • 22 \leq m, n 100\leq 100

  • land consists only of S, D, ., * and X.

  • There is only one S and D cells in the land.

Solution

This solution finds the shortest time for source S to reach destination D while avoiding flooded cells, which expand to horizontally and vertically adjacent cells after every second. The flood spreads from cells marked *, and S can only move to empty cells (marked .) or reach the destination. The algorithm operates based on a matrix traversal pattern, navigating through a grid (or matrix) where each cell has different statuses. The solution works by alternating the flood spread and the source movement in turns, where both the flood and the source expand their positions at each step. If S reaches D before being flooded, the algorithm returns the time taken. Otherwise, it returns 1-1 since no valid path is found.

Here’s the step-by-step implementation of the solution:

  • Start by storing the starting position S and all the initial positions of the flood marked as * in the land. Store these positions in two separate queues. The moves will track their movement while the flood will simulate how the flood spreads.

  • Use a breadth-first search (BFS) approach, where both the flood and the player take turns moving. For each second:

    • First, process all the flood cells. The flood spreads to any adjacent empty cells marked as .. For each cell that gets flooded:

      • Mark it * and add it to the flood queue.

    • After processing the flood, move the S to any adjacent empty cell or the destination. If the S reaches D:

      • Return the seconds it took to get there.

  • After the flood and the S have taken turns, increase the seconds by 1 and repeat the process. The S and flood continue expanding until the S either reaches the D or no more valid moves are left.

  • If the S successfully reaches the D, return the seconds taken. If there are no more valid moves for the S and it hasn’t reached D, return 1-1 to indicate that there is no valid path.

Let’s look at the following illustration to get a better understanding of the solution:

Level up your interview prep. Join Educative to access 80+ hands-on prep courses.