Leetcode Problem 2814. Minimum Time Takes to Reach Destination Without Drowning

2814. Minimum Time Takes to Reach Destination Without Drowning

Leetcode Solutions

BFS + Dijkstra

  1. Initialize a matrix d to store the time each cell gets flooded, with a default value of -1 for all cells.
  2. Use a queue q to perform BFS on the flooded cells, marking their flooding time in d.
  3. After flooding simulation, use a priority queue pq to perform a modified Dijkstra's algorithm starting from the cell containing 'S'.
  4. In each step of Dijkstra's, check if the current cell is the destination. If so, return the current time.
  5. If not, mark the cell as visited and add its neighbors to the priority queue if they are not stone cells, not yet visited, and not flooded or will not be flooded at the current time.
  6. If the destination is not reached and the queue is empty, return -1 to indicate the destination is unreachable.
UML Thumbnail

BFS for Flooding and BFS for Path Finding

Ask Question

Programming Language
image/screenshot of info(optional)
Full Screen
Loading...

Suggested Answer

Answer
Full Screen
Copy Answer Code
Loading...