https://practice.geeksforgeeks.org/problems/rat-in-a-maze-problem/1#
Please check out above link and tell me why cases are getting failed even it is running in my intellij Idea
My solution is given below:
class Solution {
static ArrayList res = new ArrayList<>();
public static ArrayList findPath(int[][] m, int n) {
// Your code here
mazePath(m, 0, 0, “”);
return res;
}
public static void mazePath(int[][] m, int row, int col, String ans) {
if(row < 0 || col < 0 || row >= m.length || col >= m[0].length || m[row][col] == 0) {
return;
}
if(row == m.length-1 && col == m.length-1) {
res.add(ans);
return;
}
m[row][col] = 0;
// U
mazePath(m, row-1, col, ans + "U");
// R
mazePath(m, row, col+1, ans + "R");
// D
mazePath(m, row+1, col, ans + "D");
// L
mazePath(m, row, col-1, ans + "L");
m[row][col] = 1;
}
}