ADD file via upload

main
p7cx2jokr 4 weeks ago
parent c9edc3255b
commit 83ca42e1c2

51
8.c

@ -0,0 +1,51 @@
#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;
}
Loading…
Cancel
Save