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.
52 lines
1.4 KiB
52 lines
1.4 KiB
3 days ago
|
#include <stdio.h>
|
||
|
#include <stdlib.h>
|
||
|
#include <time.h>
|
||
|
#define SIZE 9
|
||
|
void printSudoku(int matrix[SIZE][SIZE]) {
|
||
|
for (int i = 0; i < SIZE; i++) {
|
||
|
if (i % 3 == 0) {
|
||
|
printf("-------------------------\n");
|
||
|
}
|
||
|
for (int j = 0; j < SIZE; j++) {
|
||
|
if (j % 3 == 0) {
|
||
|
printf("| ");
|
||
|
}
|
||
|
printf("%d ", matrix[i][j]);
|
||
|
}
|
||
|
printf("|\n");
|
||
|
}
|
||
|
printf("-------------------------\n");
|
||
|
}
|
||
|
void shuffleArray(int arr[], int size) {
|
||
|
for (int i = size - 1; i > 0; i--) {
|
||
|
int j = rand() % (i + 1);
|
||
|
int temp = arr[i];
|
||
|
arr[i] = arr[j];
|
||
|
arr[j] = temp;
|
||
|
}
|
||
|
}
|
||
|
void generateIncompleteMatrix(int matrix[SIZE][SIZE]) {
|
||
|
srand(time(0));
|
||
|
int nums[SIZE] = { 1, 2, 3, 4, 5, 6, 7, 8, 9 };
|
||
|
for (int blockRow = 0; blockRow < SIZE; blockRow += 3) {
|
||
|
shuffleArray(nums, SIZE);
|
||
|
int idx = 0;
|
||
|
for (int i = 0; i < 3; i++) {
|
||
|
int count = 3;
|
||
|
while (count > 0) {
|
||
|
int j = rand() % SIZE;
|
||
|
if (matrix[blockRow + i][j] == 0) {
|
||
|
matrix[blockRow + i][j] = nums[idx++];
|
||
|
count--;
|
||
|
}
|
||
|
}
|
||
|
}
|
||
|
}
|
||
|
}
|
||
|
int main() {
|
||
|
int matrix[SIZE][SIZE] = { 0 };
|
||
|
generateIncompleteMatrix(matrix);
|
||
|
printSudoku(matrix);
|
||
|
return 0;
|
||
|
}
|