-
-
Notifications
You must be signed in to change notification settings - Fork 46.9k
implement rat in maze algorithm. #2106
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
Merged
Merged
Changes from 4 commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
172e454
implement rat in maze algorithm.
beqakd ac6ecb2
style changes
beqakd ed8201b
fix trailing whitespace
beqakd 9a98237
add test, fix style
beqakd 0e1cb01
fix style
beqakd 552d35d
method change
beqakd b5a630a
minor changes
beqakd 13d2147
style changes
beqakd f23289f
return solved
cclauss 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 |
---|---|---|
@@ -0,0 +1,167 @@ | ||
def fill_matrix(matrix, i, j): | ||
""" | ||
This method just fills matrix i, j with value | ||
Parameters : | ||
matrix(2D matrix) : matrix | ||
i, j : coordinates of the matrix. | ||
""" | ||
matrix[i][j] = 1 | ||
|
||
|
||
def solveMaze(maze): | ||
""" | ||
This method solves rat in maze algorithm. | ||
In this problem we have n by n matrix and we have start point and end point | ||
we want to go from source to distination. In this matrix 0 are block paths | ||
1 are open paths we can use. | ||
Parameters : | ||
maze(2D matrix) : maze | ||
size : size of our maze(square) | ||
cclauss marked this conversation as resolved.
Show resolved
Hide resolved
|
||
Returns: | ||
cclauss marked this conversation as resolved.
Show resolved
Hide resolved
|
||
This method returns true and array if solution found otherwise false and None | ||
cclauss marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
>>> 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]] | ||
>>> solveMaze(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] | ||
|
||
>>> 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]] | ||
>>> solveMaze(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] | ||
|
||
>>> maze = [[0, 0, 0], | ||
... [0, 1, 0], | ||
... [1, 0, 0]] | ||
>>> solveMaze(maze) | ||
[1, 1, 1] | ||
[0, 0, 1] | ||
[0, 0, 1] | ||
|
||
>>> maze = [[0, 1, 0], | ||
... [0, 1, 0], | ||
... [1, 0, 0]] | ||
>>> solveMaze(maze) | ||
Solution does not exists! | ||
|
||
>>> maze = [[0, 1], | ||
... [1, 0]] | ||
>>> solveMaze(maze) | ||
Solution does not exists! | ||
""" | ||
size = len(maze) | ||
# We need to create solution object to save path. | ||
solutions = [[0 for _ in range(size)] for _ in range(size)] | ||
solved = runmaze(maze, 0, 0, solutions) | ||
|
||
if solved: | ||
result = "".join(str(row) + "\n" for row in solutions) | ||
print(result[: len(result) - 1]) # For last \n | ||
cclauss marked this conversation as resolved.
Show resolved
Hide resolved
|
||
else: | ||
print("Solution does not exists!") | ||
|
||
|
||
""" | ||
This method is recursive method which starts from i and j | ||
and goes with 4 direction option up, down, left, right | ||
if path found to destination it breaks and return True | ||
otherwise False | ||
Parameters: | ||
maze(2D matrix) : maze | ||
size : size of our maze(square) | ||
i, j : coordinates of matrix | ||
solutions(2D matrix) : solutions | ||
Returns: | ||
Boolean if path is found True, Otherwise False. | ||
|
||
>>> 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]] | ||
>>> solveMaze(maze) | ||
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]] | ||
>>> solveMaze(maze) | ||
True | ||
|
||
>>> maze = [[0, 0, 0], | ||
... [0, 1, 0], | ||
... [1, 0, 0]] | ||
>>> solveMaze(maze) | ||
True | ||
|
||
>>> maze = [[0, 1, 0], | ||
... [0, 1, 0], | ||
... [1, 0, 0]] | ||
>>> solveMaze(maze) | ||
False | ||
|
||
>>> maze = [[0, 1], | ||
... [1, 0]] | ||
>>> solveMaze(maze) | ||
False | ||
""" | ||
|
||
|
||
def runmaze(maze, i, j, solutions): | ||
size = len(maze) | ||
# Final check point. | ||
cclauss marked this conversation as resolved.
Show resolved
Hide resolved
|
||
if (i == (size - 1)) and (j == (size - 1)): | ||
cclauss marked this conversation as resolved.
Show resolved
Hide resolved
|
||
fill_matrix(solutions, i, j) | ||
cclauss marked this conversation as resolved.
Show resolved
Hide resolved
|
||
return True | ||
|
||
lower_flag = (not (i < 0)) and (not (j < 0)) # Check lower bounds | ||
upper_flag = (i < size) and (j < size) # Check upper bounds | ||
|
||
if lower_flag and upper_flag: | ||
# check for already visited and block points. | ||
block_flag = (not (solutions[i][j])) and (not (maze[i][j])) | ||
if block_flag: | ||
# check visited | ||
fill_matrix(solutions, i, j) | ||
|
||
# check for directions | ||
if runmaze(maze, i + 1, j, solutions): | ||
return True | ||
|
||
if runmaze(maze, i, j + 1, solutions): | ||
return True | ||
|
||
if runmaze(maze, i - 1, j, solutions): | ||
return True | ||
|
||
if runmaze(maze, i, j - 1, solutions): | ||
return True | ||
|
||
solutions[i][j] = 0 | ||
return False | ||
|
||
|
||
def main(): | ||
import doctest | ||
|
||
doctest.testmod() | ||
|
||
|
||
if __name__ == "__main__": | ||
main() |
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.