From 7cfe68c6946f7470fdfa976a1bc1846e0bd3a942 Mon Sep 17 00:00:00 2001 From: p8sljnpht <3178612685@qq.com> Date: Sat, 2 Nov 2024 09:37:59 +0800 Subject: [PATCH] =?UTF-8?q?=E7=AC=AC=E5=9B=9B=E9=A2=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Sudoku.c | 169 +++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 169 insertions(+) create mode 100644 Sudoku.c diff --git a/Sudoku.c b/Sudoku.c new file mode 100644 index 0000000..bb1d256 --- /dev/null +++ b/Sudoku.c @@ -0,0 +1,169 @@ +#include +#include +#include +#include + +char error[114] = ""; +void print(int a[9][9]) { + + int i,j; + for(i=0; i<=9; i++) { + if(i%3 == 0) + printf("|-----------------------|\n"); + if(i >= 9) break; + printf("| "); + for(j=0; j<9; j++) { + if(a[i][j] > 0) + printf("%d ",a[i][j]); + else printf(". "); + if((j+1)%3 == 0) + printf("| "); + } + printf("\n"); + } +} + +void generate(int a[9][9]) { + memset(a, 0, sizeof(int) * 81); + + int i; + int x,y,z; + int t[10]; + for(i = 0; i < 9; i++) { + if(i%3 == 0) + memset(t, 0, sizeof(t)); + x = rand() % 9; + do { + y = rand() % 9; + }while(x == y); + do { + z = rand() % 9; + }while(x == z || y == z); + do { + a[i][x] = rand() % 9 + 1; + }while(t[a[i][x]]); + t[a[i][x]] = 1; + do { + a[i][y] = rand() % 9 + 1; + }while(t[a[i][y]]); + t[a[i][y]] = 1; + do { + a[i][z] = rand() % 9 + 1; + }while(t[a[i][z]]); + t[a[i][z]] = 1; + } + +} + +int isSudoku(int a[9][9], const char* error) { + int t[10], i, j, x, y, sx, sy; + + for(i=0; i<9; i++) { + memset(t, 0, sizeof(t)); + for(j=0; j<9; j++) { + if(t[a[i][j]]) { + sprintf(error, "The number %d in the row %d has been used!\n", + a[i][j], i+1); + return 0; + } + t[a[i][j]] = 1; + t[0] = 0; + } + } + + for(j=0; j<9; j++) { + memset(t, 0, sizeof(t)); + for(i=0; i<9; i++) { + if(t[a[i][j]]) { + sprintf(error, "The number %d in the col %d has been used!\n", + a[i][j], j+1); + return 0; + } + t[a[i][j]] = 1; + t[0] = 0; + } + } + + for(x=0; x<2; x++) + for(y=0; y<2; y++) { + memset(t, 0, sizeof(t)); + sx = x*3; sy = y*3; + for(i=sx; i= (int*)a + 81) return 1; + + do { + next++; + } + while(*next > 0); + + for(k=1; k<=9; k++) { + *it = k; + if(isSudoku(a,error)) { +// print(a); + if(_solve(next, a)) // call stack + return 1; + } + } + // erase + *it = 0; + return 0; +} + +int solve(int a[9][9]) { + int* it = a; + while(*it > 0) + it++; + return _solve(it, a); +} + +int main() { + srand(time(0)); + int a[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}}; +// generate(a); + + int valid = isSudoku(a, error); + + printf("The original Sudoku matrix:\n"); + print(a); + + if(valid) { + printf("True:Valid initial Sudoku matrix!\n"); + } + else { + printf("False:Invalid initial Sudoku matrix!\n %s", error); + } + + if(valid && solve(a)) { + printf("The solution of Sudoku matrix:\n"); + print(a); + } + else { + printf("No solution!"); + } +}