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.

142 lines
4.5 KiB

#include <stdio.h>
#include <stdbool.h>
#define SIZE 9
void print_sudoku(int matrix[SIZE][SIZE]) {
for (int i = 0; i < SIZE; i++) {
for (int j = 0; j < SIZE; j++) {
printf("%d ", matrix[i][j]);
if ((j + 1) % 3 == 0 && j != 8) {
printf("| ");
}
}
printf("\n");
if ((i + 1) % 3 == 0 && i != 8) {
printf("------+-------+------\n");
}
}
}
bool is_valid_sudoku(int matrix[SIZE][SIZE]) {
// <20><><EFBFBD><EFBFBD><EFBFBD>С<EFBFBD><D0A1>к<EFBFBD> 3x3 С<><D0A1>
for (int i = 0; i < SIZE; i++) {
bool row_check[SIZE + 1] = { false }; // <20><><EFBFBD>ڼ<EFBFBD><DABC><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD> 1-9 <20>ij<EFBFBD><C4B3><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
bool col_check[SIZE + 1] = { false }; // <20><><EFBFBD>ڼ<EFBFBD><DABC><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD> 1-9 <20>ij<EFBFBD><C4B3><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
for (int j = 0; j < SIZE; j++) {
// <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
if (matrix[i][j] != 0) {
if (row_check[matrix[i][j]]) {
printf("False:Invalid initial Sudoku matrix!\n");
printf("The number %d in the row %d has been used!\n", matrix[i][j], i + 1);
return false;
}
row_check[matrix[i][j]] = true;
}
// <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
if (matrix[j][i] != 0) {
if (col_check[matrix[j][i]]) {
printf("False:Invalid initial Sudoku matrix!\n");
printf("The number %d in the col %d has been used!\n", matrix[j][i], i + 1);
return false;
}
col_check[matrix[j][i]] = true;
}
}
}
// <20><><EFBFBD><EFBFBD> 3x3 С<><D0A1>
for (int start_row = 0; start_row < SIZE; start_row += 3) {
for (int start_col = 0; start_col < SIZE; start_col += 3) {
bool block_check[SIZE + 1] = { false }; // <20><><EFBFBD>ڼ<EFBFBD><DABC><EFBFBD> 3x3 С<><D0A1>
for (int i = start_row; i < start_row + 3; i++) {
for (int j = start_col; j < start_col + 3; j++) {
if (matrix[i][j] != 0) {
if (block_check[matrix[i][j]]) {
printf("False:Invalid initial Sudoku matrix!\n");
printf("The number %d in the block %d has been used!\n", matrix[i][j], (start_row / 3) * 3 + (start_col / 3) + 1);
return false;
}
block_check[matrix[i][j]] = true;
}
}
}
}
}
return true;
}
bool is_safe(int matrix[SIZE][SIZE], int row, int col, int num) {
for (int x = 0; x < SIZE; x++) {
if (matrix[row][x] == num || matrix[x][col] == num) {
return false;
}
}
int startRow = row - row % 3, startCol = col - col % 3;
for (int i = 0; i < 3; i++) {
for (int j = 0; j < 3; j++) {
if (matrix[i + startRow][j + startCol] == num) {
return false;
}
}
}
return true;
}
bool solve_sudoku(int matrix[SIZE][SIZE]) {
for (int row = 0; row < SIZE; row++) {
for (int col = 0; col < SIZE; col++) {
if (matrix[row][col] == 0) {
for (int num = 1; num <= 9; num++) {
if (is_safe(matrix, row, col, num)) {
matrix[row][col] = num;
if (solve_sudoku(matrix)) {
return true;
}
matrix[row][col] = 0; // <20><><EFBFBD><EFBFBD>ѡ<EFBFBD><D1A1>
}
}
return false; // <20><><EFBFBD><EFBFBD>û<EFBFBD><C3BB><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ܹ<EFBFBD><DCB9><EFBFBD><EFBFBD><EFBFBD><EBA3AC><EFBFBD><EFBFBD> false
}
}
}
return true; // <20>ɹ<EFBFBD><C9B9><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
}
int main() {
int sudoku_matrix[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(sudoku_matrix);
if (is_valid_sudoku(sudoku_matrix)) {
printf("True:Valid initial Sudoku matrix!\n");
if (solve_sudoku(sudoku_matrix)) {
printf("The solution of Sudoku matrix:\n");
print_sudoku(sudoku_matrix);
}
else {
printf("No solution!\n");
}
}
else {
printf("No solution!\n");
}
return 0;
}