diff --git a/4 b/4 new file mode 100644 index 0000000..9fd8ac8 --- /dev/null +++ b/4 @@ -0,0 +1,196 @@ +#include +#include +void output(const int (*p_board)[9]); +int check(const int (*p_board)[9], int row, int column, int n); +int solve(int (*p_board)[9]); +int main() +{ + int board[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}}; + printf("The original Sudoku matrix:\n"); + output(board); + int state; + int i,j,k; + int f=1; + for(i=0;i<9;i++) + { + int x[10]={0}; + for(j=0;j<9;j++) + { + x[board[i][j]]+=1; + } + for(k=1;k<=9;k++) + { + if(x[k]>1&&f==1) + { + printf("False:Invalid initial Sudoku matrix!\n"); + printf("The number %d in the col %d has been used!\n",k,i+1); + f=0; + } + } + } + for(i=0;i<9;i++) + { + int z[10]={0}; + for(j=0;j<9;j++) + { + z[board[j/3+(i%3)*3][j%3+(i/3)*3]]+=1; + } + for(k=1;k<=9;k++) + { + if(z[k]>1&&f==1) + { + printf("False:Invalid initial Sudoku matrix!\n"); + printf("The number %d in the block %d has been used!\n",k,i+1); + f=0; + } + } + } + for(i=0;i<9;i++) + { + int y[10]={0}; + for(j=0;j<9;j++) + { + y[board[j][i]]+=1; + } + for(k=1;k<=9;k++) + { + if(y[k]>1&&f==1) + { + printf("False:Invalid initial Sudoku matrix!\n"); + printf("The number %d in the row %d has been used!\n",k,i+1); + f=0; + } + } + } + if(f==1)printf("True:Valid initial Sudoku matrix!\n"); + state=solve(board); + if (state) + { + printf("The solution of Sudoku matrix:\n"); + output(board); + } + else + { + printf("No solution!\n"); + } + return 0; +} + +void output(const int (*p_board)[9]) +{ + for (int i=0;i<9;i++) + { + if (i%3==0) + { + printf("|-----------|\n"); + } + for (int j=0;j<9;j++) + { + if (j%3==0) + { + printf("|"); + } + printf("%d", p_board[i][j]); + } + printf("|\n"); + } + printf("|-----------|\n"); +} +int check(const int (*p_board)[9], int row, int column, int num) +{ + int i, j, center_row, center_column; + for (j = 0; j < 9; j++) + { + if (p_board[row][j] == num) + { + return 0; + } + } + for (i=0;i<9;i++) + { + if (p_board[i][column]==num) + { + return 0; + } + } + center_row=row/3*3; + center_column =column/3*3; + for (i=center_row;i<=center_row+2;i++) + { + for (j=center_column;j<=center_column+2;j++) + { + if (p_board[i][j]==num) + { + return 0; + } + } + } + return 1; +} +int solve(int (*p_board)[9]) +{ + struct Idex + { + int row; + int column; + int state; + }; + struct Idex arr_idex[81]; + int i,j; + int end=-1, now=0; + for (i=0;i<9;i++) + { + for (j=0;j<9;j++) + { + if (p_board[i][j]==0) + { + end++; + arr_idex[end].row=i; + arr_idex[end].column=j; + arr_idex[end].state=1; + } + } + } + for (now=0;now<=end;) + { + if (arr_idex[now].state > 9) + { + if (now==0) + { + return 0; + } + else + { + arr_idex[now].state=1; + p_board[arr_idex[now].row][arr_idex[now].column]=0; + now--; + continue; + } + } + for (i=arr_idex[now].state;i<=9;i++) + { + if (check(p_board,arr_idex[now].row,arr_idex[now].column,i)) + { + arr_idex[now].state=i+1; + p_board[arr_idex[now].row][arr_idex[now].column]=i; + now++; + break; + } + else if (i == 9) + { + arr_idex[now].state=1; + p_board[arr_idex[now].row][arr_idex[now].column]=0; + now--; + } + } + } + return 1; +} \ No newline at end of file