We have discussed Backtracking and Knight’s tour problem in Set 1. Let us discuss Rat in a Maze as another example problem that can be solved using Backtracking.

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 destination. The rat can move only in two directions: forward and down.
In the maze matrix, 0 means the block is 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 limited number of moves.

Following is an example maze.

Gray blocks are dead ends (value = 0).

Rat in a Maze

Following is binary matrix representation of the above maze.

                {1, 0, 0, 0}
                {1, 1, 0, 1}
                {0, 1, 0, 0}
                {1, 1, 1, 1}

Following is maze with highlighted solution path.

Rat in a Maze

Following is the solution matrix (output of program) for the above input matrx.

                {1, 0, 0, 0}
                {1, 1, 0, 0}
                {0, 1, 0, 0}
                {0, 1, 1, 1}
 All enteries in solution path are marked as 1.
[ad type=”banner”]

Naive Algorithm
The Naive Algorithm is to generate all paths from source to destination and one by one check if the generated path satisfies the constraints.

while there are untried paths
{
   generate the next path
   if this path has all blocks as 1
   {
      print this path;
   }
}

Backtrackng Algorithm

If destination is reached
    print the solution matrix
Else
   a) Mark current cell in solution matrix as 1. 
   b) Move forward in horizontal direction and recursively check if this 
       move leads to a solution. 
   c) If the move chosen in the above step doesn't lead to a solution
       then move down and check if  this move leads to a solution. 
   d) If none of the above solutions work then unmark this cell as 0 
       (BACKTRACK) and return false.

Implementation of Backtracking solution

[pastacode lang=”java” manual=”%2F*%20Java%20program%20to%20solve%20Rat%20in%20a%20Maze%20problem%20using%0A%20%20%20backtracking%20*%2F%0A%20%0Apublic%20class%20RatMaze%0A%7B%0A%20%20%20%20final%20int%20N%20%3D%204%3B%0A%20%0A%20%20%20%20%2F*%20A%20utility%20function%20to%20print%20solution%20matrix%0A%20%20%20%20%20%20%20sol%5BN%5D%5BN%5D%20*%2F%0A%20%20%20%20void%20printSolution(int%20sol%5B%5D%5B%5D)%0A%20%20%20%20%7B%0A%20%20%20%20%20%20%20%20for%20(int%20i%20%3D%200%3B%20i%20%3C%20N%3B%20i%2B%2B)%0A%20%20%20%20%20%20%20%20%7B%0A%20%20%20%20%20%20%20%20%20%20%20%20for%20(int%20j%20%3D%200%3B%20j%20%3C%20N%3B%20j%2B%2B)%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20System.out.print(%22%20%22%20%2B%20sol%5Bi%5D%5Bj%5D%20%2B%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%22%20%22)%3B%0A%20%20%20%20%20%20%20%20%20%20%20%20System.out.println()%3B%0A%20%20%20%20%20%20%20%20%7D%0A%20%20%20%20%7D%0A%20%0A%20%20%20%20%2F*%20A%20utility%20function%20to%20check%20if%20x%2Cy%20is%20valid%0A%20%20%20%20%20%20%20%20index%20for%20N*N%20maze%20*%2F%0A%20%20%20%20boolean%20isSafe(int%20maze%5B%5D%5B%5D%2C%20int%20x%2C%20int%20y)%0A%20%20%20%20%7B%0A%20%20%20%20%20%20%20%20%2F%2F%20if%20(x%2Cy%20outside%20maze)%20return%20false%0A%20%20%20%20%20%20%20%20return%20(x%20%3E%3D%200%20%26%26%20x%20%3C%20N%20%26%26%20y%20%3E%3D%200%20%26%26%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20y%20%3C%20N%20%26%26%20maze%5Bx%5D%5By%5D%20%3D%3D%201)%3B%0A%20%20%20%20%7D%0A%20%0A%20%20%20%20%2F*%20This%20function%20solves%20the%20Maze%20problem%20using%0A%20%20%20%20%20%20%20Backtracking.%20It%20mainly%20uses%20solveMazeUtil()%0A%20%20%20%20%20%20%20to%20solve%20the%20problem.%20It%20returns%20false%20if%20no%0A%20%20%20%20%20%20%20path%20is%20possible%2C%20otherwise%20return%20true%20and%0A%20%20%20%20%20%20%20prints%20the%20path%20in%20the%20form%20of%201s.%20Please%20note%0A%20%20%20%20%20%20%20that%20there%20may%20be%20more%20than%20one%20solutions%2C%20this%0A%20%20%20%20%20%20%20function%20prints%20one%20of%20the%20feasible%20solutions.*%2F%0A%20%20%20%20boolean%20solveMaze(int%20maze%5B%5D%5B%5D)%0A%20%20%20%20%7B%0A%20%20%20%20%20%20%20%20int%20sol%5B%5D%5B%5D%20%3D%20%7B%7B0%2C%200%2C%200%2C%200%7D%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%7B0%2C%200%2C%200%2C%200%7D%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%7B0%2C%200%2C%200%2C%200%7D%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%7B0%2C%200%2C%200%2C%200%7D%0A%20%20%20%20%20%20%20%20%7D%3B%0A%20%0A%20%20%20%20%20%20%20%20if%20(solveMazeUtil(maze%2C%200%2C%200%2C%20sol)%20%3D%3D%20false)%0A%20%20%20%20%20%20%20%20%7B%0A%20%20%20%20%20%20%20%20%20%20%20%20System.out.print(%22Solution%20doesn’t%20exist%22)%3B%0A%20%20%20%20%20%20%20%20%20%20%20%20return%20false%3B%0A%20%20%20%20%20%20%20%20%7D%0A%20%0A%20%20%20%20%20%20%20%20printSolution(sol)%3B%0A%20%20%20%20%20%20%20%20return%20true%3B%0A%20%20%20%20%7D%0A%20%0A%20%20%20%20%2F*%20A%20recursive%20utility%20function%20to%20solve%20Maze%0A%20%20%20%20%20%20%20problem%20*%2F%0A%20%20%20%20boolean%20solveMazeUtil(int%20maze%5B%5D%5B%5D%2C%20int%20x%2C%20int%20y%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20int%20sol%5B%5D%5B%5D)%0A%20%20%20%20%7B%0A%20%20%20%20%20%20%20%20%2F%2F%20if%20(x%2Cy%20is%20goal)%20return%20true%0A%20%20%20%20%20%20%20%20if%20(x%20%3D%3D%20N%20-%201%20%26%26%20y%20%3D%3D%20N%20-%201)%0A%20%20%20%20%20%20%20%20%7B%0A%20%20%20%20%20%20%20%20%20%20%20%20sol%5Bx%5D%5By%5D%20%3D%201%3B%0A%20%20%20%20%20%20%20%20%20%20%20%20return%20true%3B%0A%20%20%20%20%20%20%20%20%7D%0A%20%0A%20%20%20%20%20%20%20%20%2F%2F%20Check%20if%20maze%5Bx%5D%5By%5D%20is%20valid%0A%20%20%20%20%20%20%20%20if%20(isSafe(maze%2C%20x%2C%20y)%20%3D%3D%20true)%0A%20%20%20%20%20%20%20%20%7B%0A%20%20%20%20%20%20%20%20%20%20%20%20%2F%2F%20mark%20x%2Cy%20as%20part%20of%20solution%20path%0A%20%20%20%20%20%20%20%20%20%20%20%20sol%5Bx%5D%5By%5D%20%3D%201%3B%0A%20%0A%20%20%20%20%20%20%20%20%20%20%20%20%2F*%20Move%20forward%20in%20x%20direction%20*%2F%0A%20%20%20%20%20%20%20%20%20%20%20%20if%20(solveMazeUtil(maze%2C%20x%20%2B%201%2C%20y%2C%20sol))%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20return%20true%3B%0A%20%0A%20%20%20%20%20%20%20%20%20%20%20%20%2F*%20If%20moving%20in%20x%20direction%20doesn’t%20give%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20solution%20then%20%20Move%20down%20in%20y%20direction%20*%2F%0A%20%20%20%20%20%20%20%20%20%20%20%20if%20(solveMazeUtil(maze%2C%20x%2C%20y%20%2B%201%2C%20sol))%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20return%20true%3B%0A%20%0A%20%20%20%20%20%20%20%20%20%20%20%20%2F*%20If%20none%20of%20the%20above%20movements%20work%20then%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20BACKTRACK%3A%20unmark%20x%2Cy%20as%20part%20of%20solution%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20path%20*%2F%0A%20%20%20%20%20%20%20%20%20%20%20%20sol%5Bx%5D%5By%5D%20%3D%200%3B%0A%20%20%20%20%20%20%20%20%20%20%20%20return%20false%3B%0A%20%20%20%20%20%20%20%20%7D%0A%20%0A%20%20%20%20%20%20%20%20return%20false%3B%0A%20%20%20%20%7D%0A%20%0A%20%20%20%20public%20static%20void%20main(String%20args%5B%5D)%0A%20%20%20%20%7B%0A%20%20%20%20%20%20%20%20RatMaze%20rat%20%3D%20new%20RatMaze()%3B%0A%20%20%20%20%20%20%20%20int%20maze%5B%5D%5B%5D%20%3D%20%7B%7B1%2C%200%2C%200%2C%200%7D%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%7B1%2C%201%2C%200%2C%201%7D%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%7B0%2C%201%2C%200%2C%200%7D%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%7B1%2C%201%2C%201%2C%201%7D%0A%20%20%20%20%20%20%20%20%7D%3B%0A%20%20%20%20%20%20%20%20rat.solveMaze(maze)%3B%0A%20%20%20%20%7D%0A%7D%0A” message=”java” highlight=”” provider=”manual”/]

Output: The 1 values show the path for rat

 1  0  0  0 
 1  1  0  0 
 0  1  0  0 
 0  1  1  1
[ad type=”banner”]

Categorized in: