Thanksgiving Sale: Use Coupon Code THANKS25 to Get Extra 25% Off.
00DAYS
:
00HOURS
:
00MINUTES
:
00SECONDS
Leetcode Problem 505. The Maze II
505. The Maze II
Leetcode Solutions
Approach # Using Dijkstra Algorithm and Priority Queue
Initialize a 2D array distance with infinity values, except for the start position which is set to 0.
Create a priority queue and add the start position along with its distance (0).
While the priority queue is not empty:
a. Extract the node with the minimum distance from the queue.
b. If this node is the destination, return the distance.
c. If this node has been visited, continue to the next iteration.
d. Mark the node as visited.
e. Explore all possible directions (up, down, left, right) from this node until hitting a wall.
f. For each new position reached, if the distance to this new position is less than the current distance, update it and add the new position to the queue.