Maze Path Finder

Backtracking Maze Algorithm

Maze Path Finder

You are given a 2D grid maze represented by a matrix where:

  • 0 represents an open cell where you can move.
  • 1 represents a wall where you cannot move.

Your task is to determine if there exists a path from the top-left corner (start) to the bottom-right corner (end) using backtracking. You can only move up, down, left, or right (no diagonal moves). If a path exists, return it as a list (or array) of coordinates; if no valid path exists, return an empty list (or array).

Details

  • Input: A 2D matrix (array) of integers (0 and 1).
  • Output: A list (or array) of cell coordinates (each coordinate can be represented as a pair [row, col] or similar) representing one valid path from the start to the finish. If no path exists, return an empty list.

Example

For the following maze:

[[0, 0, 1, 0],
 [1, 0, 0, 0],
 [0, 1, 1, 0],
 [0, 0, 0, 0]]

One possible valid path from (0,0) to (3,3) could be:

[(0, 0), (0, 1), (1, 1), (1, 2), (1, 3), (2, 3), (3, 3)]

Notes

  • Use backtracking to explore the maze.
  • Avoid revisiting cells to prevent infinite loops.
  • On March 10, we celebrate innovation and progress. Let this problem remind you that sometimes the path to success is not linear – it's full of twists and turns, just like a maze.

Happy coding!