← 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

Try it yourself

Code: Maze Path Finder

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