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.
129 lines
4.1 KiB
129 lines
4.1 KiB
2 weeks ago
|
#include <stdio.h>
|
||
|
#include <stdbool.h>
|
||
|
|
||
|
#define SIZE 9
|
||
|
|
||
|
void printSudoku(int board[SIZE][SIZE]) {
|
||
|
for (int i = 0; i < SIZE; i++) {
|
||
|
for (int j = 0; j < SIZE; j++) {
|
||
|
if (j % 3 == 0 && j != 0) {
|
||
|
printf("| ");
|
||
|
}
|
||
|
printf("%d ", board[i][j] == 0 ? '.' : board[i][j]);
|
||
|
}
|
||
|
printf("\n");
|
||
|
if ((i + 1) % 3 == 0 && i != 8) {
|
||
|
printf("---------------------\n");
|
||
|
}
|
||
|
}
|
||
|
}
|
||
|
|
||
|
bool isValidPlacement(int board[SIZE][SIZE], int row_check[SIZE][SIZE], int col_check[SIZE][SIZE],
|
||
|
int block_check[SIZE][SIZE], int num, int row, int col) {
|
||
|
int blockIndex = (row / 3) * 3 + (col / 3);
|
||
|
if (row_check[row][num - 1] || col_check[col][num - 1] || block_check[blockIndex][num - 1]) {
|
||
|
return false; // 数字已在行、列或3x3块中存在
|
||
|
}
|
||
|
return true;
|
||
|
}
|
||
|
|
||
|
void setPlacement(int row_check[SIZE][SIZE], int col_check[SIZE][SIZE],
|
||
|
int block_check[SIZE][SIZE], int num, int row, int col, bool placed) {
|
||
|
int blockIndex = (row / 3) * 3 + (col / 3);
|
||
|
row_check[row][num - 1] = placed;
|
||
|
col_check[col][num - 1] = placed;
|
||
|
block_check[blockIndex][num - 1] = placed;
|
||
|
}
|
||
|
|
||
|
bool isValidSudoku(int board[SIZE][SIZE]) {
|
||
|
int row_check[SIZE][SIZE] = { 0 };
|
||
|
int col_check[SIZE][SIZE] = { 0 };
|
||
|
int block_check[SIZE][SIZE] = { 0 };
|
||
|
|
||
|
for (int i = 0; i < SIZE; i++) {
|
||
|
for (int j = 0; j < SIZE; j++) {
|
||
|
int num = board[i][j];
|
||
|
if (num != 0) {
|
||
|
if (!isValidPlacement(board, row_check, col_check, block_check, num, i, j)) {
|
||
|
printf("False: Invalid initial Sudoku matrix!\n");
|
||
|
printf("The number %d has conflicts!\n", num);
|
||
|
return false;
|
||
|
}
|
||
|
setPlacement(row_check, col_check, block_check, num, i, j, true);
|
||
|
}
|
||
|
}
|
||
|
}
|
||
|
return true;
|
||
|
}
|
||
|
|
||
|
bool solveSudoku(int board[SIZE][SIZE]) {
|
||
|
for (int i = 0; i < SIZE; i++) {
|
||
|
for (int j = 0; j < SIZE; j++) {
|
||
|
if (board[i][j] == 0) { // 找到一个空的位置
|
||
|
for (int num = 1; num <= 9; num++) {
|
||
|
board[i][j] = num; // 填入数字
|
||
|
|
||
|
if (isValidSudoku(board)) {
|
||
|
if (solveSudoku(board)) {
|
||
|
return true; // 若成功,则返回
|
||
|
}
|
||
|
}
|
||
|
board[i][j] = 0; // 撤销填入,重试下一个数字
|
||
|
}
|
||
|
return false; // 若所有数字尝试均不行,则返回不成功
|
||
|
}
|
||
|
}
|
||
|
}
|
||
|
return true; // 全部填满,返回成功
|
||
|
}
|
||
|
|
||
|
int main() {
|
||
|
int board0[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}
|
||
|
};
|
||
|
|
||
|
int board1[SIZE][SIZE] = {
|
||
|
{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}
|
||
|
};
|
||
|
|
||
|
for (int boardIndex = 0; boardIndex < 2; boardIndex++) {
|
||
|
int (*currentBoard)[SIZE] = boardIndex == 0 ? board0 : board1;
|
||
|
|
||
|
printf("The original Sudoku matrix:\n");
|
||
|
printSudoku(currentBoard);
|
||
|
if (isValidSudoku(currentBoard)) {
|
||
|
printf("True: Valid initial Sudoku matrix!\n");
|
||
|
if (solveSudoku(currentBoard)) {
|
||
|
printf("The solution of Sudoku matrix:\n");
|
||
|
printSudoku(currentBoard);
|
||
|
}
|
||
|
else {
|
||
|
printf("No solution!\n");
|
||
|
}
|
||
|
}
|
||
|
else {
|
||
|
printf("False: Invalid initial Sudoku matrix!\n");
|
||
|
printf("No solution!\n");
|
||
|
}
|
||
|
printf("\n");
|
||
|
}
|
||
|
|
||
|
return 0;
|
||
|
}
|