diff --git a/8.c b/8.c new file mode 100644 index 0000000..4bd9a1a --- /dev/null +++ b/8.c @@ -0,0 +1,51 @@ +#include +#include +#include +#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; +}