1
1
from __future__ import annotations
2
2
3
3
4
- def solve_maze (maze : list [list [int ]]) -> bool :
4
+ def solve_maze (
5
+ maze : list [list [int ]],
6
+ source_row : int ,
7
+ source_column : int ,
8
+ destination_row : int ,
9
+ destination_column : int ,
10
+ ) -> bool :
5
11
"""
6
12
This method solves the "rat in maze" problem.
7
13
Parameters :
8
- maze(2D matrix) : maze
14
+ - maze(2D matrix) : maze
15
+ - source_row (int): The row index of the starting point.
16
+ - source_column (int): The column index of the starting point.
17
+ - destination_row (int): The row index of the destination point.
18
+ - destination_column (int): The column index of the destination point.
9
19
Returns:
10
20
Return: True if the maze has a solution or False if it does not.
11
21
Description:
12
22
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) .
15
- The maze consists of walls (0s ) and open paths (1s ).
23
+ starting from a specified source cell and
24
+ aiming to reach a destination cell.
25
+ The maze consists of walls (1s ) and open paths (0s ).
16
26
By providing custom row and column values, the source and destination
17
27
cells can be adjusted.
18
28
>>> maze = [[0, 1, 0, 1, 1],
19
29
... [0, 0, 0, 0, 0],
20
30
... [1, 0, 1, 0, 1],
21
31
... [0, 0, 1, 0, 0],
22
32
... [1, 0, 0, 1, 0]]
23
- >>> solve_maze(maze)
33
+ >>> solve_maze(maze,0,0,len(maze)-1,len(maze)-1 )
24
34
[1, 0, 0, 0, 0]
25
35
[1, 1, 1, 1, 0]
26
36
[0, 0, 0, 1, 0]
@@ -37,7 +47,7 @@ def solve_maze(maze: list[list[int]]) -> bool:
37
47
... [0, 0, 0, 0, 1],
38
48
... [0, 0, 0, 0, 0],
39
49
... [0, 0, 0, 0, 0]]
40
- >>> solve_maze(maze)
50
+ >>> solve_maze(maze,0,0,len(maze)-1,len(maze)-1 )
41
51
[1, 0, 0, 0, 0]
42
52
[1, 0, 0, 0, 0]
43
53
[1, 0, 0, 0, 0]
@@ -48,30 +58,75 @@ def solve_maze(maze: list[list[int]]) -> bool:
48
58
>>> maze = [[0, 0, 0],
49
59
... [0, 1, 0],
50
60
... [1, 0, 0]]
51
- >>> solve_maze(maze)
61
+ >>> solve_maze(maze,0,0,len(maze)-1,len(maze)-1 )
52
62
[1, 1, 1]
53
63
[0, 0, 1]
54
64
[0, 0, 1]
55
65
True
56
66
57
- >>> maze = [[0, 1 , 0],
67
+ >>> maze = [[1, 0 , 0],
58
68
... [0, 1, 0],
59
69
... [1, 0, 0]]
60
- >>> solve_maze(maze)
70
+ >>> solve_maze(maze,0,1,len(maze)-1,len(maze)-1)
71
+ [0, 1, 1]
72
+ [0, 0, 1]
73
+ [0, 0, 1]
74
+ True
75
+
76
+ >>> maze = [[1, 1, 0, 0, 1, 0, 0, 1],
77
+ ... [1, 0, 1, 0, 0, 1, 1, 1],
78
+ ... [0, 1, 0, 1, 0, 0, 1, 0],
79
+ ... [1, 1, 1, 0, 0, 1, 0, 1],
80
+ ... [0, 1, 0, 0, 1, 0, 1, 1],
81
+ ... [0, 0, 0, 1, 1, 1, 0, 1],
82
+ ... [0, 1, 0, 1, 0, 1, 1, 1],
83
+ ... [1, 1, 0, 0, 0, 0, 0, 1]]
84
+ >>> solve_maze(maze,0,2,len(maze)-1,2)
85
+ [0, 0, 1, 1, 0, 0, 0, 0]
86
+ [0, 0, 0, 1, 1, 0, 0, 0]
87
+ [0, 0, 0, 0, 1, 0, 0, 0]
88
+ [0, 0, 0, 1, 1, 0, 0, 0]
89
+ [0, 0, 1, 1, 0, 0, 0, 0]
90
+ [0, 0, 1, 0, 0, 0, 0, 0]
91
+ [0, 0, 1, 0, 0, 0, 0, 0]
92
+ [0, 0, 1, 0, 0, 0, 0, 0]
93
+ True
94
+
95
+
96
+ >>> maze = [[1, 0, 0],
97
+ ... [0, 1, 1],
98
+ ... [1, 0, 0]]
99
+ >>> solve_maze(maze,0,1,len(maze)-1,len(maze)-1)
61
100
No solution exists!
62
101
False
63
102
64
103
>>> maze = [[0, 1],
65
104
... [1, 0]]
66
- >>> solve_maze(maze)
105
+ >>> solve_maze(maze,0,0,len(maze)-1,len(maze)-1 )
67
106
No solution exists!
68
107
False
108
+
109
+ >>> maze = [[0, 1],
110
+ ... [1, 0]]
111
+ >>> solve_maze(maze,2,0,len(maze)-1,len(maze)-1)
112
+ Invalid source coordinates
113
+ False
114
+
115
+ >>> maze = [[1, 0, 0],
116
+ ... [0, 1, 1],
117
+ ... [1, 0, 0]]
118
+ >>> solve_maze(maze,0,1,len(maze),len(maze)-1)
119
+ Invalid destination coordinates
120
+ False
69
121
"""
70
122
size = len (maze )
71
- source_row = 0
72
- source_column = 0
73
- destination_row = size - 1
74
- destination_column = size - 1
123
+ # Check if source and destination coordinates are Invalid.
124
+ if not (0 <= source_row <= size - 1 and 0 <= source_column <= size - 1 ):
125
+ print ("Invalid source coordinates" )
126
+ return False
127
+ elif not (0 <= destination_row <= size - 1 and 0 <= destination_column <= size - 1 ):
128
+ print ("Invalid destination coordinates" )
129
+ return False
75
130
# We need to create solution object to save path.
76
131
solutions = [[0 for _ in range (size )] for _ in range (size )]
77
132
solved = run_maze (
@@ -105,7 +160,7 @@ def run_maze(
105
160
"""
106
161
size = len (maze )
107
162
# Final check point.
108
- if i == destination_row and j == destination_column :
163
+ if i == destination_row and j == destination_column and maze [ i ][ j ] == 0 :
109
164
solutions [i ][j ] = 1
110
165
return True
111
166
0 commit comments