Backtracking solution
Rat in a Maze Problem - I
Medium Accuracy: 37.73% Submissions: 100k+ Points: 4
Consider a rat placed at (0, 0) in a square matrix of order N * N. It has to reach the destination at (N - 1, N - 1). Find all possible paths that the rat can take to reach from source to destination. The directions in which the rat can move are 'U'(up), 'D'(down), 'L' (left), 'R' (right). Value 0 at a cell in the matrix represents that it is blocked and rat cannot move to it while value 1 at a cell in the matrix represents that rat can be travel through it.
Note: In a path, no cell can be visited more than one time. If the source cell is 0, the rat cannot move to any other cell.

Example 1:

Input:
N = 4
m[][] = {{1, 0, 0, 0},
{1, 1, 0, 1},
{1, 1, 0, 0},
{0, 1, 1, 1}}
Output:
DDRDRR DRDDRR
Explanation:
The rat can reach the destination at
(3, 3) from (0, 0) by two paths - DRDDRR
and DDRDRR, when printed in sorted order
we get DDRDRR DRDDRR.
Example 2:
Input:
N = 2
m[][] = {{1, 0},
{1, 0}}
Output:
-1
Explanation:
No path exists and destination cell is
blocked.
Your Task:
You don't need to read input or print anything. Complete the function printPath() which takes N and 2D array m[ ][ ] as input parameters and returns the list of paths in lexicographically increasing order.
Note: In case of no path, return an empty list. The driver will output "-1" automatically.

Expected Time Complexity: O((3N^2)).
Expected Auxiliary Space: O(L * X), L = length of the path, X = number of paths.

Constraints:
2 ≤ N ≤ 5
0 ≤ m[i][j] ≤ 1

// { Driver Code Starts
// Initial Template for Java

import java.util.*;
class Rat {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int t = sc.nextInt();

        while (t-- > 0) {
            int n = sc.nextInt();
            int[][] a = new int[n][n];
            for (int i = 0; i < n; i++)
                for (int j = 0; j < n; j++) a[i][j] = sc.nextInt();

            Solution obj = new Solution();
            ArrayList<String> res = obj.findPath(a, n);
            Collections.sort(res);
            if (res.size() > 0) {
                for (int i = 0; i < res.size(); i++)
                    System.out.print(res.get(i) + " ");
                System.out.println();
            } else {
                System.out.println(-1);
            }
        }
    }
}
// } Driver Code Ends


// User function Template for Java

// m is the given matrix and n is the order of matrix
class Solution {
    static  ArrayList<String> res;
    static int[][] visited;
    public static void findPaths(int[][] m,int row,int col,String s,int n){
        if(row==n-1 && col==n-1){
           res.add(s);
           return;
        }
        else{
        visited[row][col]=1;
        }
         if(row+1<n && visited[row+1][col]==0 && m[row+1][col]==1){
            findPaths(m,row+1,col,s+"D",n);                         //DOWN
        }
         if(col-1>=0 && visited[row][col-1]==0 && m[row][col-1]==1){
            findPaths(m,row,col-1,s+"L",n);                     //LEFT
        }
         if(col+1<n && visited[row][col+1]==0 && m[row][col+1]==1){
            findPaths(m,row,col+1,s+"R",n);                   //RIGHT
        }
        if(row-1>=0 && visited[row-1][col]==0 && m[row-1][col]==1){
            findPaths(m,row-1,col,s+"U",n);                   //UP
        }
        visited[row][col]=0;
    }
    public static ArrayList<String> findPath(int[][] m, int n) {
        visited = new int[n][n];
        res= new ArrayList<>();
        if(m[0][0]==0){
            return res;
        }
        String s="";
        findPaths(m,0,0,s,n);
        return res;
    }
}
Comments (53)