← Back to Graphs Part 1

Maze Path Finder

7 of 9

Exercise 6: Maze Path Finder

Your Task

Write a function has_path(maze, row, col, visited) that:

  • Returns True if there is a path from (row, col) to the bottom-right corner
  • Returns False otherwise
  • 0 = wall, 1 = open path
  • You can move up, down, left, right — not diagonal

Start by calling has_path(maze, 0, 0, set()).


Examples

maze1 = [ [1, 0, 1], [1, 1, 1], [0, 0, 1] ] print(has_path(maze1, 0, 0, set())) # True # Path: (0,0) → (1,0) → (1,1) → (1,2) → (2,2) maze2 = [ [1, 0, 1], [0, 0, 1], [0, 0, 1] ] print(has_path(maze2, 0, 0, set())) # False # (0,0) is surrounded — no way through

Hints

💡 Hint 1

Base cases:

  • Out of bounds → False
  • Wall (maze[row][col] == 0) → False
  • Already visited → False
  • Reached bottom-right → True
💡 Hint 2

For the recursive case, try all 4 directions. Return True if any direction leads to a path — use or:

return (has_path(...up...) or has_path(...down...) or ...)
✅ Solution
def has_path(maze, row, col, visited): rows, cols = len(maze), len(maze[0]) # Base cases if row < 0 or row >= rows or col < 0 or col >= cols: return False if maze[row][col] == 0: return False if (row, col) in visited: return False if row == rows - 1 and col == cols - 1: return True visited.add((row, col)) return (has_path(maze, row - 1, col, visited) or has_path(maze, row + 1, col, visited) or has_path(maze, row, col - 1, visited) or has_path(maze, row, col + 1, visited))

Try it yourself

Code: Maze Path Finder

Loading Python runtime…
Python
Loading...
bash
$ Click "Run" to execute your code...