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.

250 lines
5.0 KiB

#include<stdio.h>
void output(int a[9][9]);
void complete(int a[9][9], int m, int n);
int check(int a[9][9], int row, int col, int num);
int judge(int a[9][9], int b);
void W(int a[9][9]);
//<2F><><EFBFBD><EFBFBD><EFBFBD>ģ<EFBFBD><C4A3>ж<EFBFBD> <20><><EFBFBD><EFBFBD><EFBFBD>Ƿ<EFBFBD><C7B7><EFBFBD><EFBFBD><EFBFBD><E3B6A8> <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
int main()
{
int board0[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 board1[9][9] = { {8, 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 board2[9][9] = { {5, 2, 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} };
W(board0);
W(board1);
W(board2);
}
void output(int a[9][9])//<2F><><EFBFBD><EFBFBD><E5BAAF><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ʽ<EFBFBD><CABD>ӡ<EFBFBD><D3A1><EFBFBD><EFBFBD>
{
for (int i = 0; i < 9; i++)
{
printf("|");//ÿһ<C3BF>е<EFBFBD><D0B5><EFBFBD>|<7C><>ʼ
for (int j = 0; j < 9; j++)
{
printf("%d", a[i][j]);
if ((j + 1) % 3 == 0)
{
printf("|");//<2F><><EFBFBD><EFBFBD>3*3
if (j == 8)
{
printf("\n");
}
}
}
if ((i + 1) % 3 == 0 && i != 8)
{
printf("|----<2D>ָ<EFBFBD>---|\n");
}//ÿ<><C3BF><EFBFBD>оͻ<D0BE>һ<EFBFBD><D2BB>
}
}
int check(int a[9][9], int row, int col, int num)
//<2F><><EFBFBD><EFBFBD>ij<EFBFBD><C4B3><EFBFBD>ܷ<EFBFBD><DCB7><EFBFBD><EFBFBD><EFBFBD>ijλ<C4B3>ã<EFBFBD><C3A3>ڲ<EFBFBD>ȫ<EFBFBD><C8AB><EFBFBD><EFBFBD><EFBFBD>ĺ<EFBFBD><C4BA><EFBFBD>complete<74>лᱻ<D0BB><E1B1BB><EFBFBD><EFBFBD>
{
// <20><><EFBFBD><EFBFBD>
for (int i = 0; i < 9; i++)
{
if (a[row][i] == num) return 0;
}
// <20><><EFBFBD><EFBFBD>
for (int i = 0; i < 9; i++)
{
if (a[i][col] == num) return 0;
}
// <20>Կ<EFBFBD>
int Row = row - row % 3;
int Col = col - col % 3;
for (int i = 0; i < 3; i++)
{
for (int j = 0; j < 3; j++)
{
if (a[i + Row][j + Col] == num) return 0;
}
}
return 1;
}
void complete(int a[9][9], int m, int n)
{
// <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>һ<EFBFBD><D2BB><EFBFBD><EFBFBD><EFBFBD><EFBFBD>һ<EFBFBD>У<EFBFBD><D0A3><EFBFBD><EFBFBD>سɹ<D8B3>
if (m == 9&&n==9)
{
printf("The solution of Sudoku matrix:\n");
output(a);
return;
}
// <20>ƶ<EFBFBD><C6B6><EFBFBD><EFBFBD><EFBFBD>һ<EFBFBD><D2BB>λ<EFBFBD><CEBB>
if (n == 9)
{
m++;
n = 0;
}
// <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ǰλ<C7B0><CEBB><EFBFBD>Ѿ<EFBFBD><D1BE><EFBFBD><EFBFBD><EFBFBD><EFBFBD>֣<EFBFBD><D6A3>ƶ<EFBFBD><C6B6><EFBFBD><EFBFBD><EFBFBD>һ<EFBFBD><D2BB>λ<EFBFBD><CEBB>
if (a[m][n] != 0)
{
complete(a, m, n + 1);
return;
}
// <20><><EFBFBD><EFBFBD>
for (int i = 1; i <= 9; i++)
{
if (check(a, m, n, i))
{
a[m][n] = i;
complete(a, m, n + 1);
if (m == 9) return;
a[m][n] = 0;
}
}
}
int judge(int a[9][9], int b)//<2F>ж<EFBFBD><D0B6><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ƿ<EFBFBD><C7B7><EFBFBD><EFBFBD><EFBFBD><EFBFBD><E5A3AC><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ж<EFBFBD>ֵ<EFBFBD><D6B5><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ֵΪ0<CEAA><30>ζ<EFBFBD><CEB6><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><E3B6A8>
{
int c = 0;//c=0<><30>ζ<EFBFBD><CEB6><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><E3B6A8>
//b=0˵<30><CBB5><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ӡ<EFBFBD><D3A1>ֻ<EFBFBD>жϣ<D0B6>b=1<><31><EFBFBD><EFBFBD><EFBFBD>ú<EFBFBD><C3BA><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ӡ<EFBFBD><D3A1><EFBFBD><EFBFBD>
//<2F>ж<EFBFBD><D0B6>Ƿ<EFBFBD>ÿһ<C3BF>в<EFBFBD><D0B2><EFBFBD><EFBFBD>ֶ<EFBFBD><D6B6><EFBFBD>
for (int i = 0; i < 9; i++)
{
int times[9] = { 0 };
for (int j = 0; j < 9; j++)//ͳ<>Ƶ<EFBFBD>i<EFBFBD><69>ÿ<EFBFBD><C3BF><EFBFBD><EFBFBD><EFBFBD>ֵij<D6B5><C4B3>ִ<EFBFBD><D6B4><EFBFBD>
{
if(a[i][j]>=1)times[a[i][j] - 1]++;
}
for (int j = 0; j < 9; j++)
{
if (times[j] > 1)
{
c = 1;
if (b == 1)
{ printf("False:Invalid initial Sudoku matrix!\nThe number %d in the row %d has been used!\n", j + 1, i + 1); }
break;
}
}
if (c) { break; }
}
if (c == 0)
{
//<2F>ж<EFBFBD><D0B6>Ƿ<EFBFBD>ÿһ<C3BF>в<EFBFBD><D0B2><EFBFBD><EFBFBD>ֶ<EFBFBD><D6B6><EFBFBD>
for (int j = 0; j < 9; j++)
{
int times[9] = { 0 };
for (int i = 0; i < 9; i++)//ͳ<>Ƶ<EFBFBD>j<EFBFBD><6A>ÿ<EFBFBD><C3BF><EFBFBD><EFBFBD><EFBFBD>ֵij<D6B5><C4B3>ִ<EFBFBD><D6B4><EFBFBD>
{
if (a[i][j] >= 1)times[a[i][j] - 1]++;
}
for (int i = 0; i < 9; i++)
{
if (times[i] > 1)
{
c = 1;
if (b == 1)
{ printf("False:Invalid initial Sudoku matrix!\nThe number %d in the col %d has been used!\n", i + 1, j + 1); }
break;
}
}
if (c) { break; }
}
}
if (c == 0)
{
//<2F>ж<EFBFBD>ÿһ<C3BF><D2BB><EFBFBD>Ƿ񲻳<C7B7><F1B2BBB3>ֶ<EFBFBD><D6B6><EFBFBD>
for (int i = 0; i < 3; i++)
{
for (int j = 0; j < 3; j++)
{
int times[9] = { 0 };
for (int m = 3 * i; m <= 3 * i + 2; m++)
{
for (int n = 3 * j; n <= 3 * j + 2; n++)
{
if (a[m][n] >= 1)times[a[m][n] - 1]++;
}
}
for (int i = 0; i < 9; i++)
{
if (times[i] > 1)
{
c = 1;
if (b == 1)
{ printf("False:Invalid initial Sudoku matrix!\nThe number %d in the block %d has been used!\n", i + 1, i + j + 1); }
break;
}
}
if (c) { break; }
}
if (c) { break; }
}
}
return c;
}
void W(int a[9][9])//<2F><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ļ<EFBFBD><C4BC><EFBFBD><EFBFBD><EFBFBD>Ҫ<EFBFBD>ĺ<EFBFBD><C4BA><EFBFBD>
{
printf("The original Sudoku matrix:\n");
output(a);
if (judge(a, 0) == 1)
{
judge(a, 1);
printf("No solution<6F><6E>\n");
}
else
{
printf("True:Valid initial Sudoku matrix!\n");
complete(a, 0, 0);//<2F><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>IJ<EFBFBD><C4B2><EFBFBD>
int judgee = 1;
for (int i = 0; i < 9; i++)
{
for (int j = 0; j < 9; j++)
{
if (a[i][j] == 0)
{
printf("No solution<6F><6E>\n");
judgee = 0;
break;
}
}
if (judgee == 0){break;}
}
}
return;
}