Skip to content

Commit 6b4c90b

Browse files
[UPDATE] TheAlgorithms#9066 Rate_in_Maze.py
1 parent 882fb2f commit 6b4c90b

File tree

1 file changed

+21
-11
lines changed

1 file changed

+21
-11
lines changed

backtracking/rat_in_maze.py

Lines changed: 21 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,15 @@
44
def solve_maze(maze: list[list[int]]) -> bool:
55
"""
66
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.
107
Parameters :
118
maze(2D matrix) : maze
129
Returns:
1310
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.
1416
>>> maze = [[0, 1, 0, 1, 1],
1517
... [0, 0, 0, 0, 0],
1618
... [1, 0, 1, 0, 1],
@@ -24,6 +26,9 @@ def solve_maze(maze: list[list[int]]) -> bool:
2426
[0, 0, 0, 0, 1]
2527
True
2628
29+
Note:
30+
In the output maze, the ones (1s) represent one of the possible paths from the source to the destination.
31+
2732
>>> maze = [[0, 1, 0, 1, 1],
2833
... [0, 0, 0, 0, 0],
2934
... [0, 0, 0, 0, 1],
@@ -59,18 +64,22 @@ def solve_maze(maze: list[list[int]]) -> bool:
5964
No solution exists!
6065
False
6166
"""
62-
size = len(maze)
6367
# 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
6473
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)
6675
if solved:
6776
print("\n".join(str(row) for row in solutions))
6877
else:
6978
print("No solution exists!")
7079
return solved
7180

7281

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:
7483
"""
7584
This method is recursive starting from (i, j) and going in one of four directions:
7685
up, down, left, right.
@@ -84,7 +93,7 @@ def run_maze(maze: list[list[int]], i: int, j: int, solutions: list[list[int]])
8493
"""
8594
size = len(maze)
8695
# Final check point.
87-
if i == j == (size - 1):
96+
if i == destination_row and j == destination_column:
8897
solutions[i][j] = 1
8998
return True
9099

@@ -100,10 +109,10 @@ def run_maze(maze: list[list[int]], i: int, j: int, solutions: list[list[int]])
100109

101110
# check for directions
102111
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)
107116
):
108117
return True
109118

@@ -112,6 +121,7 @@ def run_maze(maze: list[list[int]], i: int, j: int, solutions: list[list[int]])
112121
return False
113122

114123

124+
115125
if __name__ == "__main__":
116126
import doctest
117127

0 commit comments

Comments
 (0)