-2

Here is my C sudoku solver using recursive backtracing, with help code from online. The program is small, and branches of the puzzle are stored on the stack and it moves forward adding new puzzle's, and if it fails to find a solution for a position it returns to the previous branch and continues.

While this works for finding 1 solution, I'm trying to adapt it to print every solution. Where the else clause /* SOLUTION FOUND AS NO MORE EMPTY BLOCKS! */ is what ends the function, I've tried returning as sudokuSolver() with various (rows,cols) but can't seem to get to continue correctly to print all solutions.

Code:

#include <stdio.h>
#include <string.h>

int isAvailable(int puzzle[][9], int row, int col, int num)
{
    int rowStart = (row/3) * 3;
    int colStart = (col/3) * 3;
    int i, j;

    for(i=0; i<9; ++i) {
        if (puzzle[row][i] == num)
            return 0;
        if (puzzle[i][col] == num)
            return 0;
        if (puzzle[rowStart + (i%3)][colStart + (i/3)] == num)
            return 0;
    }
    return 1;
}

int solveSudoku(int puzzle[][9], int row, int col)
{
    int i;
    if (row<9 && col<9) {
        if(puzzle[row][col] != 0) {
            if ((col+1) < 9)
                return solveSudoku(puzzle, row, col+1);
            else if ((row+1) < 9)
                return solveSudoku(puzzle, row+1, 0);
            else   /*SOLUTION FOUND AS NO MORE EMPTY BLOCKS!*/
                return 1;
        }
        else {
            for(i=0; i<9; ++i) {

                if(isAvailable(puzzle, row, col, i+1)) {
                    puzzle[row][col] = i+1;

                    if ((col+1) < 9){
                        if (solveSudoku(puzzle, row, col +1))
                            return 1;
                        else
                            puzzle[row][col] = 0;
                    }
                    else if ((row+1) < 9) {
                        if (solveSudoku(puzzle, row+1, 0))
                            return 1;
                        else
                            puzzle[row][col] = 0;
                    }
                    else
                        return 1;
                }

            }
        }
        return 0;
    }
    else {
        return 1;
    }
}

int main()
{
    int sudoku_arr[9][9] = {
        {8,0,0,6,0,0,9,0,5},
        {0,0,0,0,0,0,0,0,0},
        {0,0,0,0,2,0,3,1,0},
        {0,0,7,3,1,8,0,6,0},
        {2,4,0,0,0,0,0,7,3},
        {0,0,0,0,0,0,0,0,0},
        {0,0,2,7,9,0,1,0,0},
        {5,0,0,0,8,0,0,3,6},
        {0,0,3,0,0,0,0,0,0}
    };

    if (solveSudoku(sudoku_arr, 0, 0)) {         //Solve sudoku puzzle
        printf("\nSudoku solved:\n");

        int row, col;
        for(row=0; row<9; row++) {
            for(col=0; col<9; col++) {
                printf("%d ", sudoku_arr[row][col]);
            }
            printf("\n");
        }

        return 0;
    }
    else {
        printf("\nNo sudoku solution");
        return -1;
    }
}

Now while this solves with the first solution:

Sudoku solved:
8 1 4 6 3 7 9 2 5 
3 2 5 1 4 9 6 8 7 
7 9 6 8 2 5 3 1 4 
9 5 7 3 1 8 4 6 2 
2 4 1 9 5 6 8 7 3 
6 3 8 2 7 4 5 9 1 
4 6 2 7 9 3 1 5 8 
5 7 9 4 8 1 2 3 6 
1 8 3 5 6 2 7 4 9 

There are many other possible solutions i.e. 814637925325149687796825314957318462241956873638274591462793158579481236183562749 814637925325941687796825314957318462241569873638472591462793158579184236183256749 834671925125839647796425318957318462241956873368247591682793154579184236413562789 834671925125839647796524318957318462241956873368247591682793154519482736473165289 834671925125839647796524318957318462241965873368247591682793154519482736473156289

Astralux
  • 51
  • 4
  • Print the solution from `solveSudoku`, not `main`. (Or store it in an array of solutions.) – Sneftel Jan 07 '15 at 18:07
  • Okay I can change the way it prints, but that doesn't solve the backtracing of the puzzle to continue looking for more solutions. – Astralux Jan 07 '15 at 18:34

1 Answers1

0

The sudoku_arr is passed by reference. When you set a value inside solveSudoku () function, you are overwriting the original array and hence when it tries to backtrack, there are no more locations with 0 value on array.

What you really want to do is to reset the value to 0 in solveSudoku () before you return 1;. Also, you would want to print the solution as soon as there are no empty locations left before returning.

This might be helpful.