i have wrote a method that calculates number of paths from a given cell in a 2-dimension array, to a given destination cell, but for some reason it returns an incorrect answer, any thoughts?
private static int numParths(int [][] mat, int x1, int y1, int x2, int y2)
{
if(x1<0 || x1 >mat.length-1 || y1<0 || y1>mat.length-1)
return 0;
if(x1 == x2 && y1 == y2){
System.out.println("1");
return 1;
}
if(mat[x1][y1]==-1)
return 0;
mat[x1][y1]=-1;
return numParths(mat, x1, y1+1, x2, y2) + numParths(mat, x1-1, y1, x2, y2) + numParths(mat, x1+1, y1, x2, y2) + numParths(mat, x1, y1-1, x2, y2);
}
public static void main (String[]args){
int [][] mat={{1,2,3,4},{1,2,3,4},{1,2,3,4},{1,2,3,4}};
System.out.println(numParths(mat, 0,1,2,3));
}