-2
私はcでsudokuソルバを作ったが、私の再帰関数は停止よりも最初の行でしか機能しない。それはゼロの値を持つ最初の要素を探し、次にそれを埋めて、次の値を探し、それを別の解決策で満たします。それが停止する最初の行を解決した後、どのように私はcanSolve
、ボード全体を通過する再帰関数... sudokuGrid
もグローバルです。Sudoku 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];
}
}
}
それがないものを見るためにデバッガを使用してください。または、コードが「精神的な運動」として行うことを実行します。あなたは確かにいくつかのエラーをキャッチします。 –