Rat In a Maze All Paths

Moderate
0/80
Average time to solve is 40m
profile
Contributed by
265 upvotes
Asked in companies
VisaExpedia GroupHexaware Technologies

Problem statement

You are given a 'N' * 'N' maze with a rat placed at 'MAZE[0][0]'. Find and print all paths that rat can follow to reach its destination i.e. 'MAZE['N' - 1]['N' - 1]'. Rat can move in any direc­tion ( left, right, up and down).

Value of every cell in the 'MAZE' can either be 0 or 1. Cells with value 0 are blocked means the rat can­not enter into those cells and those with value 1 are open.

Detailed explanation ( Input/output format, Notes, Images )
Input Format:
The first line of input contains an integer 'N' representing the dimension of the maze.

The next 'N' lines of input contain 'N' space-separated integers representing the type of the cell.
Output Format :
For each test case, return the path from the start position to the destination position and only cells that are part of the solution path should be 1, rest all cells should be 0.

Output for every test case will be printed in a separate line.
Note:
You do not need to print anything, it has already been taken care of. Just implement the given function.
Constraints:
1 <= N <= 10
0 <= MAZE[i][j] <=1

Where 'MAZE[i][j]' denotes the value in the cell of 'MAZE'.

Time Limit: 1 sec
Sample Input 1 :
3
1 0 1
1 0 1
1 1 1
Sample Output 1 :
1 0 0 1 0 0 1 1 1 
Explanation for Sample Output 1:
Only 1 path is possible which contains coordinate < (1,1), (2,1), (3,1), (3,2) and (3,3) >

So our path matrix will look like this:

1 0 0
1 0 0
1 1 1

Which is returned from left to right and then top to bottom in one line.
Sample Input 2 :
2
1 0
0 1
Sample Output 2 :
 [Blank]
Explanation for Sample Output 2:
As no path is possible to the last cell, a blank vector will be returned and nothing is printed.
Hint

Try every possible path using recursion and backtracking.

Approaches (1)
Backtracking Approach

Initialize, all the cells of the solution matrix used to print the path matrix to 0. First, you cannot make use of the existing maze to print the solution maze as you have to distinguish b/w 1 of maze or 1 of ‘SOLUTION matrix.

 

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. But in case it reaches the destination print the current ‘SOLUTION’ matrix.

 

The steps are as follows:

 

f('i', ‘j'):

  • If rat reaches the destination, print the solution matrix.
  • Else:
    • If not valid('MAZE[i][j]' ) return
    • Else ‘SOLUTION[i][j]' =1
    • f(i-1,j) ;  f(i+1,j) , f(i, j-1) , f(i,j+1) // Recursive calls
    • ‘SOLUTION[i][j]' = 0    // Backtracking
Time Complexity

O(4 ^ (N ^ 2)), Where ‘N’ is the dimension of the matrix.

 

Since we can call 4 recursive calls from each cell in ‘N’ * ‘N’ board, Thus the time complexity will be O(4 ^ (N ^ 2)).

Space Complexity

O(N * N), Where ‘N’ is the dimension of the matrix.

  

Since the extra space used to create 'SOLUTION matrix of size N * N. Thus the space complexity will be O(N * N).

Video Solution
Unlock at level 3
(75% EXP penalty)
Code Solution
(100% EXP penalty)
Rat In a Maze All Paths
Full screen
Console