|
|
|
|
#include <stdio.h>
|
|
|
|
|
|
|
|
|
|
// <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ĵ<EFBFBD>С
|
|
|
|
|
#define N 9
|
|
|
|
|
|
|
|
|
|
// <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ƿ<EFBFBD><C7B7><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
|
|
|
|
int isRowValid(int matrix[N][N], int row) {
|
|
|
|
|
int used[N + 1] = {0}; // <20><><EFBFBD>ڼ<EFBFBD>¼<EFBFBD><C2BC><EFBFBD>ֵ<EFBFBD>ʹ<EFBFBD><CAB9><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
|
|
|
|
|
|
|
|
|
for (int i = 0; i < N; i++) {
|
|
|
|
|
int num = matrix[row][i];
|
|
|
|
|
|
|
|
|
|
if (num != 0) {
|
|
|
|
|
if (used[num] == 1) {
|
|
|
|
|
printf("False: Invalid initial Sudoku matrix! The number %d in row %d has been used!\n", num, row + 1);
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
used[num] = 1;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return 1;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ƿ<EFBFBD><C7B7><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
|
|
|
|
int isColValid(int matrix[N][N], int col) {
|
|
|
|
|
int used[N + 1] = {0}; // <20><><EFBFBD>ڼ<EFBFBD>¼<EFBFBD><C2BC><EFBFBD>ֵ<EFBFBD>ʹ<EFBFBD><CAB9><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
|
|
|
|
|
|
|
|
|
for (int i = 0; i < N; i++) {
|
|
|
|
|
int num = matrix[i][col];
|
|
|
|
|
|
|
|
|
|
if (num != 0) {
|
|
|
|
|
if (used[num] == 1) {
|
|
|
|
|
printf("False: Invalid initial Sudoku matrix! The number %d in column %d has been used!\n", num, col + 1);
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
used[num] = 1;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return 1;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// <20><><EFBFBD><EFBFBD>һ<EFBFBD><D2BB>3x3<78><33>С<EFBFBD><D0A1><EFBFBD><EFBFBD><EFBFBD>Ƿ<EFBFBD><C7B7><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
|
|
|
|
int isBlockValid(int matrix[N][N], int startRow, int startCol) {
|
|
|
|
|
int used[N + 1] = {0}; // <20><><EFBFBD>ڼ<EFBFBD>¼<EFBFBD><C2BC><EFBFBD>ֵ<EFBFBD>ʹ<EFBFBD><CAB9><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
|
|
|
|
|
|
|
|
|
for (int i = startRow; i < startRow + 3; i++) {
|
|
|
|
|
for (int j = startCol; j < startCol + 3; j++) {
|
|
|
|
|
int num = matrix[i][j];
|
|
|
|
|
|
|
|
|
|
if (num != 0) {
|
|
|
|
|
if (used[num] == 1) {
|
|
|
|
|
printf("False: Invalid initial Sudoku matrix! The number %d in block %d has been used!\n", num, (startRow / 3) * 3 + startCol / 3 + 1);
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
used[num] = 1;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return 1;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
|
|
|
|
int main() {
|
|
|
|
|
int sudoku[N][N] = {{8, 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}
|
|
|
|
|
// <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
printf("The original Sudoku matrix:\n");
|
|
|
|
|
|
|
|
|
|
// <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
|
|
|
|
for (int i = 0; i < N; i++) {
|
|
|
|
|
for (int j = 0; j < N; j++) {
|
|
|
|
|
printf("%d ", sudoku[i][j]);
|
|
|
|
|
}
|
|
|
|
|
printf("\n");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
int isValid = 1;
|
|
|
|
|
|
|
|
|
|
// <20><><EFBFBD><EFBFBD>ÿһ<C3BF><D2BB>
|
|
|
|
|
for (int i = 0; i < N; i++) {
|
|
|
|
|
if (!isRowValid(sudoku, i)) {
|
|
|
|
|
isValid = 0;
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// <20><><EFBFBD><EFBFBD>ÿһ<C3BF><D2BB>
|
|
|
|
|
if (isValid) {
|
|
|
|
|
for (int i = 0; i < N; i++) {
|
|
|
|
|
if (!isColValid(sudoku, i)) {
|
|
|
|
|
isValid = 0;
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// <20><><EFBFBD><EFBFBD>ÿ<EFBFBD><C3BF>3x3С<33><D0A1><EFBFBD><EFBFBD>
|
|
|
|
|
if (isValid) {
|
|
|
|
|
for (int i = 0; i < N; i += 3) {
|
|
|
|
|
for (int j = 0; j < N; j += 3) {
|
|
|
|
|
if (!isBlockValid(sudoku, i, j)) {
|
|
|
|
|
isValid = 0;
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (isValid) {
|
|
|
|
|
printf("True: Valid initial Sudoku matrix!\n");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
|