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.
54 lines
1.0 KiB
54 lines
1.0 KiB
#include <iostream>
|
|
#include <stdlib.h>
|
|
#include <time.h>
|
|
int main()
|
|
{
|
|
int board[9][9] = {0};
|
|
srand(time(NULL));
|
|
void generate(int board[9][9]);
|
|
void format(int board[9][9]);
|
|
generate(board);
|
|
format(board);
|
|
}
|
|
void random(int arr[9]) {
|
|
for (int i = 0; i < 9; i++) {
|
|
arr[i] = rand() % 9 + 1;
|
|
Re:
|
|
for (int j = 0; j < i; j++) {
|
|
while (arr[i] == arr[j]) {
|
|
arr[i] = rand() % 9 + 1;
|
|
goto Re;
|
|
}
|
|
}
|
|
}
|
|
}
|
|
void format(int board[9][9]) {
|
|
for (int i = 0; i < 9; i++) {
|
|
for (int j = 0; j < 9; j++) {
|
|
printf("%d ", board[i][j]);
|
|
if ((j + 1) % 3 == 0 && j != 8) {
|
|
printf("| ");
|
|
}
|
|
}
|
|
if ((i + 1) % 3 == 0 && i != 8) {
|
|
printf("\n---------------------\n");
|
|
}
|
|
else { printf("\n"); }
|
|
}
|
|
}
|
|
void generate(int board[9][9]) {
|
|
int rowplace[9] = { 0 }, rownum[9] = { 0 };
|
|
for (int h = 0; h < 9; h += 3) {
|
|
random(rownum);
|
|
for (int i = h, k = -1; i < h + 3; i++) {
|
|
random(rowplace);
|
|
for (int j = 0; j < 9; j++) {
|
|
if (rowplace[j] < 4) {
|
|
k++;
|
|
board[i][j] = rownum[k];
|
|
}
|
|
else board[i][j] = 0;
|
|
}
|
|
}
|
|
}
|
|
} |