diff --git a/main_3.c b/main_3.c new file mode 100644 index 0000000..c2ba146 --- /dev/null +++ b/main_3.c @@ -0,0 +1,115 @@ +#include + + +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}}; + +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}}; +int err_group=0,err_num=0; + +int check_submatrix(int * ptr){ + int bottle[9]={0}; + int * cur_ptr=ptr; + for(int i=0;i<3;i++){ + for(int j=0;j<3;j++){ + bottle[*(cur_ptr+j)]++; + //printf("%d ",*(cur_ptr+j)); + } + //printf("\n"); + cur_ptr+=9; + } + for(int i=1;i<=8;i++){ + //printf("%d",bottle[i]); + if(bottle[i]>1){ + err_num = i; + return 0; + } + } + return 1; +} + +int check_col(int * ptr){ + int bottle[9]={0}; + int * cur_ptr=ptr; + for(int i=0;i<9;i++){ + bottle[* cur_ptr]++; + //printf("%d \n",* cur_ptr); + cur_ptr+=9; + } + for(int i=1;i<=8;i++){ + //printf("%d",bottle[i]); + if(bottle[i]>1){ + err_num = i; + return 0; + } + } + return 1; +} + +int check_row(int * ptr){ + int bottle[9]={0}; + int * cur_ptr=ptr; + for(int i=0;i<9;i++){ + bottle[* cur_ptr]++; + //printf("%d ",* cur_ptr); + cur_ptr++; + } + for(int i=1;i<=8;i++){ + //printf("%d",bottle[i]); + if(bottle[i]>1){ + err_num = i; + return 0; + } + } + return 1; +} + +int check_whole(int * ptr){ + for(int i=0;i<9;i+=3){ + for(int j=0;j<9;j+=3){ + if(!check_submatrix(&ptr[i*9+j])){ + printf("False:Invalid initial Sudoku matrix!\n"); + printf("The number %d in the block %d has been used!",err_num,i+j/3+1); + return 0; + } + } + } + + for(int i=0;i<9;i++){ + if(!check_row(&ptr[i*9+0])){ + printf("False:Invalid initial Sudoku matrix!\n"); + printf("The number %d in the col %d has been used!",err_num,i); + return 0; + } + } + + for(int i=0;i<9;i++){ + if(!check_col(&ptr[i])){ + printf("False:Invalid initial Sudoku matrix!\n"); + printf("The number %d in the row %d has been used!",err_num,i); + return 0; + } + } + + printf("True:Valid initial Sudoku matrix!"); +} +int main(void) { + + check_whole(&board1[0][0]); + return 0; +}