parent
eaf97b755c
commit
7ad583ac20
@ -0,0 +1,160 @@
|
||||
#include <stdio.h>
|
||||
#include <stdbool.h>
|
||||
bool checkRow(const char board[9][9], int row, char num) {
|
||||
for (int col = 0; col < 9; col++) {
|
||||
if (board[row][col] == num) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
return true;
|
||||
}
|
||||
bool checkCol(const char board[9][9], int col, char num) {
|
||||
for (int row = 0; row < 9; row++) {
|
||||
if (board[row][col] == num) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
return true;
|
||||
}
|
||||
bool checkBox(const char board[9][9], int startRow, int startCol, char num) {
|
||||
for (int row = 0; row < 3; row++) {
|
||||
for (int col = 0; col < 3; col++) {
|
||||
if (board[row + startRow][col + startCol] == num) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
}
|
||||
return true;
|
||||
}
|
||||
bool isValidSudoku(const char board[9][9]) {
|
||||
for (int row = 0; row < 9; row++) {
|
||||
for (int col = 0; col < 9; col++) {
|
||||
if (board[row][col]!= '.') {
|
||||
char num = board[row][col];
|
||||
if (!checkRow(board, row, num) ||
|
||||
!checkCol(board, col, num) ||
|
||||
!checkBox(board, row - row % 3, col - col % 3, num)) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
return true;
|
||||
}
|
||||
bool findEmptyCell(const char board[9][9], int *row, int *col) {
|
||||
for (*row = 0; *row < 9; (*row)++) {
|
||||
for (*col = 0; *col < 9; (*col)++) {
|
||||
if (board[*row][*col] == '.') {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
}
|
||||
return false;
|
||||
}
|
||||
bool solveSudoku(char board[9][9]) {
|
||||
int row, col;
|
||||
if (!findEmptyCell(board, &row, &col)) {
|
||||
return true;
|
||||
}
|
||||
|
||||
for (char num = '1'; num <= '9'; num++) {
|
||||
if (checkRow(board, row, num) &&
|
||||
checkCol(board, col, num) &&
|
||||
checkBox(board, row - row % 3, col - col % 3, num)) {
|
||||
board[row][col] = num;
|
||||
if (solveSudoku(board)) {
|
||||
return true;
|
||||
}
|
||||
board[row][col] = '.';
|
||||
}
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
void printSudokuMatrix(const char board[9][9]) {
|
||||
for (int i = 0; i < 9; i++) {
|
||||
for (int j = 0; j < 9; j++) {
|
||||
printf("%c ", board[i][j]);
|
||||
}
|
||||
printf("\n");
|
||||
}
|
||||
}
|
||||
int main() {
|
||||
char board0[9][9] = {
|
||||
{'5', '3', '.', '.', '7', '.', '.', '.', '.'},
|
||||
{'6', '.', '.', '1', '9', '5', '.', '.', '.'},
|
||||
{'.', '9', '8', '.', '.', '.', '.', '6', '.'},
|
||||
{'8', '.', '.', '.', '6', '.', '.', '.', '3'},
|
||||
{'4', '.', '.', '8', '.', '3', '.', '.', '1'},
|
||||
{'7', '.', '.', '.', '2', '.', '.', '.', '6'},
|
||||
{'.', '6', '.', '.', '.', '.', '2', '8', '.'},
|
||||
{'.', '.', '.', '4', '1', '9', '.', '.', '5'},
|
||||
{'.', '.', '.', '.', '8', '.', '.', '7', '9'}
|
||||
};
|
||||
char board1[9][9] = {
|
||||
{'8', '3', '.', '.', '7', '.', '.', '.', '.'},
|
||||
{'6', '.', '.', '1', '9', '5', '.', '.', '.'},
|
||||
{'.', '9', '8', '.', '.', '.', '.', '6', '.'},
|
||||
{'8', '.', '.', '.', '6', '.', '.', '.', '3'},
|
||||
{'4', '.', '.', '8', '.', '3', '.', '.', '1'},
|
||||
{'7', '.', '.', '.', '2', '.', '.', '.', '6'},
|
||||
{'.', '6', '.', '.', '.', '.', '2', '8', '.'},
|
||||
{'.', '.', '.', '4', '1', '9', '.', '.', '5'},
|
||||
{'.', '.', '.', '.', '8', '.', '.', '7', '9'}
|
||||
};
|
||||
char board2[9][9] = {
|
||||
{'5', '2', '.', '.', '7', '.', '.', '.', '.'},
|
||||
{'6', '.', '.', '1', '9', '5', '.', '.', '.'},
|
||||
{'.', '9', '8', '.', '.', '.', '.', '6', '.'},
|
||||
{'8', '.', '.', '.', '6', '.', '.', '.', '3'},
|
||||
{'4', '.', '.', '8', '.', '3', '.', '.', '1'},
|
||||
{'7', '.', '.', '.', '2', '.', '.', '.', '6'},
|
||||
{'.', '6', '.', '.', '.', '.', '2', '8', '.'},
|
||||
{'.', '.', '.', '4', '1', '9', '.', '.', '5'},
|
||||
{'.', '.', '.', '.', '8', '.', '.', '7', '9'}
|
||||
};
|
||||
printf("原始数独矩阵:\n");
|
||||
printSudokuMatrix(board0);
|
||||
|
||||
if (isValidSudoku(board0)) {
|
||||
printf("是数独矩阵\n");
|
||||
if (solveSudoku(board0)) {
|
||||
printf("数独矩阵的解是: \n");
|
||||
printSudokuMatrix(board0);
|
||||
} else {
|
||||
printf("无解\n");
|
||||
}
|
||||
} else {
|
||||
printf("不是数独矩阵\n");
|
||||
printf("无解\n");
|
||||
}
|
||||
printf("原始数独矩阵:\n");
|
||||
printSudokuMatrix(board1);
|
||||
if (isValidSudoku(board1)) {
|
||||
printf("是数独矩阵\n");
|
||||
if (solveSudoku(board1)) {
|
||||
printf("数独矩阵的解是: \n");
|
||||
printSudokuMatrix(board1);
|
||||
} else {
|
||||
printf("无解\n");
|
||||
}
|
||||
} else {
|
||||
printf("不是数独矩阵\n");
|
||||
printf("无解\n");
|
||||
}
|
||||
printf("原始数独矩阵: \n");
|
||||
printSudokuMatrix(board2);
|
||||
if (isValidSudoku(board2)) {
|
||||
printf("是数独矩阵\n");
|
||||
if (solveSudoku(board2)) {
|
||||
printf("数独矩阵的解是: \n");
|
||||
printSudokuMatrix(board2);
|
||||
} else {
|
||||
printf("无解\n");
|
||||
}
|
||||
} else {
|
||||
printf("不是数独矩阵\n");
|
||||
printf("无解\n");
|
||||
}
|
||||
return 0;
|
||||
}
|
Loading…
Reference in new issue