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.
123 lines
3.4 KiB
123 lines
3.4 KiB
2 weeks ago
|
#include <stdio.h>
|
||
|
#include <stdlib.h>
|
||
|
#include <time.h>
|
||
|
|
||
|
void printMatrix(int matrix[9][9]) {
|
||
|
for (int i = 0; i < 9; i++) {
|
||
|
if (i == 0 || i == 3 || i == 6) {
|
||
|
printf("|-----------------------|\n");
|
||
|
}
|
||
|
for (int j = 0; j < 9; j++) {
|
||
|
if (j == 0 || j == 3 || j == 6) {
|
||
|
printf("| ");
|
||
|
}
|
||
|
if (matrix[i][j] == 0)
|
||
|
{
|
||
|
printf(". ");
|
||
|
}
|
||
|
else {
|
||
|
printf("%d ", matrix[i][j]);
|
||
|
}
|
||
|
}
|
||
|
printf("|\n");
|
||
|
}
|
||
|
printf("|-----------------------|\n");
|
||
|
}
|
||
|
|
||
|
int isInRow(int matrix[9][9], int row, int num) {
|
||
|
for (int i = 0; i < 9; i++) {
|
||
|
if (matrix[row][i] == num) {
|
||
|
return 1;
|
||
|
}
|
||
|
}
|
||
|
return 0;
|
||
|
}
|
||
|
|
||
|
int isInGroup(int matrix[9][9], int startRow, int startCol, int num) {
|
||
|
for (int i = 0; i < 3; i++) {
|
||
|
for (int j = 0; j < 3; j++) {
|
||
|
if (matrix[startRow + i][startCol + j] == num) {
|
||
|
return 1;
|
||
|
}
|
||
|
}
|
||
|
}
|
||
|
return 0;
|
||
|
}
|
||
|
|
||
|
int main() {
|
||
|
srand(time(NULL));
|
||
|
int matrix[9][9] = { 0 };
|
||
|
int numbers[9] = { 1, 2, 3, 4, 5, 6, 7, 8, 9 };
|
||
|
|
||
|
for (int group = 0; group < 3; group++) {
|
||
|
int usedNumbers[9] = { 0 };
|
||
|
for (int i = 0; i < 9; i++) {
|
||
|
usedNumbers[i] = numbers[i];
|
||
|
}
|
||
|
|
||
|
for (int i = 0; i < 9; i++) {
|
||
|
int idx = rand() % (9 - i);
|
||
|
int temp = usedNumbers[idx];
|
||
|
usedNumbers[idx] = usedNumbers[8 - i];
|
||
|
usedNumbers[8 - i] = temp;
|
||
|
}
|
||
|
|
||
|
for (int row = 0; row < 3; row++) {
|
||
|
int count = 0;
|
||
|
while (count < 3) {
|
||
|
int num;
|
||
|
do {
|
||
|
int idx = rand() % 9;
|
||
|
num = usedNumbers[idx];
|
||
|
} while (num == 0 || isInRow(matrix, group * 3 + row, num) || isInGroup(matrix, group * 3, 0, num));
|
||
|
|
||
|
int pos;
|
||
|
do {
|
||
|
pos = rand() % 9;
|
||
|
} while (matrix[group * 3 + row][pos] != 0);
|
||
|
|
||
|
matrix[group * 3 + row][pos] = num;
|
||
|
count++;
|
||
|
}
|
||
|
}
|
||
|
}
|
||
|
|
||
|
for (int row = 0; row < 9; row++) {
|
||
|
int uniqueNumbers[9] = { 0 };
|
||
|
int uniqueCount = 0;
|
||
|
for (int i = 0; i < 9; i++) {
|
||
|
if (matrix[row][i] != 0 && !uniqueNumbers[matrix[row][i] - 1]) {
|
||
|
uniqueNumbers[matrix[row][i] - 1] = 1;
|
||
|
uniqueCount++;
|
||
|
}
|
||
|
}
|
||
|
if (uniqueCount > 3) {
|
||
|
while (uniqueCount > 3) {
|
||
|
int idx = rand() % 9;
|
||
|
if (matrix[row][idx] != 0 && uniqueNumbers[matrix[row][idx] - 1]) {
|
||
|
matrix[row][idx] = 0;
|
||
|
uniqueCount--;
|
||
|
}
|
||
|
}
|
||
|
}
|
||
|
else if (uniqueCount < 3) {
|
||
|
while (uniqueCount < 3) {
|
||
|
int num;
|
||
|
do {
|
||
|
num = rand() % 9 + 1;
|
||
|
} while (uniqueNumbers[num - 1]);
|
||
|
int idx;
|
||
|
do {
|
||
|
idx = rand() % 9;
|
||
|
} while (matrix[row][idx] != 0);
|
||
|
matrix[row][idx] = num;
|
||
|
uniqueNumbers[num - 1] = 1;
|
||
|
uniqueCount++;
|
||
|
}
|
||
|
}
|
||
|
}
|
||
|
|
||
|
printMatrix(matrix);
|
||
|
|
||
|
return 0;
|
||
|
}
|