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.

285 lines
8.1 KiB

#include <stdio.h>
#include <stdbool.h>
#include <stdlib.h>
#include <time.h>
// <20><>ӡ<EFBFBD><D3A1><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
void printMatrix(int matrix[9][9]) {
for (int i = 0; i < 9; i++) {
if (i == 0 || i == 3 || i == 6) {
printf("|-----------------------|\n");
}
for (int j = 0; j < 9; j++) {
if (j == 0 || j == 3 || j == 6) {
printf("| ");
}
if (matrix[i][j] == 0) {
printf(". ");
}
else {
printf("%d ", matrix[i][j]);
}
if (j == 8) {
printf("|\n");
}
}
if (i == 8) {
printf("|-----------------------|\n");
}
}
}
// <20>ж<EFBFBD><D0B6><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ƿ<EFBFBD><C7B7><EFBFBD>ij<EFBFBD><C4B3><EFBFBD>г<EFBFBD><D0B3><EFBFBD>
int isInRow(int matrix[9][9], int row, int num) {
for (int i = 0; i < 9; i++) {
if (matrix[row][i] == num) {
return 1;
}
}
return 0;
}
// <20>ж<EFBFBD><D0B6><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ƿ<EFBFBD><C7B7><EFBFBD>ij 3x3 <20>Ӿ<EFBFBD><D3BE><EFBFBD><EFBFBD>г<EFBFBD><D0B3><EFBFBD>
int isInGroup(int matrix[9][9], int startRow, int startCol, int num) {
for (int i = 0; i < 3; i++) {
for (int j = 0; j < 3; j++) {
if (matrix[startRow + i][startCol + j] == num) {
return 1;
}
}
}
return 0;
}
// <20>ж<EFBFBD><D0B6><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ƿ<EFBFBD><C7B7><EFBFBD>ij<EFBFBD><C4B3><EFBFBD><EFBFBD><EFBFBD>ѱ<EFBFBD>ʹ<EFBFBD><CAB9>
bool usedInRow(int matrix[9][9], int row, int num) {
for (int col = 0; col < 9; col++) {
if (matrix[row][col] == num) {
return true;
}
}
return false;
}
// <20>ж<EFBFBD><D0B6><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ƿ<EFBFBD><C7B7><EFBFBD>ij<EFBFBD><C4B3><EFBFBD><EFBFBD><EFBFBD>ѱ<EFBFBD>ʹ<EFBFBD><CAB9>
bool usedInCol(int matrix[9][9], int col, int num) {
for (int row = 0; row < 9; row++) {
if (matrix[row][col] == num) {
return true;
}
}
return false;
}
// <20>ж<EFBFBD><D0B6><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ƿ<EFBFBD><C7B7><EFBFBD>ij 3x3 <20>Ӿ<EFBFBD><D3BE><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ѱ<EFBFBD>ʹ<EFBFBD><CAB9>
bool usedInBox(int matrix[9][9], int boxStartRow, int boxStartCol, int num) {
for (int row = 0; row < 3; row++) {
for (int col = 0; col < 3; col++) {
if (matrix[row + boxStartRow][col + boxStartCol] == num) {
return true;
}
}
}
return false;
}
// <20>ж<EFBFBD><D0B6><EFBFBD><EFBFBD>ض<EFBFBD>λ<EFBFBD>÷<EFBFBD><C3B7><EFBFBD><EFBFBD>ض<EFBFBD><D8B6><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ƿ<EFBFBD><C7B7><EFBFBD>ȫ
bool isSafe(int matrix[9][9], int row, int col, int num) {
return!usedInRow(matrix, row, num) && !usedInCol(matrix, col, num) && !usedInBox(matrix, row - row % 3, col - col % 3, num);
}
// <20><><EFBFBD>ҿ<EFBFBD>λ<EFBFBD><CEBB>
bool findEmptyLocation(int matrix[9][9], int* row, int* col) {
for (*row = 0; *row < 9; (*row)++) {
for (*col = 0; *col < 9; (*col)++) {
if (matrix[*row][*col] == 0) {
return true;
}
}
}
return false;
}
// <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
bool solveSudoku(int matrix[9][9]) {
int row, col;
if (!findEmptyLocation(matrix, &row, &col)) {
return true; // <20><><EFBFBD><EFBFBD><EFBFBD>ѽ<EFBFBD><D1BD><EFBFBD>
}
for (int num = 1; num <= 9; num++) {
if (isSafe(matrix, row, col, num)) {
matrix[row][col] = num;
if (solveSudoku(matrix)) {
return true;
}
matrix[row][col] = 0; // <20><><EFBFBD><EFBFBD>
}
}
return false;
}
// <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ƿ<EFBFBD><C7B7><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
bool checkRows(int matrix[9][9]) {
for (int i = 0; i < 9; i++) {
int count[9] = { 0 };
for (int j = 0; j < 9; j++) {
if (matrix[i][j] > 0 && matrix[i][j] <= 9) {
int num = matrix[i][j] - 1;
if (count[num] > 0) {
return false;
}
count[num]++;
}
}
}
return true;
}
// <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ƿ<EFBFBD><C7B7><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
bool checkColumns(int matrix[9][9]) {
for (int j = 0; j < 9; j++) {
int count[9] = { 0 };
for (int i = 0; i < 9; i++) {
if (matrix[i][j] > 0 && matrix[i][j] <= 9) {
int num = matrix[i][j] - 1;
if (count[num] > 0) {
return false;
}
count[num]++;
}
}
}
return true;
}
// <20><><EFBFBD><EFBFBD> 3x3 <20>Ӿ<EFBFBD><D3BE><EFBFBD><EFBFBD>Ƿ<EFBFBD><C7B7><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
bool checkSubMatrices(int matrix[9][9]) {
for (int row = 0; row < 9; row += 3) {
for (int col = 0; col < 9; col += 3) {
int count[9] = { 0 };
for (int i = row; i < row + 3; i++) {
for (int j = col; j < col + 3; j++) {
if (matrix[i][j] > 0 && matrix[i][j] <= 9) {
int num = matrix[i][j] - 1;
if (count[num] > 0) {
return false;
}
count[num]++;
}
}
}
}
}
return true;
}
// <20>ж<EFBFBD><D0B6><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ƿ<EFBFBD><C7B7><EFBFBD><EFBFBD><EFBFBD>Ч<EFBFBD><D0A7><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
bool isSudokuMatrix(int matrix[9][9]) {
return checkRows(matrix) && checkColumns(matrix) && checkSubMatrices(matrix);
}
int main() {
printf("The original Sudoku matrix: \n");
srand(time(NULL));
int matrix[9][9] = { 0 };
int numbers[9] = { 1, 2, 3, 4, 5, 6, 7, 8, 9 };
for (int group = 0; group < 3; group++) {
int usedNumbers[9] = { 0 };
for (int i = 0; i < 9; i++) {
usedNumbers[i] = numbers[i];
}
for (int i = 0; i < 9; i++) {
int idx = rand() % (9 - i);
int temp = usedNumbers[idx];
usedNumbers[idx] = usedNumbers[8 - i];
usedNumbers[8 - i] = temp;
}
for (int row = 0; row < 3; row++) {
int count = 0;
while (count < 3) {
int num;
do {
int idx = rand() % 9;
num = usedNumbers[idx];
} while (num == 0 || isInRow(matrix, group * 3 + row, num) || isInGroup(matrix, group * 3, 0, num));
int pos;
do {
pos = rand() % 9;
} while (matrix[group * 3 + row][pos] != 0);
matrix[group * 3 + row][pos] = num;
count++;
}
}
}
for (int row = 0; row < 9; row++) {
int uniqueNumbers[9] = { 0 };
int uniqueCount = 0;
for (int i = 0; i < 9; i++) {
if (matrix[row][i] != 0 && !uniqueNumbers[matrix[row][i] - 1]) {
uniqueNumbers[matrix[row][i] - 1] = 1;
uniqueCount++;
}
}
if (uniqueCount > 3) {
while (uniqueCount > 3) {
int idx = rand() % 9;
if (matrix[row][idx] != 0 && uniqueNumbers[matrix[row][idx] - 1]) {
matrix[row][idx] = 0;
uniqueCount--;
}
}
}
else if (uniqueCount < 3) {
while (uniqueCount < 3) {
int num;
do {
num = rand() % 9 + 1;
} while (uniqueNumbers[num - 1]);
int idx;
do {
idx = rand() % 9;
} while (matrix[row][idx] != 0);
matrix[row][idx] = num;
uniqueNumbers[num - 1] = 1;
uniqueCount++;
}
}
}
printMatrix(matrix);
bool valid = isSudokuMatrix(matrix);
if (valid) {
printf("True:Valid initial Sudoku matrix!\n");
if (solveSudoku(matrix)) {
printf("The solution of Sudoku matrix:\n");
printMatrix(matrix);
}
else {
printf("No solution!\n");
}
}
else {
printf("False:Invalid initial Sudoku matrix!\n");
for (int j = 0; j < 9; j++) {
int count[9] = { 0 };
for (int i = 0; i < 9; i++) {
if (matrix[i][j] > 0 && matrix[i][j] <= 9) {
int num = matrix[i][j] - 1;
if (count[num] > 0) {
printf("The number %d in the col %d has been used!\n", matrix[i][j], j);
break;
}
count[num]++;
}
}
}
printf("No solution!\n");
}
return 0;
}