|
|
|
|
#include <stdio.h>
|
|
|
|
|
#include <stdbool.h>
|
|
|
|
|
#define SIZE 9
|
|
|
|
|
bool isValidSudoku(int matrix[SIZE][SIZE]);
|
|
|
|
|
bool checkRow(int matrix[SIZE][SIZE], int row, int num);
|
|
|
|
|
bool checkCol(int matrix[SIZE][SIZE], int col, int num);
|
|
|
|
|
bool checkBlock(int matrix[SIZE][SIZE], int startRow, int startCol, int num);
|
|
|
|
|
char errorMessage[100];
|
|
|
|
|
int main() {
|
|
|
|
|
int sudoku[SIZE][SIZE] = {
|
|
|
|
|
{1, 1, 6, 9, 8, 5, 0, 0, 0},
|
|
|
|
|
{0, 0, 0, 1, 0, 2, 0, 0, 0},
|
|
|
|
|
{2, 3, 0, 0, 5, 7, 6, 0, 4},
|
|
|
|
|
{8, 0, 0, 0, 0, 0, 0, 0, 9},
|
|
|
|
|
{0, 0, 0, 0, 0, 0, 0, 0, 0},
|
|
|
|
|
{0, 0, 0, 0, 0, 0, 0, 0, 0},
|
|
|
|
|
{0, 0, 0, 0, 0, 0, 0, 0, 0},
|
|
|
|
|
{0, 0, 0, 0, 0, 0, 0, 0, 0},
|
|
|
|
|
{0, 0, 0, 0, 0, 0, 0, 0, 0}
|
|
|
|
|
};
|
|
|
|
|
printf("The original Sudoku matrix:\n");
|
|
|
|
|
int i,j;
|
|
|
|
|
for (i = 0; i < SIZE; i++) {
|
|
|
|
|
for (j = 0; j < SIZE; j++) {
|
|
|
|
|
printf("%d ", sudoku[i][j]);
|
|
|
|
|
}
|
|
|
|
|
printf("\n");
|
|
|
|
|
}
|
|
|
|
|
if (!isValidSudoku(sudoku)) {
|
|
|
|
|
printf("False: Invalid initial Sudoku matrix!\n");
|
|
|
|
|
if (errorMessage[0] != '\0') { // <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ϣ<EFBFBD>Ƿ<EFBFBD>Ϊ<EFBFBD><CEAA>
|
|
|
|
|
printf("%s\n", errorMessage);
|
|
|
|
|
}
|
|
|
|
|
} else {
|
|
|
|
|
printf("True: Valid initial Sudoku matrix!\n");
|
|
|
|
|
}
|
|
|
|
|
return 0;
|
|
|
|
|
}bool isValidSudoku(int matrix[SIZE][SIZE]) {
|
|
|
|
|
int row,num;
|
|
|
|
|
for (row = 0; row < SIZE; row++) {
|
|
|
|
|
for (num = 1; num <= 9; num++) {
|
|
|
|
|
if (!checkRow(matrix, row, num)) {
|
|
|
|
|
sprintf(errorMessage, "The number %d in the row %d has been used!", num, row + 1);
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
int col;
|
|
|
|
|
for (col = 0; col < SIZE; col++) {
|
|
|
|
|
for (num = 1; num <= 9; num++) {
|
|
|
|
|
if (!checkCol(matrix, col, num)) {
|
|
|
|
|
sprintf(errorMessage, "The number %d in the col %d has been used!", num, col + 1);
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
int startRow,startCol;
|
|
|
|
|
for (startRow = 0; startRow < SIZE; startRow += 3) {
|
|
|
|
|
for (startCol = 0; startCol < SIZE; startCol += 3) {
|
|
|
|
|
for (num = 1; num <= 9; num++) {
|
|
|
|
|
if (!checkBlock(matrix, startRow, startCol, num)) {
|
|
|
|
|
sprintf(errorMessage, "The number %d in the block %d has been used!", num, (startRow / 3) * 3 + startCol / 3 + 1);
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
bool checkRow(int matrix[SIZE][SIZE], int row, int num) {
|
|
|
|
|
int count = 0,col;
|
|
|
|
|
for (col = 0; col < SIZE; col++) {
|
|
|
|
|
if (matrix[row][col] == num) count++;
|
|
|
|
|
if (count > 1) {
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
bool checkCol(int matrix[SIZE][SIZE], int col, int num) {
|
|
|
|
|
int count = 0,row;
|
|
|
|
|
for (row = 0; row < SIZE; row++) {
|
|
|
|
|
if (matrix[row][col] == num) count++;
|
|
|
|
|
if (count > 1) {
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
bool checkBlock(int matrix[SIZE][SIZE], int startRow, int startCol, int num) {
|
|
|
|
|
int count = 0,row,col;
|
|
|
|
|
for (row = 0; row < 3; row++) {
|
|
|
|
|
for (col = 0; col < 3; col++) {
|
|
|
|
|
if (matrix[startRow + row][startCol + col] == num) count++;
|
|
|
|
|
if (count > 1) {
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return true;
|
|
|
|
|
}
|