2016-04-29 84 views
-2

我在c中制作了一个数独求解器,而递归函数只对第一行起作用,而不是停止。它寻找值为零的第一个元素并填充它,然后查找下一个值,并用另一个解决方案填充它。在它解决了第一行之后,我将如何启用canSolve,递归功能贯穿整个董事会...... sudokuGrid也是全球性的。数独C递归函数无法正常工作

//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]; 
     } 
    } 
} 
+1

使用调试器来看看会发生什么。或者阅读代码做为“精神锻炼”的内容。你一定会发现一些错误。 –

回答

0

将递归与全局变量结合使用通常不是一个好主意。如果你的求解器做出错误的选择并需要回溯,它还需要将板恢复到它做出该选择时的状态。

那么你需要做什么?

我猜你需要尝试下一个候选之前擦拭董事会其他成员:

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