简体   繁体   中英

Sudoku C recursive function not working properly

I made a sudoku solver in c, and my recursive function only works for the first row than stops. It looks for the first element with the value of zero and fills it up then looks for the next value, and fills it with another solution. After it solves the first row it stops, how would I enable canSolve , the recursive function to go through the whole board... sudokuGrid is also global.

//this function makes a list of possible values for an empty cell, candidateList.
//candidateList is global so it  the list can be accesed by the recursive solver function:
//canSolve();
void verifyCell(int x, int y){
    int i;
    int j;

    for (i=0; i<9; i++){
        candidateList[i] = 0;
    }
    //Rows
    for (j=0; j<cols; j++){
        if(sudokuGrid[x][j] != 0){
           candidateList[sudokuGrid[x][j] - 1] = 1;
        }
    }
    //Columns
   for (i=0; i<rows; i++){
        if(sudokuGrid[i][y] != 0){
           candidateList[sudokuGrid[i][y] - 1] = 1;
        }
    }
    //blocks
    int startRow = ((x/3)*3);
    int startCol = ((x/3)*3);
    for (i = startRow; i<startRow+3; i++){
        for(j=startCol;j<startCol+3;j++){
            if(sudokuGrid[i][j] != 0){
               candidateList[sudokuGrid[i][j] - 1] = 1;
            }
       }
    }
    for(i = 0; i<9;i++){
        if (candidateList[i]==0){
            candidateList[i] = i+1;
        }else{
            candidateList[i] = 0;
        }
    }
    return;
}

 canSolve(){
    int i;
    int j;
    int x;
    int y;
    x= 0;
    y = 0;
    //gridToString();
    if (isSolved()==1){
        printf("Great this grid is Solved!\n");
        gridToString();
        return;
    }
    for(i=0;i<rows;i++){
        for(j=0;j<cols;j++){
            if(sudokuGrid[i][j]==0){
                x=i;
                y=j;
            }
        }
        goto rest;
    }
    printf("(%d, %d)", x, y);
    rest:;
    verifyCell(x,y);

    for(i = 0; i<rows; i++){
        printf("%d", candidateList[i]);
        if (candidateList[i] != 0){
            sudokuGrid[x][y]=candidateList[i];
            gridToString();
            canSolve();//By the end of solving the first row it stops
        }else{
            sudokuGrid[x][y]=sudokuGrid[x][y];
        }
    }
}

Using recursion in combination with global variables is usually not a good idea. If your solver makes a wrong choice and needs to backtrack it also needs to restore the board to the state it was when it made that choice.

So what do you need to do?

I guess you need to wipe the rest of the board before trying the next candidate:

        canSolve();//By the end of solving the first row it stops
        wipeRestOfBoard(x,y);  // Put 0 in sudokuGrid[x,y] and subsequent positions
    }else{

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM