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.
147 lines
3.7 KiB
147 lines
3.7 KiB
#include <stdio.h>
|
|
#include <stdbool.h>
|
|
#include <stdlib.h>
|
|
#include <time.h>
|
|
|
|
bool isValid(int grid[9][9], int r, int c, int num);
|
|
|
|
void generateIncompleteGrid(int grid[9][9]) {
|
|
srand(time(NULL));
|
|
int i, j, block;
|
|
for (i = 0; i < 9; i++) {
|
|
for (j = 0; j < 9; j++) {
|
|
grid[i][j] = 0;
|
|
}
|
|
}
|
|
for (block = 0; block < 3; block++) {
|
|
int used[10] = {0};
|
|
for (i = 0; i < 3; i++) {
|
|
int r = block * 3 + i;
|
|
int placedNum = 0;
|
|
while (placedNum < 3) {
|
|
int num = (rand() % 9) + 1;
|
|
int c = rand() % 9;
|
|
if (grid[r][c] == 0 && isValid(grid, r, c, num) && used[num] == 0) {
|
|
grid[r][c] = num;
|
|
used[num] = 1;
|
|
placedNum++;
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
void print(int grid[9][9]) {
|
|
int i, j;
|
|
for (i = 0; i < 9; i++) {
|
|
for (j = 0; j < 9; j++) {
|
|
printf("%2d ", grid[i][j]);
|
|
if ((j + 1) % 3 == 0 && j < 8) {
|
|
printf("| ");
|
|
}
|
|
}
|
|
printf("\n");
|
|
if ((i + 1) % 3 == 0 && i < 8) {
|
|
printf("-----------------------\n");
|
|
}
|
|
}
|
|
}
|
|
|
|
bool isValid(int grid[9][9], int r, int c, int num) {
|
|
int x, i, j;
|
|
for (x = 0; x < 9; x++) {
|
|
if (grid[r][x] == num || grid[x][c] == num) {
|
|
return false;
|
|
}
|
|
}
|
|
|
|
int startR = r - r % 3, startC = c - c % 3;
|
|
for (i = 0; i < 3; i++) {
|
|
for (j = 0; j < 3; j++) {
|
|
if (grid[i + startR][j + startC] == num) {
|
|
return false;
|
|
}
|
|
}
|
|
}
|
|
return true;
|
|
}
|
|
|
|
bool solve(int grid[9][9]) {
|
|
int r, c;
|
|
bool empty = false;
|
|
|
|
for (r = 0; r < 9; r++) {
|
|
for (c = 0; c < 9; c++) {
|
|
if (grid[r][c] == 0) {
|
|
empty = true;
|
|
break;
|
|
}
|
|
}
|
|
if (empty) break;
|
|
}
|
|
|
|
if (!empty) return true;
|
|
|
|
int num;
|
|
for (num = 1; num <= 9; num++) {
|
|
if (isValid(grid, r, c, num)) {
|
|
grid[r][c] = num;
|
|
|
|
if (solve(grid)) {
|
|
return true;
|
|
}
|
|
|
|
grid[r][c] = 0;
|
|
}
|
|
}
|
|
return false;
|
|
}
|
|
|
|
bool validate(int grid[9][9]) {
|
|
int r, c;
|
|
for (r = 0; r < 9; r++) {
|
|
for (c = 0; c < 9; c++) {
|
|
if (grid[r][c] != 0) {
|
|
int num = grid[r][c];
|
|
grid[r][c] = 0;
|
|
if (!isValid(grid, r, c, num)) {
|
|
return false;
|
|
}
|
|
grid[r][c] = num;
|
|
}
|
|
}
|
|
}
|
|
return true;
|
|
}
|
|
|
|
int main() {
|
|
int grid[9][9];
|
|
int board0[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}};
|
|
|
|
generateIncompleteGrid(grid);
|
|
printf("The original Sudoku matrix:\n");
|
|
print(board0);
|
|
|
|
if (validate(board0)) {
|
|
printf("True:Valid initial Sudoku matrix!\n");
|
|
if (solve(board0)) {
|
|
printf("Completed Sudoku Grid:\n");
|
|
print(board0);
|
|
} else {
|
|
printf("No solution exists for this Sudoku.\n");
|
|
}
|
|
} else {
|
|
printf("False:Invalid initial Sudoku matrix!\n");
|
|
}
|
|
return 0;
|
|
}
|
|
|