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.
105 lines
2.2 KiB
105 lines
2.2 KiB
1 year ago
|
#include<stdio.h>
|
||
|
void printmat(int board[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(j==0?"|":" |");
|
||
|
printf(" %d",board[i][j]);
|
||
|
}
|
||
|
printf(" |\n");
|
||
|
}
|
||
|
printf("|-----------------------|\n");
|
||
|
return;
|
||
|
}
|
||
|
|
||
|
int main()
|
||
|
{
|
||
|
int board[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 checkmat[9][2][2]={{{0,0},{3,3}},
|
||
|
{{0,3},{3,6}},
|
||
|
{{0,6},{3,9}},
|
||
|
{{3,0},{6,3}},
|
||
|
{{3,3},{6,6}},
|
||
|
{{3,6},{6,9}},
|
||
|
{{6,0},{9,3}},
|
||
|
{{6,3},{9,6}},
|
||
|
{{6,6},{9,9}}};
|
||
|
int i,j,k,p,num;
|
||
|
printf("The original Sudoku matrix\n");
|
||
|
printmat(board);
|
||
|
|
||
|
|
||
|
for (i=0;i<9;i++)
|
||
|
{
|
||
|
int flag[9] = {0,0,0,0,0,0,0,0,0};
|
||
|
for (j=0;j<9;j++)
|
||
|
{
|
||
|
if (board[i][j]!=0 )
|
||
|
{
|
||
|
if (flag[board[i][j]-1]==1)
|
||
|
{
|
||
|
printf("False:Invalid initial Sudoku matrix\n");
|
||
|
printf(" The number %d in the row %d has been used!\n",board[i][j],i+1);
|
||
|
return 0;
|
||
|
}
|
||
|
else flag[board[i][j]-1]=1;
|
||
|
}
|
||
|
}
|
||
|
}
|
||
|
|
||
|
for (i=0;i<9;i++)
|
||
|
{
|
||
|
int flag[9] = {0,0,0,0,0,0,0,0,0};
|
||
|
for (j=0;j<9;j++)
|
||
|
{
|
||
|
if (board[j][i]!=0 )
|
||
|
{
|
||
|
if (flag[board[j][i]-1]==1)
|
||
|
{
|
||
|
printf("False:Invalid initial Sudoku matrix\n");
|
||
|
printf(" The number %d in the col %d has been used!\n",board[j][i],i+1);
|
||
|
return 0;
|
||
|
}
|
||
|
else flag[board[j][i]-1]=1;
|
||
|
}
|
||
|
}
|
||
|
}
|
||
|
|
||
|
for (k=0;k<9;k++)
|
||
|
{
|
||
|
int flag[9] = {0,0,0,0,0,0,0,0,0};
|
||
|
for (i=checkmat[k][0][0];i<checkmat[k][1][0];i++)
|
||
|
{
|
||
|
for (j=checkmat[k][0][1];j<checkmat[k][1][1];j++)
|
||
|
{
|
||
|
if (board[i][j]!=0 )
|
||
|
{
|
||
|
if (flag[board[i][j]-1]==1)
|
||
|
{
|
||
|
printf("False:Invalid initial Sudoku matrix\n");
|
||
|
printf(" The number %d in the block %d has been used!\n",board[i][j],k+1);
|
||
|
return 0;
|
||
|
}
|
||
|
else flag[board[i][j]-1]=1;
|
||
|
}
|
||
|
}
|
||
|
}
|
||
|
}
|
||
|
printf("True:Valid initial Sudoku matrix!\n");
|
||
|
return 0;
|
||
|
}
|
||
|
|