-
-
Notifications
You must be signed in to change notification settings - Fork 46.9k
[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
Closed
[UPDATED]rat_in_maze.py #9087
Changes from 9 commits
Commits
Show all changes
22 commits
Select commit
Hold shift + click to select a range
6b4c90b
[UPDATE] #9066 Rate_in_Maze.py
Muhammadummerr e90e0a4
[UPDATED] #9066 Rate_in_Maze.py
Muhammadummerr c8c901c
[UPDATED] #9066 Rate_in_Maze.py
Muhammadummerr 58bbb81
[UPDATED] #9066 rat_in_maze
Muhammadummerr 8ff2d65
Update backtracking/rat_in_maze.py
Muhammadummerr e7686d2
Updated rat_in_maze.py.
Muhammadummerr b64b401
Updated rat_in_maze.py
Muhammadummerr 932dafd
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] f86c89a
Merge pull request #2 from TheAlgorithms/master
Muhammadummerr 553d1f0
Update backtracking/rat_in_maze.py
Muhammadummerr 765f867
[Updated] rat_in_maze.py
Muhammadummerr 901d661
UPDATED
Muhammadummerr 3897d0a
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] 84c9766
UPDATED rat_in_maze.py
Muhammadummerr 2045f7f
Formatted rat_in_maze.py
Muhammadummerr 1c99a68
Formatted rat_in_maze.py
Muhammadummerr 92590f8
formatted rat_in_maze.py
Muhammadummerr 39a00ca
fixed return type.
Muhammadummerr 3076868
fixed return type.
Muhammadummerr 9e045ff
fixed return type.
Muhammadummerr 6ffb7b0
removed print statements.
Muhammadummerr d5c3f4f
Refactor path representation in solution.
Muhammadummerr File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change | ||||
---|---|---|---|---|---|---|
@@ -1,76 +1,157 @@ | ||||||
from __future__ import annotations | ||||||
|
||||||
|
||||||
def solve_maze(maze: list[list[int]]) -> bool: | ||||||
def solve_maze( | ||||||
maze: list[list[int]], | ||||||
source_row: int, | ||||||
source_column: int, | ||||||
destination_row: int, | ||||||
destination_column: 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. | ||||||
>>> maze = [[0, 1, 0, 1, 1], | ||||||
... [0, 0, 0, 0, 0], | ||||||
... [1, 0, 1, 0, 1], | ||||||
... [0, 0, 1, 0, 0], | ||||||
... [1, 0, 0, 1, 0]] | ||||||
>>> solve_maze(maze) | ||||||
[1, 0, 0, 0, 0] | ||||||
[1, 1, 1, 1, 0] | ||||||
[0, 0, 0, 1, 0] | ||||||
[0, 0, 0, 1, 1] | ||||||
[0, 0, 0, 0, 1] | ||||||
True | ||||||
|
||||||
>>> maze = [[0, 1, 0, 1, 1], | ||||||
... [0, 0, 0, 0, 0], | ||||||
... [0, 0, 0, 0, 1], | ||||||
... [0, 0, 0, 0, 0], | ||||||
... [0, 0, 0, 0, 0]] | ||||||
>>> solve_maze(maze) | ||||||
[1, 0, 0, 0, 0] | ||||||
[1, 0, 0, 0, 0] | ||||||
[1, 0, 0, 0, 0] | ||||||
[1, 0, 0, 0, 0] | ||||||
[1, 1, 1, 1, 1] | ||||||
True | ||||||
|
||||||
>>> maze = [[0, 0, 0], | ||||||
... [0, 1, 0], | ||||||
... [1, 0, 0]] | ||||||
>>> solve_maze(maze) | ||||||
[1, 1, 1] | ||||||
[0, 0, 1] | ||||||
[0, 0, 1] | ||||||
True | ||||||
|
||||||
>>> maze = [[0, 1, 0], | ||||||
... [0, 1, 0], | ||||||
... [1, 0, 0]] | ||||||
>>> solve_maze(maze) | ||||||
No solution exists! | ||||||
False | ||||||
|
||||||
>>> maze = [[0, 1], | ||||||
... [1, 0]] | ||||||
>>> solve_maze(maze) | ||||||
No solution exists! | ||||||
False | ||||||
This method solves the "rat in maze" problem. | ||||||
Parameters : | ||||||
- maze(2D matrix) : maze | ||||||
- source_row (int): The row index of the starting point. | ||||||
- source_column (int): The column index of the starting point. | ||||||
- destination_row (int): The row index of the destination point. | ||||||
- destination_column (int): The column index of the destination point. | ||||||
Returns: | ||||||
Return: True if the maze has a solution or False if it does not. | ||||||
Muhammadummerr marked this conversation as resolved.
Show resolved
Hide resolved
|
||||||
Description: | ||||||
This method navigates through a maze represented as an n by n matrix, | ||||||
<<<<<<< HEAD | ||||||
Muhammadummerr marked this conversation as resolved.
Show resolved
Hide resolved
|
||||||
starting from a specified source cell and | ||||||
aiming to reach a destination cell. | ||||||
======= | ||||||
starting from a specified source cell (default: top-left corner) and | ||||||
aiming to reach a destination cell (default: bottom-right corner). | ||||||
>>>>>>> origin/new_branch | ||||||
Muhammadummerr marked this conversation as resolved.
Show resolved
Hide resolved
|
||||||
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], | ||||||
... [0, 0, 1, 0, 0], | ||||||
... [1, 0, 0, 1, 0]] | ||||||
>>> solve_maze(maze,0,0,len(maze)-1,len(maze)-1) | ||||||
[1, 0, 0, 0, 0] | ||||||
[1, 1, 1, 1, 0] | ||||||
[0, 0, 0, 1, 0] | ||||||
[0, 0, 0, 1, 1] | ||||||
[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], | ||||||
... [0, 0, 0, 0, 0], | ||||||
... [0, 0, 0, 0, 0]] | ||||||
>>> solve_maze(maze,0,0,len(maze)-1,len(maze)-1) | ||||||
[1, 0, 0, 0, 0] | ||||||
[1, 0, 0, 0, 0] | ||||||
[1, 0, 0, 0, 0] | ||||||
[1, 0, 0, 0, 0] | ||||||
[1, 1, 1, 1, 1] | ||||||
True | ||||||
|
||||||
>>> maze = [[0, 0, 0], | ||||||
... [0, 1, 0], | ||||||
... [1, 0, 0]] | ||||||
>>> solve_maze(maze,0,0,len(maze)-1,len(maze)-1) | ||||||
[1, 1, 1] | ||||||
[0, 0, 1] | ||||||
[0, 0, 1] | ||||||
True | ||||||
|
||||||
>>> maze = [[1, 0, 0], | ||||||
... [0, 1, 0], | ||||||
... [1, 0, 0]] | ||||||
>>> solve_maze(maze,0,1,len(maze)-1,len(maze)-1) | ||||||
[0, 1, 1] | ||||||
[0, 0, 1] | ||||||
[0, 0, 1] | ||||||
True | ||||||
|
||||||
>>> maze = [[1, 1, 0, 0, 1, 0, 0, 1], | ||||||
... [1, 0, 1, 0, 0, 1, 1, 1], | ||||||
... [0, 1, 0, 1, 0, 0, 1, 0], | ||||||
... [1, 1, 1, 0, 0, 1, 0, 1], | ||||||
... [0, 1, 0, 0, 1, 0, 1, 1], | ||||||
... [0, 0, 0, 1, 1, 1, 0, 1], | ||||||
... [0, 1, 0, 1, 0, 1, 1, 1], | ||||||
... [1, 1, 0, 0, 0, 0, 0, 1]] | ||||||
>>> solve_maze(maze,0,2,len(maze)-1,2) | ||||||
[0, 0, 1, 1, 0, 0, 0, 0] | ||||||
[0, 0, 0, 1, 1, 0, 0, 0] | ||||||
[0, 0, 0, 0, 1, 0, 0, 0] | ||||||
[0, 0, 0, 1, 1, 0, 0, 0] | ||||||
[0, 0, 1, 1, 0, 0, 0, 0] | ||||||
[0, 0, 1, 0, 0, 0, 0, 0] | ||||||
[0, 0, 1, 0, 0, 0, 0, 0] | ||||||
[0, 0, 1, 0, 0, 0, 0, 0] | ||||||
True | ||||||
|
||||||
|
||||||
>>> maze = [[1, 0, 0], | ||||||
... [0, 1, 1], | ||||||
... [1, 0, 0]] | ||||||
>>> solve_maze(maze,0,1,len(maze)-1,len(maze)-1) | ||||||
No solution exists! | ||||||
False | ||||||
|
||||||
>>> maze = [[0, 1], | ||||||
... [1, 0]] | ||||||
>>> solve_maze(maze,0,0,len(maze)-1,len(maze)-1) | ||||||
No solution exists! | ||||||
False | ||||||
|
||||||
>>> maze = [[0, 1], | ||||||
... [1, 0]] | ||||||
>>> solve_maze(maze,2,0,len(maze)-1,len(maze)-1) | ||||||
Invalid source coordinates | ||||||
False | ||||||
|
||||||
>>> maze = [[1, 0, 0], | ||||||
... [0, 1, 1], | ||||||
... [1, 0, 0]] | ||||||
>>> solve_maze(maze,0,1,len(maze),len(maze)-1) | ||||||
Invalid destination coordinates | ||||||
False | ||||||
""" | ||||||
size = len(maze) | ||||||
# Check if source and destination coordinates are Invalid. | ||||||
if not (0 <= source_row <= size - 1 and 0 <= source_column <= size - 1): | ||||||
print("Invalid source coordinates") | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||
return False | ||||||
elif not (0 <= destination_row <= size - 1 and 0 <= destination_column <= size - 1): | ||||||
print("Invalid destination coordinates") | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||
return False | ||||||
# 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!") | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||
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. | ||||||
|
@@ -84,7 +165,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 and maze[i][j] == 0: | ||||||
solutions[i][j] = 1 | ||||||
return True | ||||||
|
||||||
|
@@ -100,10 +181,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 | ||||||
|
||||||
|
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.