diff --git a/.checkMatrix.c.un~ b/.checkMatrix.c.un~ new file mode 100644 index 0000000..3243d02 Binary files /dev/null and b/.checkMatrix.c.un~ differ diff --git a/.outPut.c.un~ b/.outPut.c.un~ new file mode 100644 index 0000000..345b43e Binary files /dev/null and b/.outPut.c.un~ differ diff --git a/a.out b/a.out new file mode 100755 index 0000000..eb6cafa Binary files /dev/null and b/a.out differ diff --git a/checkMatrix.c b/checkMatrix.c index 15c74a9..65196c0 100644 --- a/checkMatrix.c +++ b/checkMatrix.c @@ -10,24 +10,24 @@ int checkMatrix(int matrixArr[9][9], int isPrintInfo){ rowCnt[i][matrixArr[i][j]]++; if(rowCnt[i][matrixArr[i][j]]>1){ if(isPrintInfo){ - printf("\nFalse:Invalid initial Sudoku matrix!"); - printf("\nThe number %d in the row %d has been used!",matrixArr[i][j],i+1); + printf("\nFalse:Invalid initial Sudoku matrix!\n"); + printf("\nThe number %d in the row %d has been used!\n",matrixArr[i][j],i+1); } return 0; } colCnt[j][matrixArr[i][j]]++; if(colCnt[j][matrixArr[i][j]]>1){ if(isPrintInfo){ - printf("\nFalse:Invalid initial Sudoku matrix!"); - printf("\nThe number %d in the col %d has been used!",matrixArr[i][j],j+1); + printf("\nFalse:Invalid initial Sudoku matrix!\n"); + printf("\nThe number %d in the col %d has been used!\n",matrixArr[i][j],j+1); } return 0; } bloCnt[i/3*3+j/3][matrixArr[i][j]]++; if(bloCnt[i/3*3+j/3][matrixArr[i][j]]>1){ if(isPrintInfo){ - printf("\nFalse:Invalid initial Sudoku matrix!"); - printf("\nThe number %d in the block %d has been used!",matrixArr[i][j],i/3*3+j/3+1); + printf("\nFalse:Invalid initial Sudoku matrix!\n"); + printf("\nThe number %d in the block %d has been used!\n",matrixArr[i][j],i/3*3+j/3+1); } return 0; } @@ -35,7 +35,7 @@ int checkMatrix(int matrixArr[9][9], int isPrintInfo){ } } if(isPrintInfo){ - printf("\nTrue:Valid initial Sudoku matrix!"); + printf("\nTrue:Valid initial Sudoku matrix!\n"); } return 1; } @@ -59,9 +59,9 @@ int checkMatrix(int matrixArr[9][9], int isPrintInfo){ // {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); // checkMatrix(board1,1); // return 0; -// } \ No newline at end of file +// } diff --git a/checkMatrix.c~ b/checkMatrix.c~ new file mode 100644 index 0000000..7decbfb --- /dev/null +++ b/checkMatrix.c~ @@ -0,0 +1,67 @@ +#include "myMatrix.h" + +int checkMatrix(int matrixArr[9][9], int isPrintInfo){ + int rowCnt[9][10]={0}; + int colCnt[9][10]={0}; + int bloCnt[9][10]={0}; + for(int i=0;i<9;i++){ + for(int j=0;j<9;j++){ + if(matrixArr[i][j]!=0){ + rowCnt[i][matrixArr[i][j]]++; + if(rowCnt[i][matrixArr[i][j]]>1){ + if(isPrintInfo){ + printf("\nFalse:Invalid initial Sudoku matrix!\n"); + printf("\nThe number %d in the row %d has been used!\n",matrixArr[i][j],i+1); + } + return 0; + } + colCnt[j][matrixArr[i][j]]++; + if(colCnt[j][matrixArr[i][j]]>1){ + if(isPrintInfo){ + printf("\nFalse:Invalid initial Sudoku matrix!\n"); + printf("\nThe number %d in the col %d has been used!\n",matrixArr[i][j],j+1); + } + return 0; + } + bloCnt[i/3*3+j/3][matrixArr[i][j]]++; + if(bloCnt[i/3*3+j/3][matrixArr[i][j]]>1){ + if(isPrintInfo){ + printf("\nFalse:Invalid initial Sudoku matrix!\n"); + printf("\nThe number %d in the block %d has been used!\n",matrixArr[i][j],i/3*3+j/3+1); + } + return 0; + } + } + } + } + if(isPrintInfo){ + printf("\nTrue:Valid initial Sudoku matrix!\n"); + } + return 1; +} + +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}, + {8, 0, 0, 0, 6, 0, 0, 0, 3}, + {4, 0, 0, 8, 0, 3, 0, 0, 1}, + {7, 0, 0, 0, 2, 0, 0, 0, 6}, + {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}}; + int board1[9][9] = {{8, 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}, + {8, 0, 0, 0, 6, 0, 0, 0, 3}, + {4, 0, 0, 8, 0, 3, 0, 0, 1}, + {7, 0, 0, 0, 2, 0, 0, 0, 6}, + {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); + checkMatrix(board1,1); + return 0; +} diff --git a/fillMatrix.exe b/fillMatrix.exe deleted file mode 100644 index e39d7e0..0000000 Binary files a/fillMatrix.exe and /dev/null differ diff --git a/outPut.c b/outPut.c index 5ac757b..c28a937 100644 --- a/outPut.c +++ b/outPut.c @@ -4,15 +4,22 @@ void matrixOutput(int matrixArr[9][9]) { for (int i = 0; i < 9; i++) { + if(i % 3 == 0) + printf("|-----------------------|\n"); for (int j = 0; j < 9; j++) { + if(j % 3 == 0){ + printf("| "); + } printf("%d", matrixArr[i][j]); if (j != 8) printf(" "); + else + printf(" |"); } - if (i != 8) - printf("\n"); + printf("\n"); } + printf("|-----------------------|\n"); } // int main() @@ -28,4 +35,4 @@ void matrixOutput(int matrixArr[9][9]) // {3, 4, 5, 2, 8, 6, 1, 7, 9}}; // matrixOutput(board); // return 0; -// } \ No newline at end of file +// } diff --git a/outPut.c~ b/outPut.c~ new file mode 100644 index 0000000..e10505f --- /dev/null +++ b/outPut.c~ @@ -0,0 +1,38 @@ +#include "myMatrix.h" + +void matrixOutput(int matrixArr[9][9]) +{ + for (int i = 0; i < 9; i++) + { + if(i % 3 == 0) + printf("|-----------------------|\n"); + for (int j = 0; j < 9; j++) + { + if(j % 3 == 0){ + printf("| "); + } + printf("%d", matrixArr[i][j]); + if (j != 8) + printf(" "); + else + printf(" |"); + } + printf("\n"); + } + printf("|-----------------------|\n"); +} + +int main() +{ + int board[9][9] = {{5, 3, 4, 6, 7, 8, 9, 1, 2}, + {6, 7, 2, 1, 9, 5, 3, 4, 8}, + {1, 9, 8, 3, 4, 2, 5, 6, 7}, + {8, 5, 9, 7, 6, 1, 4, 2, 3}, + {4, 2, 6, 8, 5, 3, 7, 9, 1}, + {7, 1, 3, 9, 2, 4, 8, 5, 6}, + {9, 6, 1, 5, 3, 7, 2, 8, 4}, + {2, 8, 7, 4, 1, 9, 6, 3, 5}, + {3, 4, 5, 2, 8, 6, 1, 7, 9}}; + matrixOutput(board); + return 0; +}