|
| 1 | +# Rat in a Maze Problem using Backtracking |
| 2 | +Language used : **Python 3** |
| 3 | + |
| 4 | +## 🎯 Aim |
| 5 | +The aim of this script is to find out the shortest path for the rat to reach the destination through the maze. |
| 6 | + |
| 7 | +## 👉 Purpose |
| 8 | +The main purpose of this script is to show the implementation of Backtracking algorithm on finding the shortest path to the destination. |
| 9 | + |
| 10 | +## 📄 Description |
| 11 | +- Form a recursive function, which will follow a path and check if the path reaches the destination or not. If the path does not reach the destination then backtrack and try other paths. |
| 12 | +- Implementation of Backtracking Algorithms. |
| 13 | + |
| 14 | +## 📈 Workflow of the script |
| 15 | +- `printSolution` - A utility function to print solution matrix solution. |
| 16 | +- `isSafe` - A utility function to check if x, y is valid index for N * N Maze. |
| 17 | +- `solveMaze` - To check whether there is proper solution or not. |
| 18 | +- `solveMazeUtil` - A recursive utility function to solve Maze problem. This is the main function of this codebase. This function solves the Maze problem using Backtracking. It mainly uses solveMazeUtil() to solve the problem. It returns false if no path is possible, otherwise return true and prints the path in the form of 1s. Please note that there may be more than one solutions, this function prints one of the feasable solutions. |
| 19 | +- `main` - This is the driver code for this code/script. |
| 20 | + |
| 21 | +## 📃 Explanation |
| 22 | +A Maze is given as N*N binary matrix of blocks where source block is the upper left most block i.e., maze[0][0] and destination block is lower rightmost block i.e., maze[N-1][N-1]. A rat starts from source and has to reach the destination. The rat can move only in two directions: forward and down. |
| 23 | + |
| 24 | +In the maze matrix, 0 means the block is a dead end and 1 means the block can be used in the path from source to destination. Note that this is a simple version of the typical Maze problem. For example, a more complex version can be that the rat can move in 4 directions and a more complex version can be with a limited number of moves. |
| 25 | + |
| 26 | +For example let us take a maze and then implement the maze in the 1 and 0 manner. |
| 27 | + |
| 28 | + |
| 29 | + |
| 30 | +If we convert this maze into the 1 and 0 manner then it'll be looked like this (where the 1 means open path and 0 means blocked path), |
| 31 | +``` |
| 32 | +{1, 0, 0, 0} |
| 33 | +{1, 1, 0, 1} |
| 34 | +{0, 1, 0, 0} |
| 35 | +{1, 1, 1, 1} |
| 36 | +``` |
| 37 | +And the solution path to the destination will be like this, |
| 38 | + |
| 39 | + |
| 40 | + |
| 41 | +And again if we convert the maze into the matrix format it'll be looked like this, |
| 42 | +``` |
| 43 | +{1, 0, 0, 0} |
| 44 | +{1, 1, 0, 0} |
| 45 | +{0, 1, 0, 0} |
| 46 | +{0, 1, 1, 1} |
| 47 | +
|
| 48 | +Here all the 1's are denoting the path to reach the destination! |
| 49 | +``` |
| 50 | + |
| 51 | +## 🧮 Algorithm |
| 52 | +1. Create a solution matrix, initially filled with 0’s. |
| 53 | +2. Create a recursive function, which takes initial matrix, output matrix and position of `rat (i, j)`. |
| 54 | +3. If the position is out of the matrix or the position is not valid then return. |
| 55 | +4. Mark the position output `[i][j]` as 1 and check if the current position is destination or not. If destination is reached print the output matrix and return. |
| 56 | +5. Recursively call for position `(i+1, j)` and `(i, j+1)`. |
| 57 | +6. Unmark position `(i, j)`, i.e output `[i][j] = 0`. |
| 58 | + |
| 59 | +## 💻 Input and Output |
| 60 | +- The input is given as, |
| 61 | +``` |
| 62 | +The path or maze given to the rat : |
| 63 | +1 0 0 0 |
| 64 | +1 1 0 1 |
| 65 | +0 1 0 0 |
| 66 | +1 1 1 1 |
| 67 | +``` |
| 68 | + |
| 69 | +- The Output comes out as, |
| 70 | +``` |
| 71 | +The path recognised by the algorithm : |
| 72 | +1 0 0 0 |
| 73 | +1 1 0 0 |
| 74 | +0 1 0 0 |
| 75 | +0 1 1 1 |
| 76 | +``` |
| 77 | + |
| 78 | + |
| 79 | + |
| 80 | +## ⏰ Time and Space complexity |
| 81 | +- **Time Complexity:** `O(2^(n^2))`. [The recursion can run upper-bound `2^(n^2)` times.] |
| 82 | +- **Space Complexity:** `O(n^2)`. [Output matrix is required so an extra space of size `n*n` is needed.] |
| 83 | + |
| 84 | +--------------------------------------------------------------- |
| 85 | +## 🖋️ Author |
| 86 | +**Code contributed by, _Abhishek Sharma_, 2021 [@abhisheks008](github.com/abhisheks008)** |
| 87 | + |
| 88 | +[](https://www.python.org/) |
0 commit comments