|
|
|
|
#include <stdio.h>
|
|
|
|
|
#include <stdlib.h>
|
|
|
|
|
#include <time.h>
|
|
|
|
|
|
|
|
|
|
// <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>1<EFBFBD><31>9<EFBFBD><39><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
|
|
|
|
void shuffle(int arr[], int n) {
|
|
|
|
|
if (n > 1) {
|
|
|
|
|
int i;
|
|
|
|
|
for (i = 0; i < n - 1; i++) {
|
|
|
|
|
int j = i + rand() % (n - i);
|
|
|
|
|
int temp = arr[i];
|
|
|
|
|
arr[i] = arr[j];
|
|
|
|
|
arr[j] = temp;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
//<2F><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ƚ<EFBFBD><C8BD><EFBFBD><EFBFBD>۵<EFBFBD><DBB5><EFBFBD><EFBFBD><EFBFBD>
|
|
|
|
|
void printSudoku(int sudoku[9][9]) {
|
|
|
|
|
printf("+-------+-------+-------+\n");
|
|
|
|
|
for (int i = 0; i < 9; i++) {
|
|
|
|
|
if (i > 0 && i % 3 == 0) {
|
|
|
|
|
printf("+-------+-------+-------+\n");
|
|
|
|
|
}
|
|
|
|
|
for (int j = 0; j < 9; j++) {
|
|
|
|
|
if (j % 3 == 0) {
|
|
|
|
|
printf("| ");
|
|
|
|
|
}
|
|
|
|
|
printf("%d ", sudoku[i][j]); // <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>е<EFBFBD>Ԫ<EFBFBD><D4AA>
|
|
|
|
|
}
|
|
|
|
|
printf("|\n");
|
|
|
|
|
}
|
|
|
|
|
printf("+-------+-------+-------+\n");
|
|
|
|
|
}
|
|
|
|
|
int main() {
|
|
|
|
|
// <20><>ʼ<EFBFBD><CABC><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
|
|
|
|
srand(time(0));
|
|
|
|
|
|
|
|
|
|
int grid[9][9] = {0}; // <20><><EFBFBD><EFBFBD>3x9<78><39><EFBFBD><EFBFBD><EFBFBD>鲢<EFBFBD><E9B2A2>ʼ<EFBFBD><CABC>Ϊ0
|
|
|
|
|
|
|
|
|
|
int numbers[9] = {1, 2, 3, 4, 5, 6, 7, 8, 9}; // <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>1<EFBFBD><31>9<EFBFBD><39><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
|
|
|
|
|
|
|
|
|
// <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>е<EFBFBD><D0B5><EFBFBD><EFBFBD><EFBFBD>
|
|
|
|
|
shuffle(numbers, 9);
|
|
|
|
|
|
|
|
|
|
int row, col;
|
|
|
|
|
int numIndex = 0;
|
|
|
|
|
|
|
|
|
|
// <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD>˳<EFBFBD><CBB3><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>䵽<EFBFBD><E4B5BD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
|
|
|
|
for (row = 0; row < 3; row++)
|
|
|
|
|
{
|
|
|
|
|
int aa=0,bb=0,cc=0;
|
|
|
|
|
while(aa==bb||bb==cc||aa==cc)
|
|
|
|
|
{
|
|
|
|
|
aa=rand()%9;
|
|
|
|
|
bb= rand()%9;
|
|
|
|
|
cc=rand()%9;
|
|
|
|
|
}
|
|
|
|
|
grid[row][aa] = numbers[numIndex];
|
|
|
|
|
numIndex++;
|
|
|
|
|
grid[row][bb] = numbers[numIndex];
|
|
|
|
|
numIndex++;
|
|
|
|
|
grid[row][cc] = numbers[numIndex];
|
|
|
|
|
numIndex++;
|
|
|
|
|
}
|
|
|
|
|
// <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>е<EFBFBD><D0B5><EFBFBD><EFBFBD><EFBFBD>
|
|
|
|
|
shuffle(numbers, 9);
|
|
|
|
|
|
|
|
|
|
numIndex = 0;
|
|
|
|
|
|
|
|
|
|
// <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD>˳<EFBFBD><CBB3><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>䵽<EFBFBD><E4B5BD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
|
|
|
|
for (row = 3; row < 6; row++)
|
|
|
|
|
{
|
|
|
|
|
int aa=0,bb=0,cc=0;
|
|
|
|
|
while(aa==bb||bb==cc||aa==cc)
|
|
|
|
|
{
|
|
|
|
|
aa=rand()%9;
|
|
|
|
|
bb= rand()%9;
|
|
|
|
|
cc=rand()%9;
|
|
|
|
|
}
|
|
|
|
|
grid[row][aa] = numbers[numIndex];
|
|
|
|
|
numIndex++;
|
|
|
|
|
grid[row][bb] = numbers[numIndex];
|
|
|
|
|
numIndex++;
|
|
|
|
|
grid[row][cc] = numbers[numIndex];
|
|
|
|
|
numIndex++;
|
|
|
|
|
}
|
|
|
|
|
// <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>е<EFBFBD><D0B5><EFBFBD><EFBFBD><EFBFBD>
|
|
|
|
|
shuffle(numbers, 9);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
numIndex = 0;
|
|
|
|
|
|
|
|
|
|
// <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD>˳<EFBFBD><CBB3><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>䵽<EFBFBD><E4B5BD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
|
|
|
|
for (row = 6; row < 9; row++)
|
|
|
|
|
{
|
|
|
|
|
int aa=0,bb=0,cc=0;
|
|
|
|
|
while(aa==bb||bb==cc||aa==cc)
|
|
|
|
|
{
|
|
|
|
|
aa=rand()%9;
|
|
|
|
|
bb= rand()%9;
|
|
|
|
|
cc=rand()%9;
|
|
|
|
|
}
|
|
|
|
|
grid[row][aa] = numbers[numIndex];
|
|
|
|
|
numIndex++;
|
|
|
|
|
grid[row][bb] = numbers[numIndex];
|
|
|
|
|
numIndex++;
|
|
|
|
|
grid[row][cc] = numbers[numIndex];
|
|
|
|
|
numIndex++;
|
|
|
|
|
}
|
|
|
|
|
// <20><>ӡ<EFBFBD><D3A1><EFBFBD><EFBFBD>
|
|
|
|
|
printSudoku(grid);
|
|
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|