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.
181 lines
3.7 KiB
181 lines
3.7 KiB
1 year ago
|
#include<stdio.h>
|
||
|
#include<string.h>
|
||
|
struct Checkout{
|
||
|
int flag;
|
||
|
char type[20];
|
||
|
int num;
|
||
|
int place;
|
||
|
};
|
||
|
|
||
|
void printmat(volatile 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;
|
||
|
}
|
||
|
|
||
|
struct Checkout check(volatile int board[9][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;
|
||
|
struct Checkout out;
|
||
|
|
||
|
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);
|
||
|
out.flag=0;
|
||
|
strcpy(out.type,"row");
|
||
|
out.num=board[i][j];
|
||
|
out.place=i+1;
|
||
|
return out;
|
||
|
}
|
||
|
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);
|
||
|
out.flag=0;
|
||
|
strcpy(out.type,"col");
|
||
|
out.num=board[j][i];
|
||
|
out.place=i+1;
|
||
|
return out;
|
||
|
}
|
||
|
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);
|
||
|
out.flag=0;
|
||
|
strcpy(out.type,"block");
|
||
|
out.num=board[i][j];
|
||
|
out.place=k+1;
|
||
|
return out;
|
||
|
}
|
||
|
else flag[board[i][j]-1]=1;
|
||
|
}
|
||
|
}
|
||
|
}
|
||
|
}
|
||
|
//printf("True:Valid initial Sudoku matrix!\n");
|
||
|
out.flag=1;
|
||
|
return out;
|
||
|
}
|
||
|
|
||
|
int sv=0;
|
||
|
|
||
|
int solve(volatile int board[9][9],int cnt)
|
||
|
{
|
||
|
if (cnt==0)
|
||
|
{
|
||
|
if(!sv) printf("The solution of Sudoku martix:\n");
|
||
|
printmat(board);
|
||
|
sv=1;
|
||
|
return 1;
|
||
|
}
|
||
|
else
|
||
|
{
|
||
|
//printf("check %d\n",cnt);
|
||
|
int i,j,num;
|
||
|
for(i=0;i<9;i++)
|
||
|
{
|
||
|
for(j=0;j<9;j++)
|
||
|
{
|
||
|
if(board[i][j]==0)
|
||
|
{
|
||
|
for(num=1;num<10;num++)
|
||
|
{
|
||
|
board[i][j]=num;
|
||
|
//printf("%d %d %d\n",i,j,num);
|
||
|
if(check(board).flag)
|
||
|
{
|
||
|
if(solve(board,cnt-1)) return 1;
|
||
|
}
|
||
|
}
|
||
|
board[i][j]=0;
|
||
|
return 0;
|
||
|
}
|
||
|
}
|
||
|
}
|
||
|
}
|
||
|
|
||
|
}
|
||
|
|
||
|
int main()
|
||
|
{
|
||
|
volatile 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 i,j,k,cnt=0;
|
||
|
printf("The original Sudoku matrix\n");
|
||
|
printmat(board);
|
||
|
struct Checkout out=check(board);
|
||
|
if (!out.flag)
|
||
|
{
|
||
|
printf("False:Invalid initial Sudoku matrix\n");
|
||
|
printf(" The number %d in the %s %d has been used!\n",out.num,out.type,out.place);
|
||
|
printf("No solusion!");
|
||
|
return 0;
|
||
|
}
|
||
|
printf("True:Valid initial Sudoku matrix!\n");
|
||
|
for (i=0;i<9;i++) for(j=0;j<9;j++) if(board[i][j]==0) cnt++;
|
||
|
solve(board,cnt);
|
||
|
if (!sv) printf("No solusion!");
|
||
|
return 0;
|
||
|
}
|