|
|
|
|
#include<stdio.h>
|
|
|
|
|
void output(int arry[9][9])
|
|
|
|
|
{
|
|
|
|
|
int i,j;
|
|
|
|
|
printf("|-----------------------|\n");
|
|
|
|
|
for(i=0;i<9;i++)
|
|
|
|
|
{
|
|
|
|
|
for(j=0;j<9;j++)
|
|
|
|
|
{
|
|
|
|
|
if(j==0)
|
|
|
|
|
printf("| ");
|
|
|
|
|
printf("%d ",arry[i][j]);
|
|
|
|
|
if((j+1)%3==0)
|
|
|
|
|
{
|
|
|
|
|
printf("| ");
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
printf("\n");
|
|
|
|
|
if((i+1)%3==0)
|
|
|
|
|
{
|
|
|
|
|
printf("|-----------------------|\n");
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
int dd(int board[][9]){
|
|
|
|
|
int a[9][9];//<2F><><EFBFBD>ò<EFBFBD>ͬ<EFBFBD><CDAC><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
|
|
|
|
int q,p;
|
|
|
|
|
for(q=0;q<3;q++)//<2F><><EFBFBD><EFBFBD>1
|
|
|
|
|
{
|
|
|
|
|
for(p=0;p<3;p++)
|
|
|
|
|
{
|
|
|
|
|
for(int r=0;r<9;r++)
|
|
|
|
|
{
|
|
|
|
|
a[0][r]=board[q][p];
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
for(q=0;q<3;q++)//<2F><><EFBFBD><EFBFBD>2
|
|
|
|
|
{
|
|
|
|
|
for(p=3;p<6;p++)
|
|
|
|
|
{
|
|
|
|
|
for(int r=0;r<9;r++)
|
|
|
|
|
{
|
|
|
|
|
a[1][r]=board[q][p];
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
for(q=0;q<3;q++)//<2F><><EFBFBD><EFBFBD>3
|
|
|
|
|
{
|
|
|
|
|
for(p=6;p<9;p++)
|
|
|
|
|
{
|
|
|
|
|
for(int r=0;r<9;r++)
|
|
|
|
|
{
|
|
|
|
|
a[2][r]=board[q][p];
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
for(q=3;q<6;q++)//<2F><><EFBFBD><EFBFBD>4
|
|
|
|
|
{
|
|
|
|
|
for(p=0;p<3;p++)
|
|
|
|
|
{
|
|
|
|
|
for(int r=0;r<9;r++)
|
|
|
|
|
{
|
|
|
|
|
a[3][r]=board[q][p];
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
for(q=3;q<6;q++)//<2F><><EFBFBD><EFBFBD>5
|
|
|
|
|
{
|
|
|
|
|
for(p=3;p<6;p++)
|
|
|
|
|
{
|
|
|
|
|
for(int r=0;r<9;r++)
|
|
|
|
|
{
|
|
|
|
|
a[4][r]=board[q][p];
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
for(q=3;q<6;q++)//<2F><><EFBFBD><EFBFBD>6
|
|
|
|
|
{
|
|
|
|
|
for(p=6;p<9;p++)
|
|
|
|
|
{
|
|
|
|
|
for(int r=0;r<9;r++)
|
|
|
|
|
{
|
|
|
|
|
a[5][r]=board[q][p];
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
for(q=6;q<9;q++)//<2F><><EFBFBD><EFBFBD>7
|
|
|
|
|
{
|
|
|
|
|
for(p=0;p<3;p++)
|
|
|
|
|
{
|
|
|
|
|
for(int r=0;r<9;r++)
|
|
|
|
|
{
|
|
|
|
|
a[6][r]=board[q][p];
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
for(q=6;q<9;q++)//<2F><><EFBFBD><EFBFBD>8
|
|
|
|
|
{
|
|
|
|
|
for(p=3;p<6;p++)
|
|
|
|
|
{
|
|
|
|
|
for(int r=0;r<9;r++)
|
|
|
|
|
{
|
|
|
|
|
a[7][r]=board[q][p];
|
|
|
|
|
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|
|
|
|
if(board[i][j]==board[i][k]&&j!=k&&board[i][j]!=0)
|
|
|
|
|
{
|
|
|
|
|
n=board[i][j];
|
|
|
|
|
return 0;//<2F><>ijһ<C4B3><D2BB><EFBFBD>ظ<EFBFBD><D8B8><EFBFBD><EFBFBD><EFBFBD>
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
for(i=0;i<9;i++)//<2F>ж<EFBFBD><D0B6><EFBFBD><EFBFBD>Ƿ<EFBFBD><C7B7>ظ<EFBFBD>
|
|
|
|
|
{
|
|
|
|
|
for(j=0;j<9;j++)
|
|
|
|
|
{
|
|
|
|
|
for(k=0;k<9;k++)
|
|
|
|
|
{
|
|
|
|
|
if(board[j][i]=board[k][i]&&j!=k&&board[j][i]!=0)
|
|
|
|
|
{
|
|
|
|
|
n=board[j][i];
|
|
|
|
|
return 0;//<2F><>ijһ<C4B3><D2BB><EFBFBD>ظ<EFBFBD><D8B8><EFBFBD><EFBFBD><EFBFBD>
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
for(i=0;i<9;i++)//<2F><><EFBFBD>ж<EFBFBD><D0B6><EFBFBD><EFBFBD>Ƿ<EFBFBD><C7B7>ظ<EFBFBD>
|
|
|
|
|
{
|
|
|
|
|
for(j=0;j<9;j++)
|
|
|
|
|
{
|
|
|
|
|
for(k=0;k<9;k++)//<2F>Ե<EFBFBD>j<EFBFBD>е<EFBFBD><D0B5><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ж<EFBFBD><D0B6>ڸ<EFBFBD><DAB8><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ƿ<EFBFBD><C7B7>ظ<EFBFBD><D8B8><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ظ<EFBFBD><D8B8><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ѭ<EFBFBD><D1AD>
|
|
|
|
|
{
|
|
|
|
|
if(a[i][j]==a[i][k]&&j!=k&&a[i][j]!=0)
|
|
|
|
|
{
|
|
|
|
|
n=a[i][j];
|
|
|
|
|
return 0;//<2F><>ijһ<C4B3><D2BB><EFBFBD>ظ<EFBFBD><D8B8><EFBFBD><EFBFBD><EFBFBD>
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}return 1;}
|
|
|
|
|
|
|
|
|
|
void calculation(int array[9][9], int row, int col)
|
|
|
|
|
{
|
|
|
|
|
void output(int arry[9][9]);
|
|
|
|
|
int dd(int board[][9]);
|
|
|
|
|
int i, j;
|
|
|
|
|
if (row > 8)
|
|
|
|
|
{
|
|
|
|
|
if(dd(array)!=0)
|
|
|
|
|
{
|
|
|
|
|
for (i = 0; i < 9; i++)
|
|
|
|
|
{
|
|
|
|
|
output(array);
|
|
|
|
|
}
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
if(dd(array)==0)
|
|
|
|
|
{
|
|
|
|
|
printf("No solution!");
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
if (array[row][col] == 0)
|
|
|
|
|
{
|
|
|
|
|
for (i = 1; i < 10; i++)
|
|
|
|
|
{
|
|
|
|
|
array[row][col] = i;
|
|
|
|
|
if (dd(array))
|
|
|
|
|
{
|
|
|
|
|
array[row][col] = i;
|
|
|
|
|
calculation(array, row + (col + 1) / 9, (col + 1) % 9);
|
|
|
|
|
}
|
|
|
|
|
array[row][col] = 0;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
calculation(array, row + (col + 1) / 9, (col + 1) % 9);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
int main()
|
|
|
|
|
{
|
|
|
|
|
void output(int arry[9][9]);
|
|
|
|
|
void cc(int z,int board[][9]);
|
|
|
|
|
void pro3(int z,int board[][9]);
|
|
|
|
|
void calculation(int array[9][9], int row, int col);
|
|
|
|
|
int t=1;
|
|
|
|
|
int board[9][9]={
|
|
|
|
|
{5,3,0,0,7,0,0,0,0},
|
|
|
|
|
{6,0,0,1,9,5,0,0,0},
|
|
|
|
|
{0,9,4,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");
|
|
|
|
|
output(board);
|
|
|
|
|
pro3(t,board);
|
|
|
|
|
if(t==1){
|
|
|
|
|
printf("The solution of Sudoku matrix:\n");
|
|
|
|
|
calculation(board, 0, 0);}//<2F>ӵ<EFBFBD>һ<EFBFBD><D2BB><EFBFBD><EFBFBD>Ԫ<EFBFBD><D4AA>ʼ<EFBFBD><CABC><EFBFBD>ȼ<EFBFBD><C8BC><EFBFBD>
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
void pro3(int z,int board[][9]){
|
|
|
|
|
int a[9][9];//<2F><><EFBFBD>ò<EFBFBD>ͬ<EFBFBD><CDAC><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
|
|
|
|
int q,p,r=0;
|
|
|
|
|
for(q=0;q<3;q++)//<2F><><EFBFBD><EFBFBD>1
|
|
|
|
|
{
|
|
|
|
|
for(p=0;p<3;p++)
|
|
|
|
|
{
|
|
|
|
|
a[0][r++]=board[q][p];
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
}r=0;
|
|
|
|
|
for(q=0;q<3;q++)//<2F><><EFBFBD><EFBFBD>2
|
|
|
|
|
{
|
|
|
|
|
for(p=3;p<6;p++)
|
|
|
|
|
{
|
|
|
|
|
a[1][r++]=board[q][p];
|
|
|
|
|
}
|
|
|
|
|
}r=0;
|
|
|
|
|
for(q=0;q<3;q++)//<2F><><EFBFBD><EFBFBD>3
|
|
|
|
|
{
|
|
|
|
|
for(p=6;p<9;p++)
|
|
|
|
|
{
|
|
|
|
|
a[2][r++]=board[q][p];
|
|
|
|
|
}
|
|
|
|
|
}r=0;
|
|
|
|
|
for(q=3;q<6;q++)//<2F><><EFBFBD><EFBFBD>4
|
|
|
|
|
{
|
|
|
|
|
for(p=0;p<3;p++)
|
|
|
|
|
{
|
|
|
|
|
a[3][r++]=board[q][p];
|
|
|
|
|
}
|
|
|
|
|
}r=0;
|
|
|
|
|
for(q=3;q<6;q++)
|
|
|
|
|
{
|
|
|
|
|
for(p=3;p<6;p++)
|
|
|
|
|
{
|
|
|
|
|
a[4][r++]=board[q][p];
|
|
|
|
|
}
|
|
|
|
|
}r=0;
|
|
|
|
|
for(q=3;q<6;q++)
|
|
|
|
|
{
|
|
|
|
|
for(p=6;p<9;p++)
|
|
|
|
|
{
|
|
|
|
|
a[5][r++]=board[q][p];
|
|
|
|
|
}
|
|
|
|
|
}r=0;
|
|
|
|
|
for(q=6;q<9;q++)
|
|
|
|
|
{
|
|
|
|
|
for(p=0;p<3;p++)
|
|
|
|
|
{
|
|
|
|
|
a[6][r++]=board[q][p];
|
|
|
|
|
}
|
|
|
|
|
}r=0;
|
|
|
|
|
for(q=6;q<9;q++)
|
|
|
|
|
{
|
|
|
|
|
for(p=3;p<6;p++)
|
|
|
|
|
{
|
|
|
|
|
a[7][r++]=board[q][p];
|
|
|
|
|
}
|
|
|
|
|
}r=0;
|
|
|
|
|
for(q=6;q<9;q++)
|
|
|
|
|
{
|
|
|
|
|
for(p=6;p<9;p++)
|
|
|
|
|
{
|
|
|
|
|
a[8][r++]=board[q][p];
|
|
|
|
|
}
|
|
|
|
|
}r=0;
|
|
|
|
|
//<2F><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD> ,<2C><>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<9;j++)
|
|
|
|
|
{
|
|
|
|
|
for(k=0;k<9;k++)
|
|
|
|
|
{
|
|
|
|
|
if(board[j][i]=board[k][i]&&j!=k&&board[j][i]!=0)
|
|
|
|
|
{
|
|
|
|
|
n=board[j][i];
|
|
|
|
|
goto flagtwo;//<2F><>ijһ<C4B3><D2BB><EFBFBD>ظ<EFBFBD><D8B8><EFBFBD><EFBFBD><EFBFBD>
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
for(i=0;i<9;i++)//<2F><><EFBFBD>ж<EFBFBD><D0B6><EFBFBD><EFBFBD>Ƿ<EFBFBD><C7B7>ظ<EFBFBD>
|
|
|
|
|
{
|
|
|
|
|
for(j=0;j<9;j++)
|
|
|
|
|
{
|
|
|
|
|
for(k=0;k<9;k++)//<2F>Ե<EFBFBD>j<EFBFBD>е<EFBFBD><D0B5><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ж<EFBFBD><D0B6>ڸ<EFBFBD><DAB8><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ƿ<EFBFBD><C7B7>ظ<EFBFBD><D8B8><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ظ<EFBFBD><D8B8><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ѭ<EFBFBD><D1AD>
|
|
|
|
|
{
|
|
|
|
|
if(a[i][j]==a[i][k]&&j!=k&&a[i][j]!=0)
|
|
|
|
|
{
|
|
|
|
|
n=a[i][j];
|
|
|
|
|
goto flagthree;//<2F><>ijһ<C4B3><D2BB><EFBFBD>ظ<EFBFBD><D8B8><EFBFBD><EFBFBD><EFBFBD>
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
if(n!=0){
|
|
|
|
|
flagone:
|
|
|
|
|
{
|
|
|
|
|
printf("False:Invalid initial Sudoku matrix!\n");
|
|
|
|
|
printf("The number %d in the col %d has been used!\n",n,i+1);
|
|
|
|
|
z=0;return;}
|
|
|
|
|
flagtwo:
|
|
|
|
|
{
|
|
|
|
|
printf("False:Invalid initial Sudoku matrix!\n");
|
|
|
|
|
printf("The number %d in the row %d has been used!\n",n,i+1);
|
|
|
|
|
z=0;return;}
|
|
|
|
|
flagthree:
|
|
|
|
|
{
|
|
|
|
|
printf("False:Invalid initial Sudoku matrix!\n");
|
|
|
|
|
printf("The number %d in the block %d has been used!\n",n,i+1);
|
|
|
|
|
z=0;return;}}
|
|
|
|
|
}
|