Skip to content

Commit

Permalink
Merge pull request #1501 from Pranav016/rat-in-maze
Browse files Browse the repository at this point in the history
Added RatInMaze.py
  • Loading branch information
HarshCasper authored Dec 27, 2020
2 parents 50d8168 + c027775 commit 37fe0ff
Show file tree
Hide file tree
Showing 2 changed files with 101 additions and 0 deletions.
1 change: 1 addition & 0 deletions Python/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,7 @@
- [Hamiltonian Cycle](backtracking/Hamiltonian_Cycle.py)
- [Sudoku Solver](backtracking/sudokuSolver.py)
- [Subset Sum](backtracking/Subset_Sum.py)
- [Rat-In-Maze problem](backracking/RatInMaze.py)


## Dynamic Programming
Expand Down
100 changes: 100 additions & 0 deletions Python/backtracking/RatInMaze.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,100 @@
'''
Problem statement- given a maze of 1s with obstacles (represented by 0).
We have to print all the possible paths to the end.
Logical intuition- We have to explore all the possible paths hence our mouse
will move in all four directions untill it reaches the end
'''


def printPathHelper(i, j, maze, n, solution):
'''
Summary Line:
This funcion helps us to print the path followed by the rat in the maze.
It puts 1s at places that the mouse can go and 0s
at places where the mouse doesn't go.
Args:
i- current position on the maze horizontally
j- current position on the maze vertically
maze- The 2D matrix that was inputted through which the Rat has to pass
n- side of the 2D matrix
solution- solution matrix to be returned
'''
if i == n-1 and j == n-1:
print()
solution[i][j] = 1
for i in range(n):
for j in range(n):
print(solution[i][j], end=" ")
print()
solution[i][j] = 0
return
if i < 0 or j < 0 or i >= n or j >= n or\
maze[i][j] == 0 or solution[i][j] == 1:
return
solution[i][j] = 1
printPathHelper(i+1, j, maze, n, solution)
printPathHelper(i, j+1, maze, n, solution)
printPathHelper(i-1, j, maze, n, solution)
printPathHelper(i, j-1, maze, n, solution)
solution[i][j] = 0
return


def printPath(maze):
'''
Summary Line:
This function helps us to take form a solution matrix and calls
another method printPathHelper() on the solution matrix
to print all possible paths for the mouse.
Args:
maze- The 2D matrix that was inputted through which the Rat has to pass
'''
n = len(maze)
solution = [[0 for j in range(n)]for i in range(n)]
printPathHelper(0, 0, maze, n, solution)

if __name__ == '__main__':
n = int(input())
maze = []
for j in range(n):
arr = [int(i) for i in input().split()]
maze.append(arr)
j += 1
printPath(maze)

'''
Sample input-
4
1 1 1 1
1 1 0 1
1 0 1 1
1 1 1 1
Illustration for the maze-
(Obstacles at positions where there is 0 in the input matrix)
0 1 2 3
+-------+-------+-------+-------+
| | | | |
0 | | | | |
| | | | |
+---------------+-------+-------+
| | | | |
1 | | | x | |
| | | | |
+-------+-------+-------+-------+
| | | | |
2 | | x | | |
| | | | |
+-------+-------+-------+-------+
| | | | |
3 | | | | |
| | | | |
+-------+-------+-------+-------+
Sample Output-
All possible paths will be printed
Time complexity- O(2^(n^2))
'''

0 comments on commit 37fe0ff

Please sign in to comment.