#include #include #include #include void printMatrix(int matrix[9][9]); int isInRow(int matrix[9][9], int row, int num); int isInGroup(int matrix[9][9], int startRow, int startCol, int num); bool checkRows(int matrix[9][9]); bool checkColumns(int matrix[9][9]); bool checkSubMatrices(int matrix[9][9]); bool isSudokuMatrix(int matrix[9][9]); // 打印矩阵 void printMatrix(int matrix[9][9]) { for (int i = 0; i < 9; i++) { if (i == 0 || i == 3 || i == 6) { printf("|-----------------------|\n"); } for (int j = 0; j < 9; j++) { if (j == 0 || j == 3 || j == 6) { printf("| "); } if (matrix[i][j] == 0) { printf(". "); } else { printf("%d ", matrix[i][j]); } if (j == 8) { printf("|\n"); } } if (i == 8) { printf("|-----------------------|\n"); } } } // 检查数字是否在指定行中 int isInRow(int matrix[9][9], int row, int num) { for (int i = 0; i < 9; i++) { if (matrix[row][i] == num) { return 1; } } return 0; } // 检查数字是否在指定的 3x3 子矩阵中 int isInGroup(int matrix[9][9], int startRow, int startCol, int num) { for (int i = 0; i < 3; i++) { for (int j = 0; j < 3; j++) { if (matrix[startRow + i][startCol + j] == num) { return 1; } } } return 0; } // 检查行中数字 1 - 9 是否最多出现一次 bool checkRows(int matrix[9][9]) { for (int i = 0; i < 9; i++) { int count[9] = { 0 }; for (int j = 0; j < 9; j++) { if (matrix[i][j] > 0 && matrix[i][j] <= 9) { int num = matrix[i][j] - 1; if (count[num] > 0) { return false; } count[num]++; } } } return true; } // 检查列中数字 1 - 9 是否最多出现一次 bool checkColumns(int matrix[9][9]) { for (int j = 0; j < 9; j++) { int count[9] = { 0 }; for (int i = 0; i < 9; i++) { if (matrix[i][j] > 0 && matrix[i][j] <= 9) { int num = matrix[i][j] - 1; if (count[num] > 0) { printf("The number %d in the col %d has been used!\n", matrix[i][j], j); return false; } count[num]++; } } } return true; } // 检查 3x3 子矩阵中数字 1 - 9 是否最多出现一次 bool checkSubMatrices(int matrix[9][9]) { for (int row = 0; row < 9; row += 3) { for (int col = 0; col < 9; col += 3) { int count[9] = { 0 }; for (int i = row; i < row + 3; i++) { for (int j = col; j < col + 3; j++) { if (matrix[i][j] > 0 && matrix[i][j] <= 9) { int num = matrix[i][j] - 1; if (count[num] > 0) { printf("The number %d in the block %d has been used!\n", matrix[i][j], (row / 3) * 3 + col / 3); return false; } count[num]++; } } } } } return true; } // 判断矩阵是否满足数独矩阵的定义 bool isSudokuMatrix(int matrix[9][9]) { return checkRows(matrix) && checkColumns(matrix) && checkSubMatrices(matrix); } int main() { printf("The original Sudoku matrix: \n"); srand(time(NULL)); int matrix[9][9] = { 0 }; int numbers[9] = { 1, 2, 3, 4, 5, 6, 7, 8, 9 }; for (int group = 0; group < 3; group++) { int usedNumbers[9] = { 0 }; for (int i = 0; i < 9; i++) { usedNumbers[i] = numbers[i]; } for (int i = 0; i < 9; i++) { int idx = rand() % (9 - i); int temp = usedNumbers[idx]; usedNumbers[idx] = usedNumbers[8 - i]; usedNumbers[8 - i] = temp; } for (int row = 0; row < 3; row++) { int count = 0; while (count < 3) { int num; do { int idx = rand() % 9; num = usedNumbers[idx]; } while (num == 0 || isInRow(matrix, group * 3 + row, num) || isInGroup(matrix, group * 3, 0, num)); int pos; do { pos = rand() % 9; } while (matrix[group * 3 + row][pos] != 0); matrix[group * 3 + row][pos] = num; count++; } } } for (int row = 0; row < 9; row++) { int uniqueNumbers[9] = { 0 }; int uniqueCount = 0; for (int i = 0; i < 9; i++) { if (matrix[row][i] != 0 && !uniqueNumbers[matrix[row][i] - 1]) { uniqueNumbers[matrix[row][i] - 1] = 1; uniqueCount++; } } if (uniqueCount > 3) { while (uniqueCount > 3) { int idx = rand() % 9; if (matrix[row][idx] != 0 && uniqueNumbers[matrix[row][idx] - 1]) { matrix[row][idx] = 0; uniqueCount--; } } } else if (uniqueCount < 3) { while (uniqueCount < 3) { int num; do { num = rand() % 9 + 1; } while (uniqueNumbers[num - 1]); int idx; do { idx = rand() % 9; } while (matrix[row][idx] != 0); matrix[row][idx] = num; uniqueNumbers[num - 1] = 1; uniqueCount++; } } } printMatrix(matrix); bool result = isSudokuMatrix(matrix); if (result) { printf("True:Valid initial Sudoku matrix!\n"); } else { printf("False:Invalid initial Sudoku matrix!\n"); } return 0; }