bugfree Icon
interview-course
interview-course
interview-course
interview-course
interview-course
interview-course
interview-course
interview-course

Leetcode Problem 499. The Maze III

499. The Maze III

Leetcode Solutions

Dijkstra's Algorithm for Shortest Path in a Maze

  1. Create a priority queue to store nodes with their distance and path from the ball's starting position.
  2. Initialize the priority queue with the starting position of the ball, distance 0, and an empty path.
  3. While the priority queue is not empty, extract the node with the minimum distance.
  4. If the node is the hole, return the path.
  5. If the node has been visited, skip it.
  6. Otherwise, mark the node as visited.
  7. For each direction (up, down, left, right), roll the ball until it hits a wall or the hole, calculating the distance and updating the path.
  8. Add the resulting position, distance, and path to the priority queue if it hasn't been visited.
  9. If the hole is never reached, return 'impossible'.
UML Thumbnail

Breadth-First Search (BFS) for Shortest Path in a Maze

Ask Question

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

Suggested Answer

Answer
Full Screen
Copy Answer Code
Loading...
bugfree Icon
OR