diff --git a/program4.c b/program4.c new file mode 100644 index 0000000..10c9de9 --- /dev/null +++ b/program4.c @@ -0,0 +1,182 @@ +#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]); + } + printf("|\n"); + } + printf("|-----------------------|\n"); + } +} + + +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; +} + +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; +} + +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"); + 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} + }; + 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; +}