|
|
|
|
#include<stdio.h>
|
|
|
|
|
int main()
|
|
|
|
|
{
|
|
|
|
|
bool fill(int board_[9][9]);
|
|
|
|
|
int JudgeMent(int board[9][9]);
|
|
|
|
|
bool judge = 1;
|
|
|
|
|
int result = 0;
|
|
|
|
|
int b_0[9][9];
|
|
|
|
|
for ( int i=0;i<9;i++)
|
|
|
|
|
{
|
|
|
|
|
for(int j=0;j<9;j++)
|
|
|
|
|
{
|
|
|
|
|
scanf("%d",&b_0[i][j]);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}//<2F><>ȡ<EFBFBD><C8A1><EFBFBD><EFBFBD>
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
judge=JudgeMent(b_0);
|
|
|
|
|
if(judge==1)
|
|
|
|
|
{
|
|
|
|
|
|
|
|
|
|
if(!fill(b_0))
|
|
|
|
|
{
|
|
|
|
|
printf("No solution");
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
printf("|-----------------------|\n");
|
|
|
|
|
for(int i=0;i<9;i++)
|
|
|
|
|
{
|
|
|
|
|
printf("| ");
|
|
|
|
|
for (int j=0;j<9;j++)
|
|
|
|
|
{
|
|
|
|
|
printf("%d ",b_0[i][j]);
|
|
|
|
|
if(j==2||j==5)printf("| ");
|
|
|
|
|
if(j==8) printf("|\n");
|
|
|
|
|
}
|
|
|
|
|
if(i==2||i==5||i==8)printf("|-----------------------|\n");
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
bool fill(int board_[9][9])
|
|
|
|
|
{
|
|
|
|
|
bool isValid(int board[9][9], int row, int col, int k);
|
|
|
|
|
int JudgeMent(int board[9][9]);
|
|
|
|
|
int Judgement(int board[9][9]);
|
|
|
|
|
for ( int i=0;i<9;i++)
|
|
|
|
|
{
|
|
|
|
|
for(int j=0;j<9;j++)
|
|
|
|
|
{
|
|
|
|
|
if(board_[i][j]!=0)
|
|
|
|
|
{
|
|
|
|
|
//b_0[i][j]++;
|
|
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
for(int k=1;k<=9;k++)
|
|
|
|
|
{
|
|
|
|
|
if(isValid( board_, i, j, k)) {
|
|
|
|
|
board_[i][j] = k;
|
|
|
|
|
/* <20>ж<EFBFBD><D0B6><EFBFBD>һ<EFBFBD><D2BB><EFBFBD>ݹ<EFBFBD>֮<EFBFBD><D6AE><EFBFBD>Ƿ<EFBFBD><C7B7>ҵ<EFBFBD>һ<EFBFBD>ֽⷨ<D6BD><E2B7A8><EFBFBD><EFBFBD><EFBFBD><EFBFBD>true */
|
|
|
|
|
if (fill(board_))
|
|
|
|
|
{
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
/* <20><><EFBFBD>ݣ<EFBFBD><DDA3><EFBFBD><EFBFBD><EFBFBD>ǰλ<C7B0><CEBB><EFBFBD><EFBFBD><EFBFBD><EFBFBD> */
|
|
|
|
|
board_[i][j] = 0;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
return 1;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
int JudgeMent(int board[9][9])
|
|
|
|
|
{
|
|
|
|
|
int parallel[9][9]={0};//<2F><><EFBFBD>ڼ<EFBFBD><DABC><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ÿ<EFBFBD><C3BF>1-9<><39><EFBFBD>ֵĴ<D6B5><C4B4><EFBFBD>
|
|
|
|
|
int vertical[9][9]={0}; //<2F><><EFBFBD>ڼ<EFBFBD><DABC><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ÿ<EFBFBD><C3BF>1-9<><39><EFBFBD>ֵĴ<D6B5><C4B4><EFBFBD>
|
|
|
|
|
int a[9][9]={0}; //<2F><><EFBFBD>ڼ<EFBFBD><DABC><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ÿ<EFBFBD><C3BF>1-9<><39><EFBFBD>ֵĴ<D6B5><C4B4><EFBFBD>
|
|
|
|
|
int element,line=0, column=0, block =0;
|
|
|
|
|
int i,j,k;//<2F><><EFBFBD>ڿ<EFBFBD><DABF><EFBFBD>ѭ<EFBFBD><D1AD>
|
|
|
|
|
int err01,err02,err12,err11,err21,err22;//<2F><>¼<EFBFBD><C2BC><EFBFBD>ݳ<EFBFBD><DDB3><EFBFBD><EFBFBD><EFBFBD>λ<EFBFBD><CEBB>
|
|
|
|
|
bool judgement=0;
|
|
|
|
|
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");
|
|
|
|
|
}//<2F><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>飻
|
|
|
|
|
|
|
|
|
|
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]++;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}//ͳ<>Ƹ<EFBFBD><C6B8>и<EFBFBD><D0B8><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ֳ<EFBFBD><D6B3>ֵĴ<D6B5><C4B4><EFBFBD>
|
|
|
|
|
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;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}//<2F>ж<EFBFBD><D0B6><EFBFBD>
|
|
|
|
|
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;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
//<2F>ж<EFBFBD><D0B6><EFBFBD>
|
|
|
|
|
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;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
//<2F>жϿ<D0B6>
|
|
|
|
|
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");
|
|
|
|
|
judgement=1;
|
|
|
|
|
}
|
|
|
|
|
return judgement;
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
bool isValid(int board[9][9], int row, int col, int k) {
|
|
|
|
|
// <20>жϵ<D0B6>ǰ<EFBFBD><C7B0><EFBFBD>Ƿ<EFBFBD><C7B7><EFBFBD><EFBFBD>ظ<EFBFBD>Ԫ<EFBFBD><D4AA>
|
|
|
|
|
for (int i = 0; i < 9; i++) {
|
|
|
|
|
if (board[i][col] == k) {
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
// <20>жϵ<D0B6>ǰ<EFBFBD><C7B0><EFBFBD>Ƿ<EFBFBD><C7B7><EFBFBD><EFBFBD>ظ<EFBFBD>Ԫ<EFBFBD><D4AA>
|
|
|
|
|
for (int j = 0; j < 9; j++) {
|
|
|
|
|
if (board[row][j] == k) {
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
// <20><><EFBFBD>㵱ǰ9<C7B0><39><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ͻǵ<CFBD>λ<EFBFBD><CEBB>
|
|
|
|
|
int startRow = (row / 3) * 3;
|
|
|
|
|
int startCol = (col / 3) * 3;
|
|
|
|
|
//<2F>жϵ<D0B6>ǰԪ<C7B0><D4AA><EFBFBD><EFBFBD><EFBFBD>ھŹ<DABE><C5B9><EFBFBD><EFBFBD>Ƿ<EFBFBD><C7B7><EFBFBD><EFBFBD>ظ<EFBFBD>Ԫ<EFBFBD><D4AA>
|
|
|
|
|
for (int i = startRow; i < startRow + 3; i++) {
|
|
|
|
|
for (int j = startCol; j < startCol + 3; j++) {
|
|
|
|
|
if (board[i][j] == k) {
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
//<2F><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>true
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|