4
4
def solve_maze (maze : list [list [int ]]) -> bool :
5
5
"""
6
6
This method solves the "rat in maze" problem.
7
- In this problem we have some n by n matrix, a start point and an end point.
8
- We want to go from the start to the end. In this matrix zeroes represent walls
9
- and ones paths we can use.
10
7
Parameters :
11
8
maze(2D matrix) : maze
12
9
Returns:
13
10
Return: True if the maze has a solution or False if it does not.
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).
14
+ The maze consists of walls (0s) and open paths (1s).
15
+ By providing custom row and column values, the source and destination cells can be adjusted.
14
16
>>> maze = [[0, 1, 0, 1, 1],
15
17
... [0, 0, 0, 0, 0],
16
18
... [1, 0, 1, 0, 1],
@@ -24,6 +26,9 @@ def solve_maze(maze: list[list[int]]) -> bool:
24
26
[0, 0, 0, 0, 1]
25
27
True
26
28
29
+ Note:
30
+ In the output maze, the ones (1s) represent one of the possible paths from the source to the destination.
31
+
27
32
>>> maze = [[0, 1, 0, 1, 1],
28
33
... [0, 0, 0, 0, 0],
29
34
... [0, 0, 0, 0, 1],
@@ -59,18 +64,22 @@ def solve_maze(maze: list[list[int]]) -> bool:
59
64
No solution exists!
60
65
False
61
66
"""
62
- size = len (maze )
63
67
# We need to create solution object to save path.
68
+ size = len (maze )
69
+ source_row = 0
70
+ source_column = 0
71
+ destination_row = size - 1
72
+ destination_column = size - 1
64
73
solutions = [[0 for _ in range (size )] for _ in range (size )]
65
- solved = run_maze (maze , 0 , 0 , solutions )
74
+ solved = run_maze (maze , source_row , source_column , destination_row , destination_column , solutions )
66
75
if solved :
67
76
print ("\n " .join (str (row ) for row in solutions ))
68
77
else :
69
78
print ("No solution exists!" )
70
79
return solved
71
80
72
81
73
- def run_maze (maze : list [list [int ]], i : int , j : int , solutions : list [list [int ]]) -> bool :
82
+ def run_maze (maze : list [list [int ]], i : int , j : int ,destination_row : int , destination_column : int , solutions : list [list [int ]]) -> bool :
74
83
"""
75
84
This method is recursive starting from (i, j) and going in one of four directions:
76
85
up, down, left, right.
@@ -84,7 +93,7 @@ def run_maze(maze: list[list[int]], i: int, j: int, solutions: list[list[int]])
84
93
"""
85
94
size = len (maze )
86
95
# Final check point.
87
- if i == j == ( size - 1 ) :
96
+ if i == destination_row and j == destination_column :
88
97
solutions [i ][j ] = 1
89
98
return True
90
99
@@ -100,10 +109,10 @@ def run_maze(maze: list[list[int]], i: int, j: int, solutions: list[list[int]])
100
109
101
110
# check for directions
102
111
if (
103
- run_maze (maze , i + 1 , j , solutions )
104
- or run_maze (maze , i , j + 1 , solutions )
105
- or run_maze (maze , i - 1 , j , solutions )
106
- or run_maze (maze , i , j - 1 , solutions )
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 )
107
116
):
108
117
return True
109
118
@@ -112,6 +121,7 @@ def run_maze(maze: list[list[int]], i: int, j: int, solutions: list[list[int]])
112
121
return False
113
122
114
123
124
+
115
125
if __name__ == "__main__" :
116
126
import doctest
117
127
0 commit comments