Can someone please review this simple code for printing all paths in a matrix from bottom left to top right.? If an element is 1, it is a "wall" and you cannot take it.
Possible steps: up and right
// Prints possible paths from bottom left to top right
public static void findPath(int n, int i , int j, int[][] mat,ArrayList<Integer> path)
{
if(i==0 && j==n-1)
{
System.out.println();
for(Integer step: path)
{
System.out.print(","+step);
}
}
if(isPossibleStep(i+1,j,n,mat))
{
path.add(i+1);
findPath(n,i+1, j,mat, path);
}
else if(isPossibleStep(i,j-1,n,mat))
{
path.add(j-1);
findPath(n,i, j-1,mat, path);
}
}
// Tells if a given path is possible
public static boolean isPossibleStep(int i, int j, int n,int[][] mat)
{
if(mat[i][j]==1)
return false;
if(i < n && j > 0)
return true;
else
return false;
}
Also, Howhow do I print just 1 possible path and then exit. Seems? It seems like a simple change but I am not able to think of a graceful way of doing it.
Thanks!