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.

213 lines
5.8 KiB

#include <stdio.h>
int checkRows(int matrix[9][9]) {
for (int i = 0; i < 9; i++) {
int count[10] = {0};
for (int j = 0; j < 9; j++) {
if (matrix[i][j] >= 1 && matrix[i][j] <= 9) {
if (count[matrix[i][j]] == 1) {
return 0;
}
count[matrix[i][j]]++;
}
}
}
return 1;
}
int checkColumns(int matrix[9][9]) {
for (int j = 0; j < 9; j++) {
int count[10] = {0};
for (int i = 0; i < 9; i++) {
if (matrix[i][j] >= 1 && matrix[i][j] <= 9) {
if (count[matrix[i][j]] == 1) {
return 0;
}
count[matrix[i][j]]++;
}
}
}
return 1;
}
int checkSubMatrices(int matrix[9][9]) {
for (int blockRow = 0; blockRow < 3; blockRow++) {
for (int blockCol = 0; blockCol < 3; blockCol++) {
int count[10] = {0};
for (int i = blockRow * 3; i < blockRow * 3 + 3; i++) {
for (int j = blockCol * 3; j < blockCol * 3 + 3; j++) {
if (matrix[i][j] >= 1 && matrix[i][j] <= 9) {
if (count[matrix[i][j]] == 1) {
return 0;
}
count[matrix[i][j]]++;
}
}
}
}
}
return 1;
}
void printMatrix(int matrix[9][9]) {
printf("|--------------------|\n|");
for (int i = 0; i < 9; i++) {
for (int j = 0; j < 9; j++) {
printf("%d ", matrix[i][j]);
if((i+1)%1==0&&(j+1)%3==0) printf("|");
if((i+1)%3==0&&(j+1)%9==0) printf("\n|--------------------|");
if((j+1)==9&&i!=8) printf("\n|");
}
}
}
int findUnassignedLocation(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 1;
}
}
}
return 0;
}
int isSafe(int matrix[9][9], int row, int col, int num) {
for (int i = 0; i < 9; i++) {
if (matrix[row][i] == num) {
return 0;
}
}
for (int i = 0; i < 9; i++) {
if (matrix[i][col] == num) {
return 0;
}
}
int startRow = row - row % 3;
int startCol = col - col % 3;
for (int i = startRow; i < startRow + 3; i++) {
for (int j = startCol; j < startCol + 3; j++) {
if (matrix[i][j] == num) {
return 0;
}
}
}
return 1;
}
int solveSudoku(int matrix[9][9]) {
int row, col;
if (!findUnassignedLocation(matrix, &row, &col)) {
return 1;
}
for (int num = 1; num <= 9; num++) {
if (isSafe(matrix, row, col, num)) {
matrix[row][col] = num;
if (solveSudoku(matrix)) {
return 1;
}
matrix[row][col] = 0;
}
}
return 0;
}
int main() {
int matrix[9][9] = {
{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");
printMatrix(matrix);
if (checkRows(matrix) && checkColumns(matrix) && checkSubMatrices(matrix)) {
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 i = 0; i < 9; i++) {
int count[10] = {0};
for (int j = 0; j < 9; j++) {
if (matrix[i][j] >= 1 && matrix[i][j] <= 9) {
if (count[matrix[i][j]] == 1) {
printf("The number %d in the row %d has been used!\n", matrix[i][j], i + 1);
break;
}
count[matrix[i][j]]++;
}
}
}
for (int j = 0; j < 9; j++) {
int count[10] = {0};
for (int i = 0; i < 9; i++) {
if (matrix[i][j] >= 1 && matrix[i][j] <= 9) {
if (count[matrix[i][j]] == 1) {
printf("The number %d in the col %d has been used!\n", matrix[i][j], j + 1);
break;
}
count[matrix[i][j]]++;
}
}
}
for (int blockRow = 0; blockRow < 3; blockRow++) {
for (int blockCol = 0; blockCol < 3; blockCol++) {
int count[10] = {0};
for (int i = blockRow * 3; i < blockRow * 3 + 3; i++) {
for (int j = blockCol * 3; j < blockCol * 3 + 3; j++) {
if (matrix[i][j] >= 1 && matrix[i][j] <= 9) {
if (count[matrix[i][j]] == 1) {
printf("The number %d in the block %d has been used!\n", matrix[i][j], blockRow * 3 + blockCol + 1);
break;
}
count[matrix[i][j]]++;
}
}
}
}
}
printf("No solution!\n");
}
return 0;
}