Recursion - Python

I made the following code in Python that opens the following text file. The first row is the size of the maze file, the second row is the starting point, although it is in row 9, column 1 on the maze, and the third row is the end point, although it is actually in row 1, column 19 in the maze.

5  10
1  1
5  10
---------------------
|             | |   |
|-+-+-+ +-+-+ + + +-|
|   |   |   | |     |
| +-+-+ + +-+-+-+ + |
|       | | |     | |
|-+-+ + + + +-+ +-+-|
|     |             |
|-+ +-+-+-+-+-+ +-+ |
|     |         |   |
---------------------

Most functions work very well and do what they should do. I'm having trouble solving the maze. The way my maze_solution function is set up right now fills the entire maze with asterisks, I only need a path to be drawn with asterisks, and I'm not sure how to do this.

class Maze: 
     def __init__(self, file):
         self.file = file
         self.maze = []
         data = self.file.readlines()

         data.pop(0)
         data.pop(0)        
         data.pop(0)

         for line in data:
             if line[-1] == '\n':
                 self.maze.append(list(line[:-1]))  
             else:
                 self.maze.append(list(line))            

    def print_maze(self):
        for i in range(len(self.maze)):
            for j in range(len(self.maze[0])):  
                print(self.maze[i][j], end='')                         
            print()        

    def maze_points(self):   
        for item in self.maze:
            rows = len(self.maze)
            columns = len(item)

        self.start = (rows - 2, 1)
        self.end = (1, columns - 2)

    def maze_solution(self, row, col):
        if row == self.end[0] and col == self.end[1]:
            self.maze[row][col] = '*'
            return True

         elif self.maze[row][col] == ' ':
             self.maze[row][col] = '*'

             if self.maze_solution(row, col + 1) or \
                self.maze_solution(row + 1, col) or \ 
                self.maze_solution(row, col - 1) or \
                self.maze_solution(row - 1, col):   
                self.maze[row][col] = ' '
                return False
             return True            

        elif self.maze[row][col] in '-|+':
            return False       

def maze_file():
    file = open('maze.txt', 'r')
    maze = Maze(file)
    maze.maze_points()
    row = maze.start[0]
    col = maze.start[1]              
    maze.maze_solution(row, col) 
    maze.print_maze()
+6
source share
2 answers

You are almost there!

, :

def maze_solution(self, row, col, path):
    # Arrived?
    if (row, col) == (self.end[0], self.end[1]):
        return path

    # Wall or been here before?
    if (self.maze[row][col] != " " or (row, col) in path[:-1]):
        return

    for nrow, ncol in [(row, col+1), (row, col-1),
                       (row+1, col), (row-1, col)]:
        sol = self.maze_solution(nrow, ncol, path + [(nrow, ncol)])
        if sol is not None:
            return sol

, , . :

def print_maze(self, path):
    for i in range(len(self.maze)):
        for j in range(len(self.maze[0])):
            print("*" if (i,j) in path else self.maze[i][j], end='')
        print()

:

---------------------
|             | |***|
|-+-+-+ +-+-+ + +*+-|
|   |   |   | |  *  |
| +-+-+ + +-+-+-+*+ |
|    ***| | |  ***| |
|-+-+*+*+ + +-+*+-+-|
|  ***|*********    |
|-+*+-+-+-+-+-+ +-+ |
|***  |         |   |
---------------------
+1

:

  • () =
  • *= ,
  • .= , ,

, True False .

. , , :

def maze_solution(self, row, col):
    if row == self.end[0] and col == self.end[1]:
        self.maze[row][col] = '*'
        return True

    elif self.maze[row][col] == ' ': 
        self.maze[row][col] = '.' # New status: "trying"

        if self.maze_solution(row, col + 1) or \
            self.maze_solution(row + 1, col) or \
            self.maze_solution(row, col - 1) or \
            self.maze_solution(row - 1, col):   
            self.maze[row][col] = '*'
            return True # Indicate success (not failure)
        return False # Indicate failure (not success)

    elif self.maze[row][col] in '-|+':
        return False       

:

---------------------
|             |.|***|
|-+-+-+ +-+-+ +.+*+-|
|   |   |   | |..*..|
| +-+-+ + +-+-+-+*+.|
|    ***| | |  ***|.|
|-+-+*+*+ + +-+*+-+-|
|  ***|*********....|
|-+*+-+-+-+-+-+.+-+.|
|***..|.........|...|
---------------------

, , , .

+1

All Articles