|
|
#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];//设置不同的区块
|
|
|
int q,p;
|
|
|
for(q=0;q<3;q++)//区块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++)//区块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++)//区块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++)//区块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++)//区块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++)//区块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++)//区块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++)//区块8
|
|
|
{
|
|
|
for(p=3;p<6;p++)
|
|
|
{
|
|
|
for(int r=0;r<9;r++)
|
|
|
{
|
|
|
a[7][r]=board[q][p];
|
|
|
}
|
|
|
}
|
|
|
}
|
|
|
for(q=6;q<9;q++)//区块9
|
|
|
{
|
|
|
for(p=6;p<9;p++)
|
|
|
{
|
|
|
for(int r=0;r<9;r++)
|
|
|
{
|
|
|
a[8][r]=board[q][p];
|
|
|
}
|
|
|
}
|
|
|
}
|
|
|
//设置区块结束 ,将每个区块的数转移为每一行的数,再判断行是否重复
|
|
|
|
|
|
int i,j,k,t=0;
|
|
|
int n;
|
|
|
for(i=0;i<9;i++)//先判断行是否重复
|
|
|
{
|
|
|
for(j=0;j<9;j++)
|
|
|
{
|
|
|
for(k=0;k<9;k++)//对第j行的数进行判断在该行内是否重复,若重复则跳出循环
|
|
|
{
|
|
|
if(board[i][j]==board[i][k]&&j!=k&&board[i][j]!=0)
|
|
|
{
|
|
|
n=board[i][j];
|
|
|
return 0;//在某一行重复出现
|
|
|
}
|
|
|
}
|
|
|
}
|
|
|
}
|
|
|
for(i=0;i<9;i++)//判断列是否重复
|
|
|
{
|
|
|
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;//在某一列重复出现
|
|
|
}
|
|
|
}
|
|
|
}
|
|
|
}
|
|
|
for(i=0;i<9;i++)//先判断行是否重复
|
|
|
{
|
|
|
for(j=0;j<9;j++)
|
|
|
{
|
|
|
for(k=0;k<9;k++)//对第j行的数进行判断在该行内是否重复,若重复则跳出循环
|
|
|
{
|
|
|
if(a[i][j]==a[i][k]&&j!=k&&a[i][j]!=0)
|
|
|
{
|
|
|
n=a[i][j];
|
|
|
return 0;//在某一行重复出现
|
|
|
}
|
|
|
}
|
|
|
}
|
|
|
}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);}//从第一个单元开始深度检索
|
|
|
return 0;
|
|
|
}
|
|
|
void pro3(int z,int board[][9]){
|
|
|
int a[9][9];//设置不同的区块
|
|
|
int q,p,r=0;
|
|
|
for(q=0;q<3;q++)//区块1
|
|
|
{
|
|
|
for(p=0;p<3;p++)
|
|
|
{
|
|
|
a[0][r++]=board[q][p];
|
|
|
|
|
|
}
|
|
|
}r=0;
|
|
|
for(q=0;q<3;q++)//区块2
|
|
|
{
|
|
|
for(p=3;p<6;p++)
|
|
|
{
|
|
|
a[1][r++]=board[q][p];
|
|
|
}
|
|
|
}r=0;
|
|
|
for(q=0;q<3;q++)//区块3
|
|
|
{
|
|
|
for(p=6;p<9;p++)
|
|
|
{
|
|
|
a[2][r++]=board[q][p];
|
|
|
}
|
|
|
}r=0;
|
|
|
for(q=3;q<6;q++)//区块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;
|
|
|
//设置区块结束 ,将每个区块的数转移为每一行的数,再判断行是否重复
|
|
|
int i,j,k,t=0;
|
|
|
int n=0;
|
|
|
for(i=0;i<9;i++)//先判断行是否重复
|
|
|
{
|
|
|
for(j=0;j<9;j++)
|
|
|
{
|
|
|
for(k=0;k<9;k++)//对第j行的数进行判断在该行内是否重复,若重复则跳出循环
|
|
|
{
|
|
|
if(board[i][j]==board[i][k]&&j!=k&&board[i][j]!=0)
|
|
|
{
|
|
|
n=board[i][j];
|
|
|
goto flagone;//在某一行重复出现
|
|
|
}
|
|
|
}
|
|
|
}
|
|
|
}
|
|
|
for(i=0;i<9;i++)//判断列是否重复
|
|
|
{
|
|
|
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];
|
|
|
goto flagtwo;//在某一列重复出现
|
|
|
}
|
|
|
}
|
|
|
}
|
|
|
}
|
|
|
for(i=0;i<9;i++)//先判断行是否重复
|
|
|
{
|
|
|
for(j=0;j<9;j++)
|
|
|
{
|
|
|
for(k=0;k<9;k++)//对第j行的数进行判断在该行内是否重复,若重复则跳出循环
|
|
|
{
|
|
|
if(a[i][j]==a[i][k]&&j!=k&&a[i][j]!=0)
|
|
|
{
|
|
|
n=a[i][j];
|
|
|
goto flagthree;//在某一行重复出现
|
|
|
}
|
|
|
}
|
|
|
}
|
|
|
}
|
|
|
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;}}
|
|
|
}
|