Search and backtracking · 14 / 15 Exercise Hard

Escape the maze

Objective

Create a Python program that finds a path from the entrance to the exit of a maze with backtracking. The path you find depends on the order in which you try the directions, so it is fixed.

Instructions

  • Read an integer n (1 to 8) and then n rows of text, all of the same length: S is the entrance (there is one), E the exit (there is one), . a free path and # a wall.
  • Write a recursive function that, from a cell, returns the text with the moves to the exit, or None if there is no path. Mark each cell as visited on entering (and do not unmark it).
  • Always try the directions in this order: D (down), R (right), U (up), L (left). Return the move's letter followed by the rest of the path.
  • Show Path: DDRR and Length: 4, or No path.
Input
3
S.#
.#.
..E
Output
Path: DDRR
Length: 4

Open the editor in full screen: the statement on the left and your code, the output and the tests on the right.

Start exercise