|
|
|
|
#include<stdio.h>
|
|
|
|
|
|
|
|
|
|
int main()
|
|
|
|
|
{
|
|
|
|
|
//<2F><><EFBFBD>ú<EFBFBD><C3BA><EFBFBD>
|
|
|
|
|
void Printf(char* p0);
|
|
|
|
|
int Sudoku(char* p0);
|
|
|
|
|
void Sudokuprint(char* p0);
|
|
|
|
|
void Sudokuback(int k,char* p0);
|
|
|
|
|
//<2F><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD> <20><><EFBFBD><EFBFBD>ӡ
|
|
|
|
|
char board[9][9]={
|
|
|
|
|
{'3','.','4','.','.','.','1','.','2'},
|
|
|
|
|
{'.','.','.','2','3','8','.','.','.'},
|
|
|
|
|
{'9','.','.','.','.','4','.','.','6'},
|
|
|
|
|
{'.','3','5','.','7','.','.','6','.'},
|
|
|
|
|
{'.','4','.','3','.','5','.','8','.'},
|
|
|
|
|
{'.','7','.','.','9','.','2','5','.'},
|
|
|
|
|
{'8','.','.','6','.','.','.','.','7'},
|
|
|
|
|
{'.','.','.','9','4','7','.','.','.'},
|
|
|
|
|
{'4','.','7','.','.','.','5','.','9'}};
|
|
|
|
|
char* p0 = board;
|
|
|
|
|
printf("The original Sudoku matrix:\n");
|
|
|
|
|
Printf(p0);
|
|
|
|
|
int i,j,k;
|
|
|
|
|
//<2F><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD> <20><><EFBFBD>û<EFBFBD><C3BB>ݺ<EFBFBD><DDBA><EFBFBD>
|
|
|
|
|
if(Sudoku(p0))
|
|
|
|
|
{
|
|
|
|
|
printf("True:Valid initial Sudoku matrix!\n");
|
|
|
|
|
Sudokuback(0, p0);
|
|
|
|
|
}
|
|
|
|
|
//<2F><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
printf("False:Invalid initial Sudoku matrix!\n");
|
|
|
|
|
Sudokuprint(p0);
|
|
|
|
|
printf("No solution\n");
|
|
|
|
|
}
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
//<2F><>ӡ<EFBFBD><D3A1><EFBFBD><EFBFBD>
|
|
|
|
|
void Printf(char* p0)
|
|
|
|
|
{
|
|
|
|
|
int i,j;
|
|
|
|
|
for(i=0;i<9;i++)
|
|
|
|
|
{
|
|
|
|
|
for(j=0;j<9;j++)
|
|
|
|
|
{
|
|
|
|
|
printf("%c ",p0[9*i+j]);
|
|
|
|
|
}
|
|
|
|
|
printf("\n");
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
//<2F>ж<EFBFBD><D0B6><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ӡ
|
|
|
|
|
int Sudoku(char* p0)
|
|
|
|
|
{
|
|
|
|
|
int i,j,number,s,q,p,t,g;
|
|
|
|
|
//<2F>˴<EFBFBD><CBB4><EFBFBD><EFBFBD><EFBFBD>1
|
|
|
|
|
int c[10]={0,0,0,0,0,0,0,0,0,0};//<2F><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ֳ<EFBFBD><D6B3>ֵĸ<D6B5><C4B8><EFBFBD>
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
//<2F><><EFBFBD><EFBFBD>С<EFBFBD><D0A1><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ж<EFBFBD>
|
|
|
|
|
int w[10]={1,2,3,4,5,6,7,8,9};
|
|
|
|
|
int k=0;
|
|
|
|
|
for(t=3;t<=9;t=t+3)
|
|
|
|
|
{
|
|
|
|
|
for(g=3;g<=9;g=g+3)
|
|
|
|
|
{
|
|
|
|
|
for(i=t-3;i<t;i++)
|
|
|
|
|
{
|
|
|
|
|
for(j=g-3;j<g;j++)//<2F>ֳ<EFBFBD><D6B3><EFBFBD>С<EFBFBD><D0A1><EFBFBD><EFBFBD>
|
|
|
|
|
{
|
|
|
|
|
for(number=1;number<=9;number++)
|
|
|
|
|
{
|
|
|
|
|
if(p0[9*i+j] == number + '0')
|
|
|
|
|
{c[number]++;}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
for(q=1;q<=9;q++)
|
|
|
|
|
{
|
|
|
|
|
if(c[q]>1)
|
|
|
|
|
{
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
k++;
|
|
|
|
|
for(s=0;s<=9;s++)c[s]=0;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
//<2F><><EFBFBD><EFBFBD><EFBFBD>е<EFBFBD><D0B5>ж<EFBFBD>
|
|
|
|
|
for(i=0;i<9;i++)
|
|
|
|
|
{
|
|
|
|
|
|
|
|
|
|
for(number=1;number<=9;number++)
|
|
|
|
|
{
|
|
|
|
|
for(j=0;j<9;j++)
|
|
|
|
|
{
|
|
|
|
|
if(p0[9*i+j] == number + '0')
|
|
|
|
|
c[number]++;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
for(q=1;q<=9;q++)
|
|
|
|
|
{
|
|
|
|
|
if(c[q]>1)
|
|
|
|
|
{
|
|
|
|
|
return 0 ;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
for(p=0;p<10;p++)c[p]=0;
|
|
|
|
|
}
|
|
|
|
|
//ͬ<><CDAC><EFBFBD>е<EFBFBD><D0B5>ж<EFBFBD>
|
|
|
|
|
for(i=0;i<9;i++)
|
|
|
|
|
{
|
|
|
|
|
for(number=1;number<=9;number++)
|
|
|
|
|
{
|
|
|
|
|
for(j=0;j<9;j++)
|
|
|
|
|
{
|
|
|
|
|
if(p0[9*j+i] == number + '0')
|
|
|
|
|
c[number]++;}
|
|
|
|
|
}
|
|
|
|
|
for(q=1;q<=9;q++)
|
|
|
|
|
{
|
|
|
|
|
if(c[q]>1)
|
|
|
|
|
{
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
for(p=0;p<10;p++)c[p]=0;
|
|
|
|
|
}
|
|
|
|
|
return 1;
|
|
|
|
|
}
|
|
|
|
|
//<2F>ж<EFBFBD><D0B6><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ӡ
|
|
|
|
|
void Sudokuprint(char* p0)
|
|
|
|
|
{
|
|
|
|
|
|
|
|
|
|
int i, j, number, s, q, p, t, g;
|
|
|
|
|
//<2F>˴<EFBFBD><CBB4><EFBFBD><EFBFBD><EFBFBD>1
|
|
|
|
|
int c[10] = { 0,0,0,0,0,0,0,0,0,0 };//<2F><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ֳ<EFBFBD><D6B3>ֵĸ<D6B5><C4B8><EFBFBD>
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
//<2F><><EFBFBD><EFBFBD>С<EFBFBD><D0A1><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ж<EFBFBD>
|
|
|
|
|
int w[10] = { 1,2,3,4,5,6,7,8,9 };
|
|
|
|
|
int k = 0;
|
|
|
|
|
for (t = 3;t <= 9;t = t + 3)
|
|
|
|
|
{
|
|
|
|
|
for (g = 3;g <= 9;g = g + 3)
|
|
|
|
|
{
|
|
|
|
|
for (i = t - 3;i < t;i++)
|
|
|
|
|
{
|
|
|
|
|
for (j = g - 3;j < g;j++)//<2F>ֳ<EFBFBD><D6B3><EFBFBD>С<EFBFBD><D0A1><EFBFBD><EFBFBD>
|
|
|
|
|
{
|
|
|
|
|
for (number = 1;number <= 9;number++)
|
|
|
|
|
{
|
|
|
|
|
if (p0[9 * i + j] == number + '0')
|
|
|
|
|
{
|
|
|
|
|
c[number]++;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
for (q = 1;q <= 9;q++)
|
|
|
|
|
{
|
|
|
|
|
if (c[q] > 1)
|
|
|
|
|
{
|
|
|
|
|
printf("The number %d in the block %d has been used!\n", q, w[k]);
|
|
|
|
|
return ;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
k++;
|
|
|
|
|
for (s = 0;s <= 9;s++)c[s] = 0;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
//<2F><><EFBFBD><EFBFBD><EFBFBD>е<EFBFBD><D0B5>ж<EFBFBD>
|
|
|
|
|
for (i = 0;i < 9;i++)
|
|
|
|
|
{
|
|
|
|
|
|
|
|
|
|
for (number = 1;number <= 9;number++)
|
|
|
|
|
{
|
|
|
|
|
for (j = 0;j < 9;j++)
|
|
|
|
|
{
|
|
|
|
|
if (p0[9 * i + j] == number + '0')
|
|
|
|
|
c[number]++;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
for (q = 1;q <= 9;q++)
|
|
|
|
|
{
|
|
|
|
|
if (c[q] > 1)
|
|
|
|
|
{
|
|
|
|
|
printf("The number %d in the col %d has been used!\n", q, i + 1);
|
|
|
|
|
return ;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
for (p = 0;p < 10;p++)c[p] = 0;
|
|
|
|
|
}
|
|
|
|
|
//ͬ<><CDAC><EFBFBD>е<EFBFBD><D0B5>ж<EFBFBD>
|
|
|
|
|
for (i = 0;i < 9;i++)
|
|
|
|
|
{
|
|
|
|
|
for (number = 1;number <= 9;number++)
|
|
|
|
|
{
|
|
|
|
|
for (j = 0;j < 9;j++)
|
|
|
|
|
{
|
|
|
|
|
if (p0[9 * j + i] == number + '0')
|
|
|
|
|
c[number]++;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
for (q = 1;q <= 9;q++)
|
|
|
|
|
{
|
|
|
|
|
if (c[q] > 1)
|
|
|
|
|
{
|
|
|
|
|
printf("The number %d in the column %d has been used!\n", q, i + 1);
|
|
|
|
|
return ;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
for (p = 0;p < 10;p++)c[p] = 0;
|
|
|
|
|
}
|
|
|
|
|
return ;
|
|
|
|
|
}
|
|
|
|
|
//<2F><><EFBFBD>ݷ<EFBFBD><DDB7><EFBFBD><EFBFBD><EFBFBD>
|
|
|
|
|
void Sudokuback(int k,char* p0)
|
|
|
|
|
{
|
|
|
|
|
int m;
|
|
|
|
|
if (k == 81)
|
|
|
|
|
{
|
|
|
|
|
if (Sudoku(p0))
|
|
|
|
|
{
|
|
|
|
|
printf("The solution of Sudoku matrix:\n");
|
|
|
|
|
Printf(p0);
|
|
|
|
|
}
|
|
|
|
|
else printf("No sulotion\n");
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
if (p0[k] == '.')
|
|
|
|
|
{
|
|
|
|
|
for (m = 1;m <= 9;m++)
|
|
|
|
|
{
|
|
|
|
|
p0[k] = m+'0';
|
|
|
|
|
if (Sudoku(p0))
|
|
|
|
|
{
|
|
|
|
|
Sudokuback(k + 1,p0);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
else if (!Sudoku(p0)&&m==9)
|
|
|
|
|
{
|
|
|
|
|
p0[k] = '.';
|
|
|
|
|
return ;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
Sudokuback(k + 1, p0);
|
|
|
|
|
}
|
|
|
|
|
}
|