0

I'm implementing a backtracking algorithm using Java to find a solution to a given Soduko board. I'm trying to modify the algorithm to instead give all possible solutions, however the modified algorithm is not working. See details below:

public static void solveSoduko(int[][] grid, int row, int column) {
    
    // base case
    if(row == grid.length - 1 && column == grid.length) {
        arrayPrint(grid);
        return;
    }
    
    if(column == grid.length) {
        row ++;
        column = 0;
    } 
    
    
    if(grid[row][column] != 0) {
        solveSoduko(grid, row, column + 1);
    }
    
    
    for(int i = 1; i <= 9; i++) {
        
        if(isValid(grid, row, column, i)){
            grid[row][column] = i;
            solveSoduko(grid, row, column + 1);
            grid[row][column] = 0;
        }
    }
    

}

The algorithm keeps running indefinitely regardless of the given Soduko grid (i tested it with ones that have a unique solution). The algorithm also alters non empty cell. How can i modify the algorithm.

matt
  • 515,959
  • 87
  • 875
  • 1,141

0 Answers0