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.

50 lines
1.2 KiB

#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; // <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ƿ<EFBFBD><C7B7>ظ<EFBFBD>
}
return true;
}
int main() {
srand(time(NULL)); // <20><>ʼ<EFBFBD><CABC><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
int sudoku_matrix[9][9] = { 0 }; // <20><>ʼ<EFBFBD><CABC><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
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;
}