@@ -10,10 +10,11 @@ def solve_maze(maze: list[list[int]]) -> bool:
10
10
Return: True if the maze has a solution or False if it does not.
11
11
Description:
12
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
13
+ starting from a specified source cell (default: top-left corner) and
14
14
aiming to reach a destination cell (default: bottom-right corner).
15
15
The maze consists of walls (0s) and open paths (1s).
16
- By providing custom row and column values, the source and destination cells can be adjusted.
16
+ By providing custom row and column values, the source and destination
17
+ cells can be adjusted.
17
18
>>> maze = [[0, 1, 0, 1, 1],
18
19
... [0, 0, 0, 0, 0],
19
20
... [1, 0, 1, 0, 1],
@@ -68,22 +69,29 @@ def solve_maze(maze: list[list[int]]) -> bool:
68
69
"""
69
70
# We need to create solution object to save path.
70
71
size = len (maze )
71
- source_row = 0
72
- source_column = 0
73
- destination_row = size - 1
74
- destination_column = size - 1
72
+ source_row = 0
73
+ source_column = 0
74
+ destination_row = size - 1
75
+ destination_column = size - 1
75
76
solutions = [[0 for _ in range (size )] for _ in range (size )]
76
- solved = run_maze (maze , source_row , source_column ,destination_row ,
77
- destination_column , solutions )
77
+ solved = run_maze (
78
+ maze , source_row , source_column , destination_row , destination_column , solutions
79
+ )
78
80
if solved :
79
81
print ("\n " .join (str (row ) for row in solutions ))
80
82
else :
81
83
print ("No solution exists!" )
82
84
return solved
83
85
84
86
85
- def run_maze (maze : list [list [int ]], i : int , j : int ,destination_row :int ,
86
- destination_column :int , solutions : list [list [int ]]) -> bool :
87
+ def run_maze (
88
+ maze : list [list [int ]],
89
+ i : int ,
90
+ j : int ,
91
+ destination_row : int ,
92
+ destination_column : int ,
93
+ solutions : list [list [int ]],
94
+ ) -> bool :
87
95
"""
88
96
This method is recursive starting from (i, j) and going in one of four directions:
89
97
up, down, left, right.
@@ -97,7 +105,7 @@ def run_maze(maze: list[list[int]], i: int, j: int,destination_row:int,
97
105
"""
98
106
size = len (maze )
99
107
# Final check point.
100
- if i == destination_row and j == destination_column :
108
+ if i == destination_row and j == destination_column :
101
109
solutions [i ][j ] = 1
102
110
return True
103
111
@@ -113,14 +121,16 @@ def run_maze(maze: list[list[int]], i: int, j: int,destination_row:int,
113
121
114
122
# check for directions
115
123
if (
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 )
124
+ run_maze (maze , i + 1 , j , destination_row , destination_column , solutions )
125
+ or run_maze (
126
+ maze , i , j + 1 , destination_row , destination_column , solutions
127
+ )
128
+ or run_maze (
129
+ maze , i - 1 , j , destination_row , destination_column , solutions
130
+ )
131
+ or run_maze (
132
+ maze , i , j - 1 , destination_row , destination_column , solutions
133
+ )
124
134
):
125
135
return True
126
136
@@ -129,7 +139,6 @@ def run_maze(maze: list[list[int]], i: int, j: int,destination_row:int,
129
139
return False
130
140
131
141
132
-
133
142
if __name__ == "__main__" :
134
143
import doctest
135
144
0 commit comments