|
|
#include<stdio.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}};
|
|
|
void Judge(int a[9][9]);
|
|
|
Judge(board0);
|
|
|
Judge(board1);
|
|
|
}
|
|
|
|
|
|
void Judge(int a[9][9])
|
|
|
{
|
|
|
//Step1:输出待判断的矩阵
|
|
|
printf("The original Sudoku matrix:\n") ;
|
|
|
for(int i=0;i<9;i++)
|
|
|
{
|
|
|
printf("|");//每一行的以|开始
|
|
|
for(int j=0;j<9;j++)
|
|
|
{
|
|
|
printf("%d",a[i][j]);
|
|
|
if((j+1)%3==0)
|
|
|
{
|
|
|
printf("|");//区分3*3
|
|
|
if(j==8)
|
|
|
{
|
|
|
printf("\n");
|
|
|
}
|
|
|
}
|
|
|
}
|
|
|
if((i+1)%3==0&&i!=8)
|
|
|
{
|
|
|
printf("|----分割---|\n");
|
|
|
}//每三行就换一行
|
|
|
|
|
|
}
|
|
|
|
|
|
//Step2:进行是否满足定义的判断
|
|
|
int judge=0;//judge作为是否满足定义的标准,为0表示满足
|
|
|
//Step2-1:是否每一行不出现多次同一数字
|
|
|
for(int i=0;i<9;i++)
|
|
|
{
|
|
|
int times[9]={0};
|
|
|
for(int j=0;j<9;j++)//统计第i行每个数字的出现次数
|
|
|
{
|
|
|
if(a[i][j]>=1)times[a[i][j]-1]++;
|
|
|
}
|
|
|
|
|
|
for(int j=0;j<9;j++)
|
|
|
{
|
|
|
if(times[j]>1)
|
|
|
{
|
|
|
judge=1;
|
|
|
printf("False:Invalid initial Sudoku matrix!\n");
|
|
|
printf("The number %d in the row %d has been used!\n",j+1,i+1);
|
|
|
break;
|
|
|
}
|
|
|
}
|
|
|
if(judge){break;}
|
|
|
}
|
|
|
|
|
|
//Step2-2:判断是否每一列不出现多次同一数字
|
|
|
if(judge==0)
|
|
|
{
|
|
|
for(int j=0;j<9;j++)
|
|
|
{
|
|
|
int times[9]={0};
|
|
|
for(int i=0;i<9;i++)//统计第j列每个数字的出现次数
|
|
|
{
|
|
|
if(a[i][j]>=1)times[a[i][j]-1]++;
|
|
|
}
|
|
|
|
|
|
for(int i=0;i<9;i++)
|
|
|
{
|
|
|
if(times[i]>1)
|
|
|
{
|
|
|
judge=1;
|
|
|
printf("False:Invalid initial Sudoku matrix!\n");
|
|
|
printf("The number %d in the col %d has been used!\n",i+1,j+1);
|
|
|
break;
|
|
|
}
|
|
|
}
|
|
|
if(judge){break;}
|
|
|
}
|
|
|
}
|
|
|
//Step2-3:每一块是否出现多次同一数字
|
|
|
if(judge==0)
|
|
|
{
|
|
|
for(int i=0;i<3;i++)
|
|
|
{
|
|
|
for(int j=0;j<3;j++)
|
|
|
{
|
|
|
int times[9]={0};
|
|
|
for(int m=3*i;m<=3*i+2;m++)
|
|
|
{
|
|
|
for(int n=3*j;n<=3*j+2;n++)
|
|
|
{
|
|
|
if(a[m][n]>=1)times[a[m][n]-1]++;
|
|
|
}
|
|
|
}
|
|
|
|
|
|
for(int i=0;i<9;i++)
|
|
|
{
|
|
|
if(times[i]>1)
|
|
|
{
|
|
|
judge=1;
|
|
|
printf("False:Invalid initial Sudoku matrix!\n");
|
|
|
printf("The number %d in the block %d has been used!\n",i+1,i+j+1);
|
|
|
break;
|
|
|
}
|
|
|
}
|
|
|
if(judge){break;}
|
|
|
}
|
|
|
if(judge){break;}
|
|
|
}
|
|
|
}
|
|
|
|
|
|
if(judge==0){printf("True:Valid initial Sudoku matrix!\n") ;}
|
|
|
|
|
|
return;
|
|
|
}
|