You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

116 lines
2.9 KiB

#include <stdio.h>
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;
}