ADD file via upload

main
p8btn9vm4 2 weeks ago
parent bd90efcd6d
commit b5aa40d66d

@ -0,0 +1,49 @@
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <stdbool.h>
void print_sudoku(int matrix[9][9]) {
for (int i = 0; i < 9; i++) {
for (int j = 0; j < 9; j++) {
printf("%d ", matrix[i][j]);
if ((j + 1) % 3 == 0 && j != 8) {
printf("| ");
}
}
printf("\n");
if ((i + 1) % 3 == 0 && i != 8) {
printf("------+-------+------\n");
}
}
}
bool is_valid(int* nums, int num) {
for (int i = 0; i < 3; i++) {
if (nums[i] == num) return false; // 检查数字是否重复
}
return true;
}
int main() {
srand(time(NULL)); // 初始化随机数种子
int sudoku_matrix[9][9] = { 0 }; // 初始化数独矩阵
for (int i = 0; i < 3; i++) {
int count[3] = { 0 };
for (int j = 1; j < 10; j++) {
int row = rand() % 3;
int col = rand() % 9;
while (sudoku_matrix[3 * i + row][col] != 0 || count[row] == 3) {
row = rand() % 3;
col = rand() % 9;
}
sudoku_matrix[3 * i + row][col] = j;
count[row]++;
}
}
print_sudoku(sudoku_matrix);
return 0;
}
Loading…
Cancel
Save