Skip to content

Commit e7686d2

Browse files
Updated rat_in_maze.py.
1 parent 58bbb81 commit e7686d2

File tree

1 file changed

+71
-16
lines changed

1 file changed

+71
-16
lines changed

Diff for: backtracking/rat_in_maze.py

+71-16
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,36 @@
11
from __future__ import annotations
22

33

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:
511
"""
612
This method solves the "rat in maze" problem.
713
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.
919
Returns:
1020
Return: True if the maze has a solution or False if it does not.
1121
Description:
1222
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).
1626
By providing custom row and column values, the source and destination
1727
cells can be adjusted.
1828
>>> maze = [[0, 1, 0, 1, 1],
1929
... [0, 0, 0, 0, 0],
2030
... [1, 0, 1, 0, 1],
2131
... [0, 0, 1, 0, 0],
2232
... [1, 0, 0, 1, 0]]
23-
>>> solve_maze(maze)
33+
>>> solve_maze(maze,0,0,len(maze)-1,len(maze)-1)
2434
[1, 0, 0, 0, 0]
2535
[1, 1, 1, 1, 0]
2636
[0, 0, 0, 1, 0]
@@ -37,7 +47,7 @@ def solve_maze(maze: list[list[int]]) -> bool:
3747
... [0, 0, 0, 0, 1],
3848
... [0, 0, 0, 0, 0],
3949
... [0, 0, 0, 0, 0]]
40-
>>> solve_maze(maze)
50+
>>> solve_maze(maze,0,0,len(maze)-1,len(maze)-1)
4151
[1, 0, 0, 0, 0]
4252
[1, 0, 0, 0, 0]
4353
[1, 0, 0, 0, 0]
@@ -48,30 +58,75 @@ def solve_maze(maze: list[list[int]]) -> bool:
4858
>>> maze = [[0, 0, 0],
4959
... [0, 1, 0],
5060
... [1, 0, 0]]
51-
>>> solve_maze(maze)
61+
>>> solve_maze(maze,0,0,len(maze)-1,len(maze)-1)
5262
[1, 1, 1]
5363
[0, 0, 1]
5464
[0, 0, 1]
5565
True
5666
57-
>>> maze = [[0, 1, 0],
67+
>>> maze = [[1, 0, 0],
5868
... [0, 1, 0],
5969
... [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)
61100
No solution exists!
62101
False
63102
64103
>>> maze = [[0, 1],
65104
... [1, 0]]
66-
>>> solve_maze(maze)
105+
>>> solve_maze(maze,0,0,len(maze)-1,len(maze)-1)
67106
No solution exists!
68107
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
69121
"""
70122
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
75130
# We need to create solution object to save path.
76131
solutions = [[0 for _ in range(size)] for _ in range(size)]
77132
solved = run_maze(
@@ -105,7 +160,7 @@ def run_maze(
105160
"""
106161
size = len(maze)
107162
# 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:
109164
solutions[i][j] = 1
110165
return True
111166

0 commit comments

Comments
 (0)