Update complement.c

ver2.0
psoyatper 1 year ago
parent 2574b59055
commit 2c2668d4ad

@ -1,171 +1,162 @@
#include <stdio.h> #include <stdio.h>
#include <stdbool.h> #include <stdbool.h>
#include <stdlib.h> #include <stdlib.h>
#include <time.h> #include <time.h>
void shuffleArray(int array[], int size) { void shuffleArray(int array[], int size) {
for (int i = size - 1; i > 0; i--) { for (int i = size - 1; i > 0; i--) {
int j = rand() % (i + 1); int j = rand() % (i + 1);
int temp = array[i]; int temp = array[i];
array[i] = array[j]; array[i] = array[j];
array[j] = temp; array[j] = temp;
} }
} }
void create(int board[9][9]){ void create(int board[9][9]){
srand(time(NULL)); srand(time(NULL));
int index = 0; int index = 0;
for (int r = 0; r < 9; r+=3){ for (int r = 0; r < 9; r+=3){
int nums[9] = {1, 2, 3, 4, 5, 6, 7, 8, 9}; int nums[9] = {1, 2, 3, 4, 5, 6, 7, 8, 9};
shuffleArray(nums, 9); shuffleArray(nums, 9);
for (int i = r; i < r+3; i++) { for (int i = r; i < r+3; i++) {
for (int j = 0; j < 9; j += 3) { for (int j = 0; j < 9; j += 3) {
board[i][j] = nums[index++]; board[i][j] = nums[index++];
if (index >= 9) { if (index >= 9) {
index = 0; index = 0;
} }
} }
} }
} }
for(int j = 0;j < 9;j++){ for(int j = 0;j < 9;j++){
shuffleArray(board[j], 9); shuffleArray(board[j], 9);
} }
} }
bool check(int matrix[9][9]) { bool check(int matrix[9][9]) {
for (int i = 0; i < 9; i++) { for (int i = 0; i < 9; i++) {
int count[10] = {0}; int count[10] = {0};
for (int j = 0; j < 9; j++) { for (int j = 0; j < 9; j++) {
int num = matrix[i][j]; int num = matrix[i][j];
if (num != 0) { if (num != 0) {
count[num]++; count[num]++;
if (count[num] > 1) { if (count[num] > 1) {
printf("False: The number %d in the row %d has been used!\n", num, i + 1); printf("False: The number %d in the row %d has been used!\n", num, i + 1);
return false; return false;
} }
} }
} }
} }
for (int j = 0; j < 9; j++) { for (int j = 0; j < 9; j++) {
int count[10] = {0}; int count[10] = {0};
for (int i = 0; i < 9; i++) { for (int i = 0; i < 9; i++) {
int num = matrix[i][j]; int num = matrix[i][j];
if (num != 0) { if (num != 0) {
count[num]++; count[num]++;
if (count[num] > 1) { if (count[num] > 1) {
printf("False: The number %d in the col %d has been used!\n", num, j + 1); printf("False: The number %d in the col %d has been used!\n", num, j + 1);
return false; return false;
} }
} }
} }
} }
for (int block = 0; block < 9; block++) { for (int block = 0; block < 9; block++) {
int count[10] = {0}; int count[10] = {0};
int startRow = (block / 3) * 3; int startRow = (block / 3) * 3;
int startCol = (block % 3) * 3; int startCol = (block % 3) * 3;
for (int i = startRow; i < startRow + 3; i++) { for (int i = startRow; i < startRow + 3; i++) {
for (int j = startCol; j < startCol + 3; j++) { for (int j = startCol; j < startCol + 3; j++) {
int num = matrix[i][j]; int num = matrix[i][j];
if (num != 0) { if (num != 0) {
count[num]++; count[num]++;
if (count[num] > 1) { if (count[num] > 1) {
printf("False: The number %d in the block %d has been used!\n", num, block + 1); printf("False: The number %d in the block %d has been used!\n", num, block + 1);
return false; return false;
} }
} }
} }
} }
} }
printf("True: Valid initial Sudoku matrix!\n"); printf("True: Valid initial Sudoku matrix!\n");
} }
bool complement(int board[9][9], int row, int col) { bool complement(int board[9][9], int row, int col) {
if (row == 9) { if (row == 9) {
return true; return true;
} }
if (board[row][col] != 0) { if (board[row][col] != 0) {
if (col == 8) { if (col == 8) {
return complement(board, row + 1, 0); return complement(board, row + 1, 0);
} else { } else {
return complement(board, row, col + 1); return complement(board, row, col + 1);
} }
} }
for (int num = 1; num <= 9; num++) { for (int num = 1; num <= 9; num++) {
bool valid = true; bool valid = true;
for (int i = 0; i < 9; i++) { for (int i = 0; i < 9; i++) {
if (board[row][i] == num || board[i][col] == num || if (board[row][i] == num || board[i][col] == num ||
board[(row / 3) * 3 + i / 3][(col / 3) * 3 + i % 3] == num) { board[(row / 3) * 3 + i / 3][(col / 3) * 3 + i % 3] == num) {
valid = false; valid = false;
break; break;
} }
} }
if (valid) { if (valid) {
board[row][col] = num; board[row][col] = num;
if (col == 8) { if (col == 8) {
if (complement(board, row + 1, 0)) { if (complement(board, row + 1, 0)) {
return true; return true;
} }
} else { } else {
if (complement(board, row, col + 1)) { if (complement(board, row, col + 1)) {
return true; return true;
} }
} }
board[row][col] = 0; board[row][col] = 0;
} }
} }
return false; return false;
} }
void printSudokuSolution(int matrix[9][9]) {
printf("The solution of Sudoku matrix:\n"); void output(int board[9][9]){
for (int i = 0; i < 9; i++) { int i;
for (int j = 0; j < 9; j++) { for(i = 0;i < 9 ;i++){
printf("%d ", matrix[i][j]); if(i %3 == 0){
} printf("|-----------------------|\n");
printf("\n"); }
} printf("| %d %d %d | %d %d %d | %d %d %d |\n",
} board[i][0],board[i][1],board[i][2],board[i][3],board[i][4],board[i][5],board[i][6],board[i][7],board[i][8]);
}
printf("|-----------------------|\n");
void output(int board[9][9]){ }
int i;
for(i = 0;i < 9 ;i++){
if(i %3 == 0){
printf("|-----------------------|\n");
} int main() {
printf("| %d %d %d | %d %d %d | %d %d %d |\n", int board[9][9] = {0};
board[i][0],board[i][1],board[i][2],board[i][3],board[i][4],board[i][5],board[i][6],board[i][7],board[i][8]); create(board);
} printf("The original Sudoku matrix:\n");
printf("|-----------------------|\n"); output(board);
} if (check(board)) {
if (complement(board, 0, 0)) {
printf("The solution of Sudoku matrix:\n");
output(board);
} else {
int main() { printf("No solution!\n");
int board[9][9] = {0}; }
create(board); } else {
printf("The original Sudoku matrix:\n"); printf("No solution!\n");
output(board); }
if (check(board)) {
if (complement(board, 0, 0)) { return 0;
printSudokuSolution(board);
} else {
printf("No solution!\n");
}
} else {
printf("No solution!\n");
}
return 0;
} }
Loading…
Cancel
Save