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.
249 lines
5.8 KiB
249 lines
5.8 KiB
#include <stdio.h>
|
|
void geshihua(int arr0[9][9])
|
|
{
|
|
int i,j;
|
|
printf("|");
|
|
for(i=0;i<9;i++)
|
|
{
|
|
for(j=0;j<9;j++)
|
|
{
|
|
printf("%d ",arr0[i][j]);
|
|
|
|
if(j==8)
|
|
{
|
|
printf("|\n");
|
|
}
|
|
if(i==8&&j==8)
|
|
break;
|
|
if((i+1)%3==0&&j==8&&i!=8)
|
|
{
|
|
printf("|--------------------|\n");
|
|
}
|
|
if((j+1)%3==0)
|
|
{
|
|
printf("|");
|
|
|
|
}
|
|
|
|
}
|
|
}
|
|
}
|
|
void judge(int a[9][9])
|
|
{
|
|
int i,j,m,n;
|
|
for(i=0;i<9;i++)
|
|
{
|
|
for(j=0;j<9;j++)
|
|
{
|
|
if(a[i][j]==0)
|
|
continue;
|
|
for(m=0;m<9;m++)
|
|
if(m!=i&&a[m][j]==a[i][j])
|
|
{
|
|
printf("False:Invalid initial Sudoku matrix!\nThe number %d in the col %d has been used!",a[i][j],j+1);
|
|
return;
|
|
}
|
|
for(n=0;n<9;n++)
|
|
{
|
|
if(n!=j&&a[i][j]==a[n][j])
|
|
{
|
|
printf("False:Invalid initial Sudoku matrix!\nThe number %d in the rank %d has been used!",a[i][j],i+1);
|
|
return;
|
|
}
|
|
}
|
|
}
|
|
}
|
|
for(i=0;i<3;i++)
|
|
{
|
|
for(j=0;j<3;j++)
|
|
{
|
|
if(a[i][j]=0)
|
|
continue;
|
|
for(m=0;m<3;m++)
|
|
{
|
|
for(n=0;n<3;n++)
|
|
{
|
|
if(i!=m&&n!=j&a[i][j]==a[m][n])
|
|
{
|
|
printf("??The number 3 in the block 1 has been used!");
|
|
return;
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
for(i=3;i<6;i++)
|
|
{
|
|
for(j=0;j<3;j++)
|
|
{
|
|
if(a[i][j]=0)
|
|
continue;
|
|
for(m=3;m<6;m++)
|
|
{
|
|
for(n=3;n<6;n++)
|
|
{
|
|
if(i!=m&&n!=j&a[i][j]==a[m][n])
|
|
{
|
|
printf("??The number 3 in the block 2 has been used!");
|
|
return;
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
for(i=6;i<9;i++)
|
|
{
|
|
for(j=0;j<3;j++)
|
|
{
|
|
if(a[i][j]=0)
|
|
continue;
|
|
for(m=3;m<6;m++)
|
|
{
|
|
for(n=3;n<6;n++)
|
|
{
|
|
if(i!=m&&n!=j&a[i][j]==a[m][n])
|
|
{
|
|
printf("??The number 3 in the block 3 has been used!");
|
|
return;
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
for(i=0;i<3;i++)
|
|
{
|
|
for(j=3;j<6;j++)
|
|
{
|
|
if(a[i][j]=0)
|
|
continue;
|
|
for(m=0;m<3;m++)
|
|
{
|
|
for(n=0;n<3;n++)
|
|
{
|
|
if(i!=m&&n!=j&a[i][j]==a[m][n])
|
|
{
|
|
printf("??The number 3 in the block 4 has been used!");
|
|
return;
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
for(i=3;i<6;i++)
|
|
{
|
|
for(j=3;j<6;j++)
|
|
{
|
|
if(a[i][j]=0)
|
|
continue;
|
|
for(m=0;m<3;m++)
|
|
{
|
|
for(n=0;n<3;n++)
|
|
{
|
|
if(i!=m&&n!=j&a[i][j]==a[m][n])
|
|
{
|
|
printf("??The number 3 in the block 5 has been used!");
|
|
return;
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
for(i=6;i<9;i++)
|
|
{
|
|
for(j=3;j<6;j++)
|
|
{
|
|
if(a[i][j]=0)
|
|
continue;
|
|
for(m=0;m<3;m++)
|
|
{
|
|
for(n=0;n<3;n++)
|
|
{
|
|
if(i!=m&&n!=j&a[i][j]==a[m][n])
|
|
{
|
|
printf("??The number 3 in the block 6 has been used!");
|
|
return;
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
for(i=0;i<3;i++)
|
|
{
|
|
for(j=6;j<9;j++)
|
|
{
|
|
if(a[i][j]=0)
|
|
continue;
|
|
for(m=0;m<3;m++)
|
|
{
|
|
for(n=0;n<3;n++)
|
|
{
|
|
if(i!=m&&n!=j&a[i][j]==a[m][n])
|
|
{
|
|
printf("??The number 3 in the block 7 has been used!");
|
|
return;
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
for(i=3;i<6;i++)
|
|
{
|
|
for(j=6;j<9;j++)
|
|
{
|
|
if(a[i][j]=0)
|
|
continue;
|
|
for(m=0;m<3;m++)
|
|
{
|
|
for(n=0;n<3;n++)
|
|
{
|
|
if(i!=m&&n!=j&a[i][j]==a[m][n])
|
|
{
|
|
printf("??The number 3 in the block 8 has been used!");
|
|
return;
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
for(i=6;i<9;i++)
|
|
{
|
|
for(j=6;j<9;j++)
|
|
{
|
|
if(a[i][j]=0)
|
|
continue;
|
|
for(m=0;m<3;m++)
|
|
{
|
|
for(n=0;n<3;n++)
|
|
{
|
|
if(i!=m&&n!=j&a[i][j]==a[m][n])
|
|
{
|
|
printf("??The number 3 in the block 9 has been used!");
|
|
return;
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
printf("True:Valid initial Sudoku matrix!");
|
|
}
|
|
int main()
|
|
{
|
|
int board[9][9] = {{5, 9, 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 i,j;
|
|
printf("The original Sudoku matrix:\n");
|
|
geshihua(board);
|
|
printf("\n");
|
|
judge(board);
|
|
return 0;
|
|
|
|
}
|
|
|