I am trying to solve a recursive backtracking problem in which we must start at a given coordinate in a 2D array of ints, and find a path to an edge cell of the array, such that each successive element along the path is less than or equal to the previous one (starting with the given element) I don't know how to backtrack properly if the element in the current cell is equal to the previous element, so I have only been using test cases with different values in each cell. Any help / comments would be appreciated Here is my code so far:
public static boolean canFlowOffMap(int[][] map, int row, int col) {
int current = map[row][col];
return mthd6(map, current, row, col, false);
}
private static int[][] OPTIONS = { { 0, 1 }, { 0, -1 }, { 1, 0 }, { -1, 0 } };
private static boolean mthd6(int[][] map, int current, int r, int c, boolean result) {
if (r == 0 || c == 0 || r == map.length -1 || c == map[0].length -1) {
return true;
} else {
for (int[] option : OPTIONS) {
r += option[0];
c += option[1];
if (map[r][c] < current) {
current = map[r][c];
result = mthd6(map, current, r, c, false);
}
r -= option[0];
c -= option[1];
}
return result;
}
}