diff --git a/program4_random.c b/program4_random.c new file mode 100644 index 0000000..8df7bb4 --- /dev/null +++ b/program4_random.c @@ -0,0 +1,285 @@ +#include +#include +#include +#include + +// 打印数独矩阵 +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; +} + +// 判断数字是否在某行中已被使用 +bool usedInRow(int matrix[9][9], int row, int num) { + for (int col = 0; col < 9; col++) { + if (matrix[row][col] == num) { + return true; + } + } + return false; +} + +// 判断数字是否在某列中已被使用 +bool usedInCol(int matrix[9][9], int col, int num) { + for (int row = 0; row < 9; row++) { + if (matrix[row][col] == num) { + return true; + } + } + return false; +} + +// 判断数字是否在某 3x3 子矩阵中已被使用 +bool usedInBox(int matrix[9][9], int boxStartRow, int boxStartCol, int num) { + for (int row = 0; row < 3; row++) { + for (int col = 0; col < 3; col++) { + if (matrix[row + boxStartRow][col + boxStartCol] == num) { + return true; + } + } + } + return false; +} + +// 判断在特定位置放置特定数字是否安全 +bool isSafe(int matrix[9][9], int row, int col, int num) { + return!usedInRow(matrix, row, num) && !usedInCol(matrix, col, num) && !usedInBox(matrix, row - row % 3, col - col % 3, num); +} + +// 查找空位置 +bool findEmptyLocation(int matrix[9][9], int* row, int* col) { + for (*row = 0; *row < 9; (*row)++) { + for (*col = 0; *col < 9; (*col)++) { + if (matrix[*row][*col] == 0) { + return true; + } + } + } + return false; +} + +// 解决数独问题 +bool solveSudoku(int matrix[9][9]) { + int row, col; + if (!findEmptyLocation(matrix, &row, &col)) { + return true; // 数独已解决 + } + for (int num = 1; num <= 9; num++) { + if (isSafe(matrix, row, col, num)) { + matrix[row][col] = num; + if (solveSudoku(matrix)) { + return true; + } + matrix[row][col] = 0; // 回溯 + } + } + return false; +} + +// 检查行是否符合数独规则 +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; +} + +// 检查列是否符合数独规则 +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) { + return false; + } + count[num]++; + } + } + } + return true; +} + +// 检查 3x3 子矩阵是否符合数独规则 +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) { + 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 valid = isSudokuMatrix(matrix); + if (valid) { + printf("True:Valid initial Sudoku matrix!\n"); + if (solveSudoku(matrix)) { + printf("The solution of Sudoku matrix:\n"); + printMatrix(matrix); + } + else { + printf("No solution!\n"); + } + } + else { + printf("False:Invalid initial Sudoku matrix!\n"); + 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); + break; + } + count[num]++; + } + } + } + printf("No solution!\n"); + } + return 0; +} \ No newline at end of file