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.

115 lines
3.4 KiB

#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("|");
}
}
}
}
// <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ƿ<EFBFBD><C7B7><EFBFBD><EFBFBD>ظ<EFBFBD><D8B8><EFBFBD><EFBFBD><EFBFBD>
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) { // ֻ<><D6BB><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ԫ<EFBFBD><D4AA>
return false;
}
}
return true;
}
// <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ƿ<EFBFBD><C7B7><EFBFBD><EFBFBD>ظ<EFBFBD><D8B8><EFBFBD><EFBFBD><EFBFBD>
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) { // ֻ<><D6BB><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ԫ<EFBFBD><D4AA>
return false;
}
}
return true;
}
// <20><><EFBFBD><EFBFBD>3x3<78>Ӿ<EFBFBD><D3BE><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ƿ<EFBFBD><C7B7><EFBFBD><EFBFBD>ظ<EFBFBD><D8B8><EFBFBD><EFBFBD><EFBFBD>
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) { // ֻ<><D6BB><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ԫ<EFBFBD><D4AA>
return false;
}
}
}
return true;
}
// <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ʼ<EFBFBD><CABC><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ƿ<EFBFBD><C7B7><EFBFBD>Ч
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) { // ֻ<><D6BB><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ԫ<EFBFBD><D4AA>
int num = board[row][col];
if (!checkRow(board, row, num) || !checkColumn(board, col, num) || !checkBox(board, row - row % 3, col - col % 3, num)) {
// <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ԭ<EFBFBD><D4AD>
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); // <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ֻ<EFBFBD><D6BB>ʾ<EFBFBD><CABE><EFBFBD>ĸ<EFBFBD>3x3<78><EFBFBD><E9A3AC><EFBFBD><EFBFBD>ȷ<EFBFBD><C8B7><EFBFBD><EFBFBD>
}
return false;
}
}
}
}
printf("True:Valid initial Sudoku matrix!\n");
return true;
}
int main() {
printf("<EFBFBD><EFBFBD>The original Sudoku matrix:\n");
int board[SIZE][SIZE] = {
// <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>IJ<EFBFBD>ȱ<EFBFBD><C8B1><EFBFBD><EFBFBD><EFBFBD><EFBFBD>0<EFBFBD><30>ʾ<EFBFBD><CABE>ȱ<EFBFBD><C8B1><EFBFBD>֣<EFBFBD><D6A3><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ֱ<EFBFBD>ʾ<EFBFBD>Ѹ<EFBFBD><D1B8><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
{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;
}