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.

239 lines
7.0 KiB

#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
#include <time.h>
void boradprint(int** board) {
printf("|-----------------------|\n");
for (int row = 0; row < 9; row++) {
if (row == 3 || row == 6) {
printf("|-----------------------|\n");
}
printf("| ");
for (int col = 0; col < 9; col++) {
if (board[row][col] == 0) {
printf(". ");
}
else {
printf("%d ", board[row][col]);
}
if (col == 2 || col == 5) {
printf("| ");
}
}
printf("|\n");
}
printf("|-----------------------|\n");
}
void freeBoard(int** board, int rows) {
for (int i = 0; i < rows; i++) {
free(board[i]); // <20>ͷ<EFBFBD>ÿһ<C3BF>е<EFBFBD><D0B5>ڴ<EFBFBD>
}
free(board); // <20>ͷ<EFBFBD>ָ<EFBFBD><D6B8><EFBFBD>е<EFBFBD>ָ<EFBFBD><D6B8><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ڴ<EFBFBD>
}
bool isVaild(int** borad)
{
printf("The original Sudoku matrix: \n");
boradprint(borad);
int row_count = 0;
int col_count = 0;
int col_true[10] = { 0 };
int row_true[10] = { 0 };
while (row_count!=9)
{
for (int i = 0; i < 9; i++)//every raw
{
if (borad[row_count][i] > 0) //if it has define
{
if (!col_true[borad[row_count][i]])//check
{
col_true[borad[row_count][i]] = 1;
}
else
{
printf("False:Invalid initial Sudoku matrix!\n");
printf("The number %d in the col %d has been used!\n", borad[row_count][i], row_count+1 );
return false;
}
}
}
row_count++;
for (int i = 0; i <= 9; i++)col_true[i] = 0;
}
while (col_count!=9)
{
for (int i = 0; i < 9; i++)//every col
{
if (borad[i][col_count]) //if it has define
{
if (!row_true[borad[i][col_count]])//check
{
row_true[borad[i][col_count]] = 1;
}
else
{
printf("False:Invalid initial Sudoku matrix!\n");
printf("The number %d in the col %d has been used!\n", borad[i][col_count], col_count+1);
return false;
}
}
}
col_count++;
for (int i = 0; i < 10; i++)row_true[i] = 0;
}
row_count = col_count = 0;
int block_count = 1;
int block_group[10]={0};
int block_row = (row_count/3)*3;
int block_col = (col_count/3)*3;
while (block_count != 9)
{
for (int i = block_row; i < block_row + 3; i++)
for (int j = block_col; j < block_col + 3; j++)
if (borad[i][j])
if (!block_group[borad[i][j]])block_group[borad[i][j]] = 1;
else
{
printf("False:Invalid initial Sudoku matrix!\n");
printf("The number %d in the block %d has been used!\n", borad[i][j], block_count);
return false;
}
block_count++;
for (int i = 0; i < 10; i++)
{
block_group[i] = 0;
}
switch (block_count)
{
case 2:block_row = 0; block_col = 3; break;
case 3:block_row = 0; block_col = 6; break;
case 4:block_row = 3; block_col = 0; break;
case 5:block_row = 3; block_col = 3; break;
case 6:block_row = 3; block_col = 6; break;
case 7:block_row = 6; block_col = 0; break;
case 8:block_row = 6; block_col = 3; break;
case 9:block_row = 6; block_col = 6; break;
default:return false;
}
}
printf("True:Valid initial Sudoku matrix!\n");
return true;
}
int** boradgenerate() {
int count_col = 0;
int count_row = 0;
int col[3] = { 0 };
int num_true[9] = { 0 };
int col_true[9] = { 0 };
int** borad;
borad = (int**)malloc(9 * sizeof(int*));
for (int i = 0; i < 9; i++)
borad[i] = (int*)malloc(9 * sizeof(int));
for (int i = 0; i < 9; i++)
for (int j = 0; j < 9; j++)
borad[i][j] = 0;
for (count_row = 0; count_row <= 8; count_row++) {
if (count_row % 3 == 0)
for (int i = 0; i < 9; i++)
num_true[i] = 0;
while (count_col != 3)
{
int num = rand() % 9;
if (!col_true[num]) {
col[count_col++] = num;
col_true[num] = 1;
}
}
count_col = 0;
for (int i = 0; i < 9; i++)
col_true[i] = 0;
while (count_col != 3)
{
int num = rand() % 9;
if (!num_true[num]) {
borad[count_row][col[count_col++]] = num + 1;
num_true[num] = 1;
}
}
count_col = 0;
}
return borad;
}
int** change(int board[][9]) {
int** doublePointer = (int**)malloc(9 * sizeof(int*));
for (int i = 0; i < 9; i++) {
doublePointer[i] = (int*)malloc(9 * sizeof(int));
for (int j = 0; j < 9; j++) {
doublePointer[i][j] = board[i][j];
}
}
return doublePointer;
}
bool solveborad(int** borad)
{
printf("The original Sudoku matrix: \n");
isVaild(borad);
int** temp_borad=borad;
for (int i = 0 ; i < 9; i++)
{
for (int j = 0; j < 9; j++)
{
if (!borad[i][j]) {
int temp = borad[i][j];
for (int k = 1; k < 10; k++)
{
temp_borad[i][j] = k;
if (isVaild(temp_borad))
{
borad[i][j] = k;
if (solveborad(borad))
{
printf("True:Valid initial Sudoku matrix!\n");
boradprint(borad);
return true;
}
else
{
borad[i][j] = temp;
}
}
else
{
temp_borad[i][j] =temp;
}
}
printf("No solution");
return false;
}
}
}
return true;
printf("True:Valid initial Sudoku matrix!");
}
int main() {
srand(time(NULL));
int board1[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} };
int** board2=change(board1);
solveborad(board2);
return 0;
}