-2

This link has a Backtracking implementation of the Sudoku Solver algorithm. Notice how line number 42, reverts the value initially assigned to a cell, to another value, in case the initially assigned value did not give a valid output.

However, I don't understand how merely changing the value of that cell alone is sufficient. This call could've triggered many other calls, and since arrays (matrices) are passed by memory (reference), it does not keep a copy of the matrix (grid[N][N]) at every invocation of the recursive function, and so changes till the base case of the recursion will reflect even in the first frame of recursion by the time it returns back.

According to me, just before calling the recursive function, you should make a temporary copy of grid[N][N] and restore it as soon as the call gets returned, and before the next function in the same frame is called.

Something like

for (int num = 1; num <= N; num++)
    {
        // if looks promising
        if (isSafe(grid, row, col, num))
        {
            //save grid state
            int[][] temp = new int[N][N];
            save(temp,grid); //copy all values from grid to temp             

            // make tentative assignment
            grid[row][col] = num;

            // return, if success, yay!
            if (SolveSudoku(grid))
                return true;

            //restore grid state           
            restore(temp,grid); //copy all values from temp back to grid

            // failure, unmake & try again
            grid[row][col] = UNASSIGNED;
        }
    }

Please help me understand this detail.

saltandwater
  • 791
  • 2
  • 9
  • 25
  • 3
    It is not possible to provide an authoritative answer without examining the rest of the code; but stackoverflow.com is not really a place for this. Although it does appear that the back copy of the original grid is unnecessary, since the `UNASSIGNED` assignment appears to revert the move, again, can't really say with 100% certainty. But this is exactly what a debugger is for. Use this very useful tool you will find on your computer to step through this code, one line at a time, and see how it works, all by yourself, without having to ask strangers on the Internet for help. – Sam Varshavchik Jul 18 '16 at 01:54
  • 2
    At each level of recursion the code assigns one cell and reverts one cell. Storing the entire grid is not necessary because they original state will be restored one cell at a time after each recursive call returns. You could say that the complete state is stored on the call stack, in the local variables row and col, which will be saved at each recursion level. – samgak Jul 18 '16 at 02:10

1 Answers1

3

It is saving state: Each recursive call saves state on the call stack.

The unassigned parts of the grid are modified until a valid solution is found. Once it is, all the stacked function calls are terminated (lines 38 and 39), leaving the grid[][] in its solved state. If not it restores the current cell to its UNASSIGNED value and tries the next possible value.

This is a brute-force solver. You might want to google around that as well.

Hope this helps.

Dúthomhas
  • 8,200
  • 2
  • 17
  • 39