Skip to content

[UPDATED]rat_in_maze.py #9087

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 22 commits into from
Closed
Changes from 5 commits
Commits
Show all changes
22 commits
Select commit Hold shift + click to select a range
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
47 changes: 37 additions & 10 deletions backtracking/rat_in_maze.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,17 @@
def solve_maze(maze: list[list[int]]) -> bool:
"""
This method solves the "rat in maze" problem.
In this problem we have some n by n matrix, a start point and an end point.
We want to go from the start to the end. In this matrix zeroes represent walls
and ones paths we can use.
Parameters :
maze(2D matrix) : maze
Returns:
Return: True if the maze has a solution or False if it does not.
Description:
This method navigates through a maze represented as an n by n matrix,
starting from a specified source cell (default: top-left corner) and
aiming to reach a destination cell (default: bottom-right corner).
The maze consists of walls (1s) and open paths (0s).
By providing custom row and column values, the source and destination
cells can be adjusted.
>>> maze = [[0, 1, 0, 1, 1],
... [0, 0, 0, 0, 0],
... [1, 0, 1, 0, 1],
Expand All @@ -24,6 +28,10 @@ def solve_maze(maze: list[list[int]]) -> bool:
[0, 0, 0, 0, 1]
True

Note:
In the output maze, the ones (1s) represent one of the possible
paths from the source to the destination.

>>> maze = [[0, 1, 0, 1, 1],
... [0, 0, 0, 0, 0],
... [0, 0, 0, 0, 1],
Expand Down Expand Up @@ -60,17 +68,30 @@ def solve_maze(maze: list[list[int]]) -> bool:
False
"""
size = len(maze)
source_row = 0
source_column = 0
destination_row = size - 1
destination_column = size - 1
# We need to create solution object to save path.
solutions = [[0 for _ in range(size)] for _ in range(size)]
solved = run_maze(maze, 0, 0, solutions)
solved = run_maze(
maze, source_row, source_column, destination_row, destination_column, solutions
)
if solved:
print("\n".join(str(row) for row in solutions))
else:
print("No solution exists!")
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
print("No solution exists!")

return solved


def run_maze(maze: list[list[int]], i: int, j: int, solutions: list[list[int]]) -> bool:
def run_maze(
maze: list[list[int]],
i: int,
j: int,
destination_row: int,
destination_column: int,
solutions: list[list[int]],
) -> bool:
"""
This method is recursive starting from (i, j) and going in one of four directions:
up, down, left, right.
Expand All @@ -84,7 +105,7 @@ def run_maze(maze: list[list[int]], i: int, j: int, solutions: list[list[int]])
"""
size = len(maze)
# Final check point.
if i == j == (size - 1):
if i == destination_row and j == destination_column:
solutions[i][j] = 1
return True

Expand All @@ -100,10 +121,16 @@ def run_maze(maze: list[list[int]], i: int, j: int, solutions: list[list[int]])

# check for directions
if (
run_maze(maze, i + 1, j, solutions)
or run_maze(maze, i, j + 1, solutions)
or run_maze(maze, i - 1, j, solutions)
or run_maze(maze, i, j - 1, solutions)
run_maze(maze, i + 1, j, destination_row, destination_column, solutions)
or run_maze(
maze, i, j + 1, destination_row, destination_column, solutions
)
or run_maze(
maze, i - 1, j, destination_row, destination_column, solutions
)
or run_maze(
maze, i, j - 1, destination_row, destination_column, solutions
)
):
return True

Expand Down