|
|
#include <stdio.h>
|
|
|
#include <stdbool.h>
|
|
|
|
|
|
#define SIZE 9
|
|
|
void geshihua(int arr0[9][9])
|
|
|
{
|
|
|
int i,j;
|
|
|
printf("|");
|
|
|
for(i=0;i<9;i++)
|
|
|
{
|
|
|
for(j=0;j<9;j++)
|
|
|
{
|
|
|
printf("%d ",arr0[i][j]);
|
|
|
|
|
|
if(j==8)
|
|
|
{
|
|
|
printf("|\n");
|
|
|
}
|
|
|
if(i==8&&j==8)
|
|
|
break;
|
|
|
if((i+1)%3==0&&j==8&&i!=8)
|
|
|
{
|
|
|
printf("|--------------------|\n");
|
|
|
}
|
|
|
if((j+1)%3==0)
|
|
|
{
|
|
|
printf("|");
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
}
|
|
|
}
|
|
|
|
|
|
// 检查行中是否有重复数字
|
|
|
bool checkRow(int board[SIZE][SIZE], int row, int num) {
|
|
|
int col;
|
|
|
for (col = 0; col < SIZE; col++) {
|
|
|
if (board[row][col] == num && board[row][col] != 0) { // 只检查非零元素
|
|
|
return false;
|
|
|
}
|
|
|
}
|
|
|
return true;
|
|
|
}
|
|
|
|
|
|
// 检查列中是否有重复数字
|
|
|
bool checkColumn(int board[SIZE][SIZE], int col, int num) {
|
|
|
int row;
|
|
|
for (row = 0; row < SIZE; row++) {
|
|
|
if (board[row][col] == num && board[row][col] != 0) { // 只检查非零元素
|
|
|
return false;
|
|
|
}
|
|
|
}
|
|
|
return true;
|
|
|
}
|
|
|
|
|
|
// 检查3x3子矩阵中是否有重复数字
|
|
|
bool checkBox(int board[SIZE][SIZE], int rowStart, int colStart, int num) {
|
|
|
int row,col;
|
|
|
for (row = 0; row < 3; row++) {
|
|
|
for (col = 0; col < 3; col++) {
|
|
|
if (board[row + rowStart][col + colStart] == num && board[row + rowStart][col + colStart] != 0) { // 只检查非零元素
|
|
|
return false;
|
|
|
}
|
|
|
}
|
|
|
}
|
|
|
return true;
|
|
|
}
|
|
|
|
|
|
// 检查初始数独矩阵是否有效
|
|
|
bool isValidInitialSudoku(int board[SIZE][SIZE]) {
|
|
|
int row,col;
|
|
|
for (row = 0; row < SIZE; row++) {
|
|
|
for (col = 0; col < SIZE; col++) {
|
|
|
if (board[row][col] != 0) { // 只检查非零元素
|
|
|
int num = board[row][col];
|
|
|
if (!checkRow(board, row, num) || !checkColumn(board, col, num) || !checkBox(board, row - row % 3, col - col % 3, num)) {
|
|
|
// 输出不满足的原因
|
|
|
if (!checkRow(board, row, num)) {
|
|
|
printf("False:Invalid initial Sudoku matrix! The number %d in the row %d has been used!\n", num, row + 1);
|
|
|
} else if (!checkColumn(board, col, num)) {
|
|
|
printf("False:Invalid initial Sudoku matrix! The number %d in the column %d has been used!\n", num, col + 1);
|
|
|
} else {
|
|
|
printf("False:Invalid initial Sudoku matrix! The number %d in the block %d has been used!\n", num, (row / 3) * 3 + 1); // 简化输出,只表示是哪个3x3块,不精确到列
|
|
|
}
|
|
|
return false;
|
|
|
}
|
|
|
}
|
|
|
}
|
|
|
}
|
|
|
printf("True:Valid initial Sudoku matrix!\n");
|
|
|
return true;
|
|
|
}
|
|
|
|
|
|
int main() {
|
|
|
printf("“The original Sudoku matrix:\n");
|
|
|
int board[SIZE][SIZE] = {
|
|
|
// 这里填入你的残缺矩阵,0表示空缺部分,非零数字表示已给出的数字
|
|
|
{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}
|
|
|
};
|
|
|
geshihua(board);
|
|
|
|
|
|
isValidInitialSudoku(board);
|
|
|
|
|
|
return 0;
|
|
|
}
|