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.

139 lines
2.6 KiB

#include <stdio.h>
#include <stdlib.h>
#include <time.h>
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}};
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}};
printf("The original Sudoku matrix:\n");
int i=0,j=0,z=0,k=0,m=0;
for(i=0;i<9;i++){
if(i==0)
{printf("|-----------------------|");
printf("\n");
}
for(j=0;j<9;j++)
{
if(j==0)
{printf("| ");
}
printf("%d ",board0[i][j]);
if(j%3==2&&j!=0){
printf("| ");
}
if(j==8){
printf("\n");
}
}
if(i%3==2&&i!=0)
{
printf("|-----------------------|\n");
}
}
int a[10];
for(m=0;m<=10;m++)
{
a[m]=0;
}
for(i=0;i<9;i++)
{
for (j=0;j<9;j++)
{
a[board0[i][j]]++;
}
for (j=1;j<10;j++)
{
if(a[j]>1)
{
printf("False:Invalid initial Sudoku matrix!\n");
printf("The number %d in the row %d has been used!\n",j,i+1);
k++;
exit(0);
}
}
for(m=0;m<=10;m++)
{
a[m]=0;
}
}
for(j=0;j<9;j++)
{
for (i=0;i<9;i++)
{
a[board0[i][j]]++;
}
for (i=1;i<10;i++)
{
if(a[i]>1)
{
printf("False:Invalid initial Sudoku matrix!\n");
printf("The number %d in the col %d has been used!\n",i,j+1);
k++;
exit(0);
}
}
for(m=0;m<=10;m++)
{
a[m]=0;
}
}
for(z=0;z<3;z++){
for(i=z*3;i<(z+1)*3;i++)
{
for(j=i*3;j<(i+1)*3;j++){
a[board0[i][j]]++;
}
for (j=1;j<10;j++)
{
if(a[j]>1)
{
printf("False:Invalid initial Sudoku matrix!\n");
printf("The number %d in the block %d has been used!\n",j,z*3+i/3+j/3);
k++;
exit(0);
}
}
for(m=0;m<=10;m++)
{
a[m]=0;
}
}
}
if(k==0)
{
printf("True:Valid initial Sudoku matrix!");
}
return 0;
}