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.
87 lines
1.8 KiB
87 lines
1.8 KiB
2 weeks ago
|
#include<stdio.h>
|
||
|
int main(){
|
||
|
int board[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}};
|
||
|
printf("The original Sudoku matrix: \n");
|
||
|
for(int i=0;i<9;i++){
|
||
|
if(i%3==0){
|
||
|
printf("-------------\n");
|
||
|
}
|
||
|
for(int j=0;j<9;j++){
|
||
|
if(j%3==0||j==9){
|
||
|
printf("|");
|
||
|
}
|
||
|
printf("%d",board[i][j]);
|
||
|
}
|
||
|
printf("|\n");
|
||
|
}printf("-------------\n");
|
||
|
|
||
|
int useful=1;
|
||
|
int a[10];
|
||
|
for(int r=0;r<9&&useful;r++){
|
||
|
for(int k=0;k<10;k++)
|
||
|
a[k]=0;
|
||
|
for(int c=0;c<9;c++){
|
||
|
|
||
|
if(board[r][c]!=0){
|
||
|
if(a[board[r][c]]==1){
|
||
|
printf("False:Invalid initial Sudoku matrix!\n");
|
||
|
printf("The number %d in the row %d has been used!",board[r][c],r+1);
|
||
|
useful=0;
|
||
|
break;
|
||
|
}
|
||
|
a[board[r][c]]==1;
|
||
|
}
|
||
|
}
|
||
|
|
||
|
}
|
||
|
for(int c=0;c<9&&useful;c++){
|
||
|
for(int k=0;k<10;k++)
|
||
|
a[k]=0;
|
||
|
for(int r=0;r<9;r++){
|
||
|
if(board[r][c]!=0){
|
||
|
if(a[board[r][c]]==1){
|
||
|
printf("False:Invalid initial Sudoku matrix!\n");
|
||
|
printf("The number %d in the col %d has been used!",board[r][c],c+1);
|
||
|
useful=0;
|
||
|
break;
|
||
|
}
|
||
|
a[board[r][c]]=1;
|
||
|
}
|
||
|
}
|
||
|
}
|
||
|
for(int bigr=0;bigr<9&&useful;bigr+=3){
|
||
|
for(int bigc=0;bigc<9&&useful;bigc+=3){
|
||
|
for(int k=0;k<10;k++)
|
||
|
a[k]=0;
|
||
|
for(int r=0;r<3;r++){
|
||
|
for(int c=0;c<3;c++){
|
||
|
int x=board[bigr+r][bigc+c];
|
||
|
if(x!=0){
|
||
|
if(a[x]==1){
|
||
|
printf("False:Invalid initial Sudoku matrix!\n");
|
||
|
printf("The number %d in the block %d has been used!",bigr+r+1,x);
|
||
|
useful=0;
|
||
|
goto end;
|
||
|
}
|
||
|
a[x]=1;
|
||
|
}
|
||
|
}
|
||
|
}
|
||
|
}
|
||
|
}
|
||
|
if(useful){
|
||
|
|
||
|
printf("True:Valid initial Sudoku matrix!\n");
|
||
|
}
|
||
|
end:
|
||
|
return 0;
|
||
|
}
|