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
122 lines
3.8 KiB
#include <stdio.h>
|
|
#include <stdbool.h>
|
|
|
|
#define SIZE 9
|
|
|
|
// 函数声明
|
|
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]);
|
|
|
|
// 主函数
|
|
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;
|
|
}
|
|
|
|
|
|
|
|
// 检查在给定的数独矩阵中,数字 num 能否放在位置 (row, col)
|
|
bool isSafe(int grid[SIZE][SIZE], int row, int col, int num) {
|
|
int x, i, j;
|
|
// 检查行
|
|
for (x = 0; x < SIZE; x++)
|
|
if (grid[row][x] == num)
|
|
return false;
|
|
|
|
// 检查列
|
|
for (x = 0; x < SIZE; x++)
|
|
if (grid[x][col] == num)
|
|
return false;
|
|
|
|
// 检查 3x3 宫格
|
|
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;
|
|
}
|
|
|
|
// 解决数独问题的递归函数
|
|
bool solveSudoku(int grid[SIZE][SIZE]) {
|
|
int row, col, num;
|
|
// 查找空白格
|
|
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; // 回溯
|
|
}
|
|
}
|
|
return false; // 触发回溯
|
|
}
|
|
return true; // 解决了数独
|
|
}
|
|
|
|
// 查找不满足数独定义的原因
|
|
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;
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
// 格式化输出数独矩阵
|
|
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");
|
|
|
|
}
|