From 9cf67e9df69ffabab6460f937a59b31a81c17bb9 Mon Sep 17 00:00:00 2001 From: pmfsq5yrv <1159971203@qq.com> Date: Sat, 2 Nov 2024 18:44:34 +0800 Subject: [PATCH] =?UTF-8?q?=E6=9D=A8=E5=AD=90=E7=BE=BD=E7=9A=84=E4=BB=A3?= =?UTF-8?q?=E7=A0=81=E5=AE=9E=E7=8E=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- YangTsePrograme.c | 146 ++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 146 insertions(+) create mode 100644 YangTsePrograme.c diff --git a/YangTsePrograme.c b/YangTsePrograme.c new file mode 100644 index 0000000..82f6f2a --- /dev/null +++ b/YangTsePrograme.c @@ -0,0 +1,146 @@ +#include +#include +#include +#include + +bool isValid(int grid[9][9], int r, int c, int num); + +void generateIncompleteGrid(int grid[9][9]) { + srand(time(NULL)); + int i, j, block; + for (i = 0; i < 9; i++) { + for (j = 0; j < 9; j++) { + grid[i][j] = 0; + } + } + for (block = 0; block < 3; block++) { + int used[10] = {0}; + for (i = 0; i < 3; i++) { + int r = block * 3 + i; + int placedNum = 0; + while (placedNum < 3) { + int num = (rand() % 9) + 1; + int c = rand() % 9; + if (grid[r][c] == 0 && isValid(grid, r, c, num) && used[num] == 0) { + grid[r][c] = num; + used[num] = 1; + placedNum++; + } + } + } + } +} + +void print(int grid[9][9]) { + int i, j; + for (i = 0; i < 9; i++) { + for (j = 0; j < 9; j++) { + printf("%2d ", grid[i][j]); + if ((j + 1) % 3 == 0 && j < 8) { + printf("| "); + } + } + printf("\n"); + if ((i + 1) % 3 == 0 && i < 8) { + printf("-----------------------\n"); + } + } +} + +bool isValid(int grid[9][9], int r, int c, int num) { + int x, i, j; + for (x = 0; x < 9; x++) { + if (grid[r][x] == num || grid[x][c] == num) { + return false; + } + } + + int startR = r - r % 3, startC = c - c % 3; + for (i = 0; i < 3; i++) { + for (j = 0; j < 3; j++) { + if (grid[i + startR][j + startC] == num) { + return false; + } + } + } + return true; +} + +bool solve(int grid[9][9]) { + int r, c; + bool empty = false; + + for (r = 0; r < 9; r++) { + for (c = 0; c < 9; c++) { + if (grid[r][c] == 0) { + empty = true; + break; + } + } + if (empty) break; + } + + if (!empty) return true; + + int num; + for (num = 1; num <= 9; num++) { + if (isValid(grid, r, c, num)) { + grid[r][c] = num; + + if (solve(grid)) { + return true; + } + + grid[r][c] = 0; + } + } + return false; +} + +bool validate(int grid[9][9]) { + int r, c; + for (r = 0; r < 9; r++) { + for (c = 0; c < 9; c++) { + if (grid[r][c] != 0) { + int num = grid[r][c]; + grid[r][c] = 0; + if (!isValid(grid, r, c, num)) { + return false; + } + grid[r][c] = num; + } + } + } + return true; +} + +int main() { + int grid[9][9]; + 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}}; + + generateIncompleteGrid(grid); + printf("The original Sudoku matrix:\n"); + print(board0); + + if (validate(board0)) { + printf("True:Valid initial Sudoku matrix!\n"); + if (solve(board0)) { + printf("Completed Sudoku Grid:\n"); + print(board0); + } else { + printf("No solution exists for this Sudoku.\n"); + } + } else { + printf("False:Invalid initial Sudoku matrix!\n"); + } + return 0; +} +