Skip to content

Commit c8c901c

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

File tree

1 file changed

+29
-20
lines changed

1 file changed

+29
-20
lines changed

Diff for: backtracking/rat_in_maze.py

+29-20
Original file line numberDiff line numberDiff line change
@@ -10,10 +10,11 @@ def solve_maze(maze: list[list[int]]) -> bool:
1010
Return: True if the maze has a solution or False if it does not.
1111
Description:
1212
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
13+
starting from a specified source cell (default: top-left corner) and
1414
aiming to reach a destination cell (default: bottom-right corner).
1515
The maze consists of walls (0s) and open paths (1s).
16-
By providing custom row and column values, the source and destination cells can be adjusted.
16+
By providing custom row and column values, the source and destination
17+
cells can be adjusted.
1718
>>> maze = [[0, 1, 0, 1, 1],
1819
... [0, 0, 0, 0, 0],
1920
... [1, 0, 1, 0, 1],
@@ -68,22 +69,29 @@ def solve_maze(maze: list[list[int]]) -> bool:
6869
"""
6970
# We need to create solution object to save path.
7071
size = len(maze)
71-
source_row=0
72-
source_column=0
73-
destination_row=size-1
74-
destination_column=size-1
72+
source_row = 0
73+
source_column = 0
74+
destination_row = size - 1
75+
destination_column = size - 1
7576
solutions = [[0 for _ in range(size)] for _ in range(size)]
76-
solved = run_maze(maze, source_row, source_column,destination_row,
77-
destination_column, solutions)
77+
solved = run_maze(
78+
maze, source_row, source_column, destination_row, destination_column, solutions
79+
)
7880
if solved:
7981
print("\n".join(str(row) for row in solutions))
8082
else:
8183
print("No solution exists!")
8284
return solved
8385

8486

85-
def run_maze(maze: list[list[int]], i: int, j: int,destination_row:int,
86-
destination_column:int, solutions: list[list[int]]) -> bool:
87+
def run_maze(
88+
maze: list[list[int]],
89+
i: int,
90+
j: int,
91+
destination_row: int,
92+
destination_column: int,
93+
solutions: list[list[int]],
94+
) -> bool:
8795
"""
8896
This method is recursive starting from (i, j) and going in one of four directions:
8997
up, down, left, right.
@@ -97,7 +105,7 @@ def run_maze(maze: list[list[int]], i: int, j: int,destination_row:int,
97105
"""
98106
size = len(maze)
99107
# Final check point.
100-
if i == destination_row and j == destination_column:
108+
if i == destination_row and j == destination_column:
101109
solutions[i][j] = 1
102110
return True
103111

@@ -113,14 +121,16 @@ def run_maze(maze: list[list[int]], i: int, j: int,destination_row:int,
113121

114122
# check for directions
115123
if (
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)
124+
run_maze(maze, i + 1, j, destination_row, destination_column, solutions)
125+
or run_maze(
126+
maze, i, j + 1, destination_row, destination_column, solutions
127+
)
128+
or run_maze(
129+
maze, i - 1, j, destination_row, destination_column, solutions
130+
)
131+
or run_maze(
132+
maze, i, j - 1, destination_row, destination_column, solutions
133+
)
124134
):
125135
return True
126136

@@ -129,7 +139,6 @@ def run_maze(maze: list[list[int]], i: int, j: int,destination_row:int,
129139
return False
130140

131141

132-
133142
if __name__ == "__main__":
134143
import doctest
135144

0 commit comments

Comments
 (0)