Skip to content

Instantly share code, notes, and snippets.

@eyalcohen4
Created June 26, 2017 19:23
Show Gist options
  • Save eyalcohen4/d981feca7a95b0e59f2770daf83e2c5f to your computer and use it in GitHub Desktop.
Save eyalcohen4/d981feca7a95b0e59f2770daf83e2c5f to your computer and use it in GitHub Desktop.
public static int numPaths (int[][] mat, int x1, int y1, int x2, int y2) {
return numPaths(mat, x1, y1, x2, y2, 0);
}
private static int numPaths(int[][] mat, int x1, int y1, int x2, int y2, int counter) {
int tmp;
if (x1 < 0 || x2 < 0 || y1 < 0 || y2 < 0) {
return 0;
} else if (x1 >= mat.length || x2 >= mat.length || y1 >= mat.length || y2 >= mat.length) {
return 0;
} else if (mat[x1][y1] == -1 || x1 > y1) {
return 0;
}
if (x1 == x2 && y1 == y2) {
return 1;
} else {
tmp = mat[x1][y1];
mat[x1][y1] = -1;
counter = numPaths(mat, x1 + 1, y1, x2, y2, counter) +
numPaths(mat, x1 - 1, y1, x2, y2, counter) +
numPaths(mat, x1, y1 + 1, x2, y2, counter) +
numPaths(mat, x1, y1 - 1, x2, y2, counter);
mat[x1][y1] = tmp;
return counter;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment