简体   繁体   English

数独 C 递归函数无法正常工作

[英]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.我在 c 中做了一个数独求解器,我的递归函数只适用于第一行而不是停止。 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.在它解决了它停止的第一行之后,我将如何启用canSolve ,递归函数遍历整个板...... sudokuGrid也是全局的。

//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{

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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