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.

132 lines
2.3 KiB

#include <stdio.h>
#include <stdbool.h>
int l=0;
void x1(int m[9][9])
{
int i,j;
for(i=0;i<9;i++)
{
if(i%3==0)printf("-------------\n");
for(j=0;j<9;j++)
{
if(j%3==0)printf("|");
printf("%d",m[i][j]);
}
printf("|\n");
}
printf("-------------");
}
void hang(int m[9][9], int *l) {
int i,j,k;
for(i=0;i<9;i++)
{
for(j=0;j<9;j++)
{
int a=m[i][j];
for(k=0;k<9;k++)
{
if(m[i][j]==m[i][k]&&j!=k&&a!=0)
{
printf("The number %d in the %d row has been used!\n", a, i+1);
(*l)++;
break;
}
}
if(*l==1)break;
}
if(*l==1){
break;
}
}
}
void lie(int m[9][9], int *l) {
int i,j,k;
for(i=0;i<9;i++)
{
for(j=0;j<9;j++)
{
int a=m[j][i];
for(k=0;k<9;k++)
{
if(m[j][i]==m[k][i]&&j!=k&&a!=0&&*l==0)
{
if(*l==1)break;
printf("The number %d in the %d col has been used!\n", a, i+1);
(*l)++;
break;
}
}
if(*l==1)break;
}
if(*l==1){
break;
}
}
}
void o3(int m[9][9], int *l) {
int i,j,k,n,t;
for(n=0;n<9;n++)
{
for(i=n%3*3;i<n*3%3*3+3;i++)
{
for(j=n/3*3;j<n/3*3+3;j++)
{
int a=m[i][j];
for(k=n%3*3;k<n%3*3+3;k++)
{
for(t=n/3*3;t<n/3*3+3;t++)
{
if(a==m[k][t]&&i!=k&&j!=t&&a!=0&&*l==0)
{
printf("The number %d in the block %d has been used!\n", a,n+1);
(*l)++;
break;
}
}
}
if(*l==1)break;
}
if(*l==1){
break;}
}
}
}
int main()
{
int m[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");
x1(m);
printf("\n");
hang(m,&l);
lie(m,&l);
o3(m,&l);
if(l>=1) {
printf("False: Invalid initial Sudoku matrix!\n");
}
if (l==0) {
printf("True: Valid initial Sudoku matrix!\n");
}
return 0;
}