#include int checkRows(int matrix[9][9]) { for (int i = 0; i < 9; i++) { int count[10] = {0}; for (int j = 0; j < 9; j++) { if (matrix[i][j] >= 1 && matrix[i][j] <= 9) { if (count[matrix[i][j]] == 1) { return 0; } count[matrix[i][j]]++; } } } return 1; } int checkColumns(int matrix[9][9]) { for (int j = 0; j < 9; j++) { int count[10] = {0}; for (int i = 0; i < 9; i++) { if (matrix[i][j] >= 1 && matrix[i][j] <= 9) { if (count[matrix[i][j]] == 1) { return 0; } count[matrix[i][j]]++; } } } return 1; } int checkSubMatrices(int matrix[9][9]) { for (int blockRow = 0; blockRow < 3; blockRow++) { for (int blockCol = 0; blockCol < 3; blockCol++) { int count[10] = {0}; for (int i = blockRow * 3; i < blockRow * 3 + 3; i++) { for (int j = blockCol * 3; j < blockCol * 3 + 3; j++) { if (matrix[i][j] >= 1 && matrix[i][j] <= 9) { if (count[matrix[i][j]] == 1) { return 0; } count[matrix[i][j]]++; } } } } } return 1; } void printMatrix(int matrix[9][9]) { printf("|--------------------|\n|"); for (int i = 0; i < 9; i++) { for (int j = 0; j < 9; j++) { printf("%d ", matrix[i][j]); if((i+1)%1==0&&(j+1)%3==0) printf("|"); if((i+1)%3==0&&(j+1)%9==0) printf("\n|--------------------|"); if((j+1)==9&&i!=8) printf("\n|"); } } } int main() { int matrix[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} }; printf("The original Sudoku matrix: \n"); printMatrix(matrix); if (checkRows(matrix) && checkColumns(matrix) && checkSubMatrices(matrix)) { printf("True:Valid initial Sudoku matrix!\n"); } else { printf("False:Invalid initial Sudoku matrix!\n"); for (int i = 0; i < 9; i++) { int count[10] = {0}; for (int j = 0; j < 9; j++) { if (matrix[i][j] >= 1 && matrix[i][j] <= 9) { if (count[matrix[i][j]] == 1) { printf("The number %d in the row %d has been used!\n", matrix[i][j], i + 1); } count[matrix[i][j]]++; } } } for (int j = 0; j < 9; j++) { int count[10] = {0}; for (int i = 0; i < 9; i++) { if (matrix[i][j] >= 1 && matrix[i][j] <= 9) { if (count[matrix[i][j]] == 1) { printf("The number %d in the col %d has been used!\n", matrix[i][j], j + 1); } count[matrix[i][j]]++; } } } for (int blockRow = 0; blockRow < 3; blockRow++) { for (int blockCol = 0; blockCol < 3; blockCol++) { int count[10] = {0}; for (int i = blockRow * 3; i < blockRow * 3 + 3; i++) { for (int j = blockCol * 3; j < blockCol * 3 + 3; j++) { if (matrix[i][j] >= 1 && matrix[i][j] <= 9) { if (count[matrix[i][j]] == 1) { printf("The number %d in the block %d has been used!\n", matrix[i][j], blockRow * 3 + blockCol + 1); } count[matrix[i][j]]++; } } } } } } return 0; }