|
| 1 | +# RAT IN A MAZE Problem using Backtracking algorithm |
| 2 | +# Problem Statement : The aim of this problem is to find out |
| 3 | +# the matrix through which the rat can reach |
| 4 | +# it's destination by the shortest path |
| 5 | +# travelling through the given maze as a matrix |
| 6 | +# using 1 and 0, where 1 means open path and 0 means |
| 7 | +# blocked path. |
| 8 | + |
| 9 | +# Author : Abhishek Sharma, 2021 |
| 10 | + |
| 11 | +# ----------------------------------------------------------------- |
| 12 | + |
| 13 | +# Solution for the above problem statement using Python 3 language. |
| 14 | +# Algorithm used : Backtracking |
| 15 | + |
| 16 | +# ----------------------------------------------------------------- |
| 17 | + |
| 18 | + |
| 19 | +# Initializing the constant N value (Here it is 4) |
| 20 | +N = 4 |
| 21 | + |
| 22 | +# A utility function to print solution matrix sol |
| 23 | +def printSolution( sol ): |
| 24 | + |
| 25 | + for i in sol: |
| 26 | + for j in i: |
| 27 | + print(str(j) + " ", end ="") |
| 28 | + print("") |
| 29 | + |
| 30 | + |
| 31 | +# A utility function to check if x, y is valid |
| 32 | +# index for N * N Maze |
| 33 | +def isSafe( maze, x, y ): |
| 34 | + |
| 35 | + if x >= 0 and x < N and y >= 0 and y < N and maze[x][y] == 1: |
| 36 | + return True |
| 37 | + |
| 38 | + return False |
| 39 | + |
| 40 | + |
| 41 | +# This function solves the Maze problem using Backtracking. |
| 42 | +# It mainly uses solveMazeUtil() to solve the problem. It |
| 43 | +# returns false if no path is possible, otherwise return |
| 44 | +# true and prints the path in the form of 1s. Please note |
| 45 | +# that there may be more than one solutions, this function |
| 46 | +# prints one of the feasable solutions. |
| 47 | + |
| 48 | +# To check whether there is proper solution or not |
| 49 | +def solveMaze( maze ): |
| 50 | + |
| 51 | + # Creating a 4 * 4 2-D list |
| 52 | + sol = [ [ 0 for j in range(4) ] for i in range(4) ] |
| 53 | + |
| 54 | + if solveMazeUtil(maze, 0, 0, sol) == False: |
| 55 | + print("Solution doesn't exist"); |
| 56 | + return False |
| 57 | + |
| 58 | + printSolution(sol) |
| 59 | + return True |
| 60 | + |
| 61 | + |
| 62 | + |
| 63 | +# A recursive utility function to solve Maze problem |
| 64 | +def solveMazeUtil(maze, x, y, sol): |
| 65 | + |
| 66 | + # if (x, y is goal) return True |
| 67 | + if x == N - 1 and y == N - 1: |
| 68 | + sol[x][y] = 1 |
| 69 | + return True |
| 70 | + |
| 71 | + # Check if maze[x][y] is valid |
| 72 | + if isSafe(maze, x, y) == True: |
| 73 | + |
| 74 | + # mark x, y as part of solution path |
| 75 | + sol[x][y] = 1 |
| 76 | + |
| 77 | + # Move forward in x direction |
| 78 | + if solveMazeUtil(maze, x + 1, y, sol) == True: |
| 79 | + return True |
| 80 | + |
| 81 | + # If moving in x direction doesn't give solution |
| 82 | + # then Move down in y direction |
| 83 | + if solveMazeUtil(maze, x, y + 1, sol) == True: |
| 84 | + return True |
| 85 | + |
| 86 | + # If none of the above movements work then |
| 87 | + # BACKTRACK: unmark x, y as part of solution path |
| 88 | + sol[x][y] = 0 |
| 89 | + return False |
| 90 | + |
| 91 | + |
| 92 | + |
| 93 | +# Driver program to test above function |
| 94 | +if __name__ == "__main__": |
| 95 | + # Initialising the maze |
| 96 | + maze = [ [1, 0, 0, 0], |
| 97 | + [1, 1, 0, 1], |
| 98 | + [0, 1, 0, 0], |
| 99 | + [1, 1, 1, 1] ] |
| 100 | + print ("- RAT IN A MAZE Problem using Backtracking Algorithms -") |
| 101 | + print ("-------------------------------------------------------") |
| 102 | + print () |
| 103 | + print ("The path or maze given to the rat :") |
| 104 | + printSolution (maze) |
| 105 | + print () |
| 106 | + print ("The path recognised by the algorithm :") |
| 107 | + solveMaze(maze) |
| 108 | + |
| 109 | +# ----------------------------------------------------------------- |
| 110 | +# Input given : |
| 111 | +# The path of maze given to the rat : |
| 112 | +# 1 0 0 0 |
| 113 | +# 1 1 0 1 |
| 114 | +# 0 1 0 0 |
| 115 | +# 1 1 1 1 |
| 116 | + |
| 117 | +# ----------------------------------------------------------------- |
| 118 | + |
| 119 | +# Output : |
| 120 | + |
| 121 | +# The path recognised by the algorithm : |
| 122 | +# 1 0 0 0 |
| 123 | +# 1 1 0 0 |
| 124 | +# 0 1 0 0 |
| 125 | +# 0 1 1 1 |
| 126 | + |
| 127 | +# ----------------------------------------------------------------- |
| 128 | + |
| 129 | +# Abhishek Sharma, 2021 @abhisheks008 |
0 commit comments