Given the code bellow, I facing the following problem: I have this ArrayList that is supposed to store my solutions in it. On the first if condition, if I print there, It prints the solutions correctly. However, outside this function, it just print a single false matrix. In that in mind I suppose I having problems with referencing the object or something like that.
The solutions ArrayList was declared as an attribute of the class, I was hoping that, when the recursive backtracking found a solution and added to the ArrayList, it would update the attribute and I would be able to use it trough the whole class.
I was reading about the .clone() method, and thinking about passing a copy of solutions on each recursive call, but I not sure exactly how it would work..
public void recursiveBacktracking (ArrayList<boolean[][]> solutions, int x, int y, boolean visited[][]) {
visited[x][y] = true;
if (x == eRow && y == eCol) {
solutions.add(visited);
// If I print here..
}
if (isValidCell(x, y)) {
// go down (x, y)
if (x + 1 < map.getRow() && !visited[x + 1][y]) {
recursiveBacktracking(solutions, x + 1, y, visited);
}
// go up (x, y)
if (x - 1 >= 0 && !visited[x - 1][y]) {
recursiveBacktracking(solutions, x - 1, y, visited);
}
// go right (x, y)
if (y + 1 < map.getCol() && !visited[x][y + 1]) {
recursiveBacktracking(solutions, x, y + 1, visited);
}
// go left (x, y)
if (y - 1 >= 0 && !visited[x][y - 1]) {
recursiveBacktracking(solutions, x, y - 1, visited);
}
}
// backtrack from current cell and remove it from current path
visited[x][y] = false;