Matrix bottom left to top right 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&lt;Integer&gt; path)
    {
        if(i==0 &amp;&amp; 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 &lt; n &amp;&amp; j &gt; 0)
            return true;
        else
            return false;
    }


Also, how do I print just 1 possible path and then exit? It seems like a simple change but I am not able to think of a graceful way of doing it.
You can refactor the findPath logic somewhat and reduce the indent level while preserving the method's behavior:

// Prints possible paths from bottom left to top right
public static void findPath(int n, int i , int j, int[][] mat, ArrayList&lt;Integer&gt; path)
{
    if(i == 0 &amp;&amp; j == n - 1)
    {
        System.out.println();
        for(Integer step: path)
        {
            System.out.print("," + step);
        }
    }

    boolean rightStep = isPossibleStep(i + 1, j, n, mat);                
    if(!isPossibleStep(i, j - 1, n, mat) &amp;&amp; !rightStep)
        return;

    path.add(rightStep ? ++i : --j);
    findPath(n, i, j, mat, path);    
}


isPossibleStep can also be simplified by use of short-circuit logic. The following expresses the intent more directly with the benefit of being terse:

public static boolean isPossibleStep(int i, int j, int n, int[][] mat)
{
    return mat[i][j] != 1 
        &amp;&amp; i &lt; n 
        &amp;&amp; j &gt; 0;
}