Skip to content

Commit e90e0a4

Browse files
[UPDATED] TheAlgorithms#9066 Rate_in_Maze.py
1 parent 6b4c90b commit e90e0a4

File tree

1 file changed

+17
-9
lines changed

1 file changed

+17
-9
lines changed

Diff for: backtracking/rat_in_maze.py

+17-9
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,9 @@ def solve_maze(maze: list[list[int]]) -> bool:
99
Returns:
1010
Return: True if the maze has a solution or False if it does not.
1111
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).
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
14+
aiming to reach a destination cell (default: bottom-right corner).
1415
The maze consists of walls (0s) and open paths (1s).
1516
By providing custom row and column values, the source and destination cells can be adjusted.
1617
>>> maze = [[0, 1, 0, 1, 1],
@@ -27,7 +28,8 @@ def solve_maze(maze: list[list[int]]) -> bool:
2728
True
2829
2930
Note:
30-
In the output maze, the ones (1s) represent one of the possible paths from the source to the destination.
31+
In the output maze, the ones (1s) represent one of the possible
32+
paths from the source to the destination.
3133
3234
>>> maze = [[0, 1, 0, 1, 1],
3335
... [0, 0, 0, 0, 0],
@@ -71,15 +73,17 @@ def solve_maze(maze: list[list[int]]) -> bool:
7173
destination_row=size-1
7274
destination_column=size-1
7375
solutions = [[0 for _ in range(size)] for _ in range(size)]
74-
solved = run_maze(maze, source_row, source_column,destination_row,destination_column, solutions)
76+
solved = run_maze(maze, source_row, source_column,destination_row,
77+
destination_column, solutions)
7578
if solved:
7679
print("\n".join(str(row) for row in solutions))
7780
else:
7881
print("No solution exists!")
7982
return solved
8083

8184

82-
def run_maze(maze: list[list[int]], i: int, j: int,destination_row:int,destination_column:int, solutions: list[list[int]]) -> bool:
85+
def run_maze(maze: list[list[int]], i: int, j: int,destination_row:int,
86+
destination_column:int, solutions: list[list[int]]) -> bool:
8387
"""
8488
This method is recursive starting from (i, j) and going in one of four directions:
8589
up, down, left, right.
@@ -109,10 +113,14 @@ def run_maze(maze: list[list[int]], i: int, j: int,destination_row:int,destinati
109113

110114
# check for directions
111115
if (
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)
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)
116124
):
117125
return True
118126

0 commit comments

Comments
 (0)