diff --git a/.vscode/tasks.json b/.vscode/tasks.json new file mode 100644 index 0000000..26b914f --- /dev/null +++ b/.vscode/tasks.json @@ -0,0 +1,28 @@ +{ + "tasks": [ + { + "type": "cppbuild", + "label": "C/C++: gcc.exe 生成活动文件", + "command": "C:\\Users\\GaoShiBo\\software\\mingw64\\bin\\gcc.exe", + "args": [ + "-fdiagnostics-color=always", + "-g", + "${file}", + "-o", + "${fileDirname}\\${fileBasenameNoExtension}.exe" + ], + "options": { + "cwd": "${fileDirname}" + }, + "problemMatcher": [ + "$gcc" + ], + "group": { + "kind": "build", + "isDefault": true + }, + "detail": "调试器生成的任务。" + } + ], + "version": "2.0.0" +} \ No newline at end of file diff --git a/fillMatrix.c b/fillMatrix.c index d0e973b..4083d21 100644 --- a/fillMatrix.c +++ b/fillMatrix.c @@ -1,10 +1,31 @@ #include "myMatrix.h" -int fillMatrix(){ - //todo +int fillMatrix(int matrixArr[9][9], int row, int col){ + if(row==9){ + return 1; + } + if(col==9){ + return fillMatrix(matrixArr,row+1,0); + } + if(matrixArr[row][col]!=0){ + return fillMatrix(matrixArr, row, col+1); + } + for(int i=1;i<=9;i++){ + matrixArr[row][col]=i; + if(checkMatrix(matrixArr,0)){ + if(fillMatrix(matrixArr,row,col+1)){ + return 1; + } + matrixArr[row][col]=0; + }else{ + matrixArr[row][col]=0; + } + } + return 0; } -int main(){ +int main() +{ int board0[9][9] = {{5, 3, 0, 0, 7, 0, 0, 0, 0}, {6, 0, 0, 1, 9, 5, 0, 0, 0}, {0, 9, 8, 0, 0, 0, 0, 6, 0}, @@ -32,12 +53,22 @@ int main(){ {0, 6, 0, 0, 0, 0, 2, 8, 0}, {0, 0, 0, 4, 1, 9, 0, 0, 5}, {0, 0, 0, 0, 8, 0, 0, 7, 9}}; - printf("The original Sudoku matrix: \n"); - matrixOutput(board1); - if(checkMatrix(board1,1)){ - fillMatrix(); - }else{ + matrixOutput(board0); + if (checkMatrix(board0, 1)) + { + if (fillMatrix(board0, 0, 0)) + { + printf("\nThe solution of Sudoku matrix:\n"); + matrixOutput(board0); + } + else + { + printf("\nNo solution!"); + } + } + else + { printf("\nNo solution!"); } return 0; diff --git a/fillMatrix.exe b/fillMatrix.exe new file mode 100644 index 0000000..e39d7e0 Binary files /dev/null and b/fillMatrix.exe differ