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.

122 lines
3.8 KiB

#include <stdio.h>
#include <stdbool.h>
#define SIZE 9
// <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
bool isSafe(int grid[SIZE][SIZE], int row, int col, int num);
bool solveSudoku(int grid[SIZE][SIZE]);
void findInvalidReason(int grid[SIZE][SIZE]);
void print(int matrix[SIZE][SIZE]);
// <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
int main() {
int sudoku[SIZE][SIZE] = { {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}};
printf("The original Sudoku matrix: \n");
print(sudoku);
if (solveSudoku(sudoku)) {
printf("True:Valid initial Sudoku matrix!\n");
printf("The solution of Sudoku matrix:\n");
print(sudoku);
} else {
printf("False:Invalid initial Sudoku matrix!\n");
findInvalidReason(sudoku);
printf("No solution!\n");
}
return 0;
}
// <20><><EFBFBD><EFBFBD><EFBFBD>ڸ<EFBFBD><DAB8><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>У<EFBFBD><D0A3><EFBFBD><EFBFBD><EFBFBD> num <20>ܷ<EFBFBD><DCB7><EFBFBD><EFBFBD><EFBFBD>λ<EFBFBD><CEBB> (row, col)
bool isSafe(int grid[SIZE][SIZE], int row, int col, int num) {
int x, i, j;
// <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
for (x = 0; x < SIZE; x++)
if (grid[row][x] == num)
return false;
// <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
for (x = 0; x < SIZE; x++)
if (grid[x][col] == num)
return false;
// <20><><EFBFBD><EFBFBD> 3x3 <20><><EFBFBD><EFBFBD>
int startRow = row - row % 3, startCol = col - col % 3;
for (i = 0; i < 3; i++)
for (j = 0; j < 3; j++)
if (grid[i + startRow][j + startCol] == num)
return false;
return true;
}
// <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ĵݹ麯<DDB9><E9BAAF>
bool solveSudoku(int grid[SIZE][SIZE]) {
int row, col, num;
// <20><><EFBFBD>ҿհ׸<D5B0>
for (row = 0; row < SIZE; row++)
for (col = 0; col < SIZE; col++)
if (grid[row][col] == 0) {
for (num = 1; num <= 9; num++) {
if (isSafe(grid, row, col, num)) {
grid[row][col] = num;
if (solveSudoku(grid))
return true;
grid[row][col] = 0; // <20><><EFBFBD><EFBFBD>
}
}
return false; // <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
}
return true; // <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
}
// <20><><EFBFBD>Ҳ<EFBFBD><D2B2><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ԭ<EFBFBD><D4AD>
void findInvalidReason(int grid[SIZE][SIZE]) {
int row, col, num, blockRow, blockCol;
for (row = 0; row < SIZE; row++) {
for (col = 0; col < SIZE; col++) {
if (grid[row][col] != 0) {
for (num = 1; num <= 9; num++) {
if (grid[row][col] == num && !isSafe(grid, row, col, num)) {
blockRow = row - row % 3;
blockCol = col - col % 3;
printf("The number %d is duplicated in row %d, column %d, or block %d.\n", num, row + 1, col + 1, (blockRow / 3) * 3 + (blockCol / 3) + 1);
return;
}
}
}
}
}
}
// <20><>ʽ<EFBFBD><CABD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
void print(int matrix[SIZE][SIZE]) {
int i, j;
printf("|-----------------------------|\n");
for (i = 0; i < SIZE; i++) {
printf("|");
for (j = 0; j < SIZE; j++) {
printf("%2d ", matrix[i][j]);
if ((j + 1) % 3 == 0 ) printf("|");
}
printf("\n");
if ((i + 1) % 3 == 0 && i < SIZE-1) {
printf("|-----------------------------|\n");
}
}
printf("|-----------------------------|\n");
}