@@ -9,8 +9,9 @@ def solve_maze(maze: list[list[int]]) -> bool:
9
9
Returns:
10
10
Return: True if the maze has a solution or False if it does not.
11
11
Description:
12
- This method navigates through a maze represented as an n by n matrix, starting from a specified source cell
13
- (default: top-left corner) and aiming to reach a destination cell (default: bottom-right corner).
12
+ This method navigates through a maze represented as an n by n matrix,
13
+ starting from a specified source cell (default: top-left corner) and
14
+ aiming to reach a destination cell (default: bottom-right corner).
14
15
The maze consists of walls (0s) and open paths (1s).
15
16
By providing custom row and column values, the source and destination cells can be adjusted.
16
17
>>> maze = [[0, 1, 0, 1, 1],
@@ -27,7 +28,8 @@ def solve_maze(maze: list[list[int]]) -> bool:
27
28
True
28
29
29
30
Note:
30
- In the output maze, the ones (1s) represent one of the possible paths from the source to the destination.
31
+ In the output maze, the ones (1s) represent one of the possible
32
+ paths from the source to the destination.
31
33
32
34
>>> maze = [[0, 1, 0, 1, 1],
33
35
... [0, 0, 0, 0, 0],
@@ -71,15 +73,17 @@ def solve_maze(maze: list[list[int]]) -> bool:
71
73
destination_row = size - 1
72
74
destination_column = size - 1
73
75
solutions = [[0 for _ in range (size )] for _ in range (size )]
74
- solved = run_maze (maze , source_row , source_column ,destination_row ,destination_column , solutions )
76
+ solved = run_maze (maze , source_row , source_column ,destination_row ,
77
+ destination_column , solutions )
75
78
if solved :
76
79
print ("\n " .join (str (row ) for row in solutions ))
77
80
else :
78
81
print ("No solution exists!" )
79
82
return solved
80
83
81
84
82
- def run_maze (maze : list [list [int ]], i : int , j : int ,destination_row :int ,destination_column :int , solutions : list [list [int ]]) -> bool :
85
+ def run_maze (maze : list [list [int ]], i : int , j : int ,destination_row :int ,
86
+ destination_column :int , solutions : list [list [int ]]) -> bool :
83
87
"""
84
88
This method is recursive starting from (i, j) and going in one of four directions:
85
89
up, down, left, right.
@@ -109,10 +113,14 @@ def run_maze(maze: list[list[int]], i: int, j: int,destination_row:int,destinati
109
113
110
114
# check for directions
111
115
if (
112
- run_maze (maze , i + 1 , j ,destination_row ,destination_column , solutions )
113
- or run_maze (maze , i , j + 1 ,destination_row ,destination_column , solutions )
114
- or run_maze (maze , i - 1 , j ,destination_row ,destination_column , solutions )
115
- or run_maze (maze , i , j - 1 ,destination_row ,destination_column , solutions )
116
+ run_maze (maze , i + 1 , j ,destination_row ,
117
+ destination_column , solutions )
118
+ or run_maze (maze , i , j + 1 ,destination_row ,
119
+ destination_column , solutions )
120
+ or run_maze (maze , i - 1 , j ,destination_row ,
121
+ destination_column , solutions )
122
+ or run_maze (maze , i , j - 1 ,destination_row ,
123
+ destination_column , solutions )
116
124
):
117
125
return True
118
126
0 commit comments