|
|
|
|
#include <stdio.h>
|
|
|
|
|
#include <stdbool.h>
|
|
|
|
|
|
|
|
|
|
#define SIZE 9
|
|
|
|
|
|
|
|
|
|
// <20><>ӡ<EFBFBD><D3A1><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
|
|
|
|
void printSudoku(int board[SIZE][SIZE]) {
|
|
|
|
|
int row,col;
|
|
|
|
|
for (row = 0; row < SIZE; row++) {
|
|
|
|
|
for (col = 0; col < SIZE; col++) {
|
|
|
|
|
printf("%d ", board[row][col]);
|
|
|
|
|
}
|
|
|
|
|
printf("\n");
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// <20><><EFBFBD>鵱ǰλ<C7B0><CEBB><EFBFBD>Ƿ<EFBFBD><C7B7><EFBFBD>ȫ
|
|
|
|
|
bool isSafe(int board[SIZE][SIZE], int row, int col, int num) {
|
|
|
|
|
// <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
|
|
|
|
int x;
|
|
|
|
|
for (x = 0; x < SIZE; x++) {
|
|
|
|
|
if (board[row][x] == num) {
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
|
|
|
|
for (x = 0; x < SIZE; x++) {
|
|
|
|
|
if (board[x][col] == num) {
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// <20><><EFBFBD><EFBFBD> 3x3 <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
|
|
|
|
int startRow = row - row % 3;
|
|
|
|
|
int startCol = col - col % 3;
|
|
|
|
|
int i,j;
|
|
|
|
|
for (i = 0; i < 3; i++) {
|
|
|
|
|
for (j = 0; j < 3; j++) {
|
|
|
|
|
if (board[i + startRow][j + startCol] == num) {
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ļ<EFBFBD><C4BB><EFBFBD><EFBFBD>㷨
|
|
|
|
|
bool solveSudoku(int board[SIZE][SIZE]) {
|
|
|
|
|
// <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
|
|
|
|
int row,col;
|
|
|
|
|
for (row = 0; row < SIZE; row++) {
|
|
|
|
|
for (col = 0; col < SIZE; col++) {
|
|
|
|
|
// <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ǰλ<C7B0><CEBB><EFBFBD>ǿյ<C7BF>
|
|
|
|
|
if (board[row][col] == 0) {
|
|
|
|
|
// <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD> 1 <20><> 9
|
|
|
|
|
int num;
|
|
|
|
|
for (num = 1; num <= 9; num++) {
|
|
|
|
|
if (isSafe(board, row, col, num)) {
|
|
|
|
|
// <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
|
|
|
|
board[row][col] = num;
|
|
|
|
|
|
|
|
|
|
// <20>ݹ<EFBFBD><DDB9><EFBFBD><EFBFBD>ý<EFBFBD><C3BD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
|
|
|
|
if (solveSudoku(board)) {
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// <20><><EFBFBD><EFBFBD>ʧ<EFBFBD>ܣ<EFBFBD><DCA3><EFBFBD><EFBFBD><EFBFBD>
|
|
|
|
|
board[row][col] = 0;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// <20><><EFBFBD><EFBFBD> 1 <20><> 9 <20><><EFBFBD><EFBFBD><EFBFBD>ܷ<EFBFBD><DCB7>ã<EFBFBD><C3A3><EFBFBD><EFBFBD><EFBFBD> false
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>λ<EFBFBD>ö<EFBFBD><C3B6><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ϣ<EFBFBD><CFA3><EFBFBD><EFBFBD><EFBFBD> true
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ч<EFBFBD><D0A7>
|
|
|
|
|
bool isValidSudoku(int board[SIZE][SIZE]) {
|
|
|
|
|
// <20><><EFBFBD><EFBFBD><EFBFBD>к<EFBFBD><D0BA><EFBFBD><EFBFBD>Ƿ<EFBFBD><C7B7><EFBFBD><EFBFBD>ظ<EFBFBD><D8B8><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
|
|
|
|
int row,col;
|
|
|
|
|
for (row = 0; row < SIZE; row++) {
|
|
|
|
|
int rowSet[10] = {0};
|
|
|
|
|
int colSet[10] = {0};
|
|
|
|
|
for (col = 0; col < SIZE; col++) {
|
|
|
|
|
int num = board[row][col];
|
|
|
|
|
if (num != 0) {
|
|
|
|
|
if (rowSet[num]) {
|
|
|
|
|
printf("Row %d has duplicate number %d\n", row, num);
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
rowSet[num] = 1;
|
|
|
|
|
|
|
|
|
|
if (colSet[num]) {
|
|
|
|
|
printf("Column %d has duplicate number %d\n", col, num);
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
colSet[num] = 1;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// <20><><EFBFBD><EFBFBD> 3x3 <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ƿ<EFBFBD><C7B7><EFBFBD><EFBFBD>ظ<EFBFBD><D8B8><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
|
|
|
|
int block;
|
|
|
|
|
for (block = 0; block < 9; block++) {
|
|
|
|
|
int blockSet[10] = {0};
|
|
|
|
|
int startRow = block / 3 * 3;
|
|
|
|
|
int startCol = block % 3 * 3;
|
|
|
|
|
int i,j;
|
|
|
|
|
for (i = 0; i < 3; i++) {
|
|
|
|
|
for (j = 0; j < 3; j++) {
|
|
|
|
|
int num = board[startRow + i][startCol + j];
|
|
|
|
|
if (num != 0) {
|
|
|
|
|
if (blockSet[num]) {
|
|
|
|
|
printf("3x3 block starting at (%d, %d) has duplicate number %d\n", startRow, startCol, num);
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
blockSet[num] = 1;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
int main() {
|
|
|
|
|
int board[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");
|
|
|
|
|
printSudoku(board);
|
|
|
|
|
|
|
|
|
|
if (isValidSudoku(board)) {
|
|
|
|
|
printf("True:Valid initial Sudoku matrix!\n");
|
|
|
|
|
if (solveSudoku(board)) {
|
|
|
|
|
printf("The solution of Sudoku matrix:\n");
|
|
|
|
|
printSudoku(board);
|
|
|
|
|
} else {
|
|
|
|
|
printf("No solution!\n");
|
|
|
|
|
}
|
|
|
|
|
} else {
|
|
|
|
|
printf("False:Invalid initial Sudoku matrix!\n");
|
|
|
|
|
printf("No solution!\n");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
|
}
|