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.
51 lines
1.1 KiB
51 lines
1.1 KiB
#include <stdio.h>
|
|
#include <stdlib.h>
|
|
#include <time.h>
|
|
|
|
void print_sudoku(char sudoku[9][9]) {
|
|
for (int i = 0; i < 9; i++) {
|
|
if (i % 3 == 0 && i != 0) {
|
|
printf("-------------------------\n");
|
|
}
|
|
for (int j = 0; j < 9; j++) {
|
|
if (j % 3 == 0 && j != 0) {
|
|
printf("| ");
|
|
}
|
|
printf("%c ", sudoku[i][j]);
|
|
}
|
|
printf("\n");
|
|
}
|
|
}
|
|
|
|
int main() {
|
|
char sudoku_board[9][9];
|
|
|
|
// 初始化随机数种子
|
|
srand(time(NULL));
|
|
|
|
// 生成不完整数独盘
|
|
for (int i = 0; i < 9; i++) {
|
|
for (int j = 0; j < 9; j++) {
|
|
sudoku_board[i][j] = '.';
|
|
}
|
|
}
|
|
|
|
for (int i = 0; i < 9; i++) {
|
|
for (int count = 0; count < 3; count++) {
|
|
int j;
|
|
do {
|
|
j = rand() % 9;
|
|
} while (sudoku_board[i][j] != '.');
|
|
int x;
|
|
do {
|
|
x = (rand() % 9) + 1;
|
|
} while (strchr(sudoku_board[i], x + '0'));
|
|
sudoku_board[i][j] = x + '0';
|
|
}
|
|
}
|
|
|
|
print_sudoku(sudoku_board);
|
|
|
|
return 0;
|
|
}
|