diff --git a/数独矩阵第四关.c b/数独矩阵第四关.c new file mode 100644 index 0000000..91673fd --- /dev/null +++ b/数独矩阵第四关.c @@ -0,0 +1,204 @@ +#include + +void print_valid(int a[9][9]){ + int x=1; + int i,j,ii,jj; + for (i = 0; i < 9; i++) { + int count[10] = {0}; + for (j = 0; j < 9; j++) { + if (a[i][j]!= 0) { + if (count[a[i][j]] > 0) { + x = 0; + printf("False:Invalid initial Sudoku matrix!\n The number %d in the row %d has been used!\n", a[i][j], i + 1); + return; + } + count[a[i][j]]++; + } + } + } + int y=1; + for (j = 0; j < 9; j++) { + int count[10] = {0}; + for (i = 0; i <9; i++) { + if (a[i][j]!= 0) { + if (count[a[i][j]] > 0) { + y = 0; + printf("False:Invalid initial Sudoku matrix!\n The number %d in the col %d has been used!\n", a[i][j], j + 1); + return; + } + count[a[i][j]]++; + } + } + } + int z=1; + for (i = 0; i <9; i += 3) { + for (j = 0; j <9; j += 3) { + int count[10] = {0}; + for (ii = i; ii < i + 3; ii++) { + for (jj = j; jj < j + 3; jj++) { + if (a[ii][jj]!= 0) { + if (count[a[ii][jj]] > 0) { + z = 0; + printf("False:Invalid initial Sudoku matrix!\n The number %d in the 3x3 submatrix starting at row %d, col %d has been used!\n", a[ii][jj], i + 1, j + 1); + return; + } + count[a[ii][jj]]++; + } + } + } + } + } + if (x&&y&&z) { + printf("True:Valid initial Sudoku matrix!\n"); + } + return; +} + +int is_valid(int a[9][9]){ + int x=1; + int i,j,ii,jj; + for (i = 0; i < 9; i++) { + int count[10] = {0}; + for (j = 0; j < 9; j++) { + if (a[i][j]!= 0) { + if (count[a[i][j]] > 0) { + x = 0; + return 0; + } + count[a[i][j]]++; + } + } + } + int y=1; + for (j = 0; j < 9; j++) { + int count[10] = {0}; + for (i = 0; i <9; i++) { + if (a[i][j]!= 0) { + if (count[a[i][j]] > 0) { + y = 0; + return 0; + } + count[a[i][j]]++; + } + } + } + int z=1; + for (i = 0; i <9; i += 3) { + for (j = 0; j <9; j += 3) { + int count[10] = {0}; + for (ii = i; ii < i + 3; ii++) { + for (jj = j; jj < j + 3; jj++) { + if (a[ii][jj]!= 0) { + if (count[a[ii][jj]] > 0) { + z = 0; + return 0; + } + count[a[ii][jj]]++; + } + } + } + } + } + if (x&&y&&z) { + } + return 1; +} + +int solve_sudoku(int a[9][9]) { + int row,col,num; + for (row = 0; row < 9; row++) { + for (col = 0; col < 9; col++) { + if (a[row][col] == 0) { + for (num = 1; num <= 9; num++) { + if (is_valid(a)) { + a[row][col] = num; + if (solve_sudoku(a)) { + return 1; + } + a[row][col] = 0; + } + } + return 0; + } + } + } + return 1; +} + +void print(int a[9][9]) { + printf("|-----------------------|\n"); + int i,j; + for(i=0;i<9;i++){ + printf("| "); + for(j=0;j<9;j++){ + printf("%d ",a[i][j]); + if(j==2||j==5) printf("| "); + if(j==8) printf("|\n"); + } + if(i==2||i==5) { + int x=22; + printf("|"); + while(x>=0) { + printf("-"); + x--; + } + printf("|\n"); + } + } + printf("|-----------------------|\n"); +} + +void final(int a[9][9]) { + printf("The original Sudoku matrix: \n"); + print(a); + int valid=is_valid(a); + print_valid(a); + if(valid){ + if (solve_sudoku(a)) { + printf("The solution of Sudoku matrix:\n"); + print(a); + } + else printf("No solution!\n"); + } + else printf("No solution!\n"); +} + +int main(){ + 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} + }; + final(board0); + int board1[9][9] = { + {8, 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} + }; + final(board1); + int board2[9][9] = { + {5, 2, 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} + }; + final(board2); + return 0; +}