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.

140 lines
2.4 KiB

This file contains ambiguous Unicode characters!

This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.

#include<stdio.h>
int main()
{
int board[9][9];
int parallel[9][9]={0};//用于计数计算每行1-9出现的次数
int vertical[9][9]={0}; //用于计数计算每列1-9出现的次数
int a[9][9]={0}; //用于计数计算每块1-9出现的次数
int element,line=0, column=0, block =0;
int i,j,k;//用于控制循环
int err01,err02,err12,err11,err21,err22;//记录数据出错的位置
for ( i=0;i<9;i++)
{
for( j=0;j<9;j++)
{
scanf("%d",&board[i][j]);
}
}//读取数组
printf("The original Sudoku matrix: \n") ;
printf("|-----------------------|\n");
for(i=0;i<9;i++)
{
printf("| ");
for ( j=0;j<9;j++)
{
printf("%d ",board[i][j]);
if(j==2||j==5)printf("| ");
if(j==8) printf("|\n");
}
if(i==2||i==5||i==8)printf("|-----------------------|\n");
}//输出数组;
for ( i=0;i<9;i++)
{
for( j=0;j<9;j++)
{
if(board[i][j]>=1&&board[i][j]<=9)
{
element=board[i][j];
parallel[i][element-1]++;
}
}
}//统计各行各个数字出现的次数
for ( i=0;i<9;i++)
{
for( j=0;j<9;j++)
{
if(parallel[i][j]>1)
{
line=parallel[i][j];
err01=j+1;
err02=i+1;
goto end;
}
}
}//判断行
for ( i=0;i<9;i++)
{
for( j=0;j<9;j++)
{
if(board[i][j]>=1&&board[i][j]<=9)
{
element=board[i][j];
vertical[element-1][j]++;
}
}
}
for ( i=0;i<9;i++)
{
for( j=0;j<9;j++)
{
if(vertical[i][j]>1)
{
column=vertical[i][j];
err11=j+1;
err12=i+1;
goto end;
}
}
}
//判断列
for (i=0;i<9;i++)
{
for(j=0;j<9;j++)
{
if(board[i][j]>=1&&board[i][j]<=9)
{
element=board[i][j];
k=i/3*3+j/3;
a[k][element-1]++;
}
}
}
for ( i=0;i<9;i++)
{
for( j=0;j<9;j++)
{
if(a[i][j]>1)
{
block=a[i][j];
err21=i+1;
err22=j+1;
goto end;
}
}
}
//判断块
end:
if(line>1)
{
printf("False:Invalid initial Sudoku matrix!\n");
printf("The number %d in the line %d has been used!",err01,err02);
}
else if(column>1)
{
printf("False:Invalid initial Sudoku matrix!\n");
printf("The number %d in the column %d has been used!",err12,err11);
}
else if(block>1)
{
printf("False:Invalid initial Sudoku matrix!\n");
printf("The number %d in the block %d has been used!",err22,err21);
}
else
{
printf("True:Valid initial Sudoku matrix!\n");
}
return 0;
}