Leetcode Problem 490. The Maze

490. The Maze

Leetcode Solutions

Depth First Search (DFS) Approach

Algorithm

  1. Initialize a 2D array visited with the same dimensions as the maze to keep track of visited cells.
  2. Define the dfs function that takes the current position and the maze as arguments.
  3. If the current position is the destination, return true.
  4. If the current position is already visited, return false.
  5. Mark the current position as visited.
  6. Explore all four possible directions from the current position until hitting a wall.
  7. For each direction, recursively call the dfs function with the new position (just before the wall).
  8. If any recursive call returns true, propagate this result back up, indicating a successful path.
  9. If all directions are explored and none lead to the destination, return false.
UML Thumbnail

Breadth First Search (BFS) Approach

Ask Question

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

Suggested Answer

Answer
Full Screen
Copy Answer Code
Loading...