|
|
@ -0,0 +1,66 @@
|
|
|
|
|
|
|
|
/*-------------------*/
|
|
|
|
|
|
|
|
/*实验:数独矩阵------*/
|
|
|
|
|
|
|
|
/*2.随机生成一个 9×9 的不完整的矩阵;*/
|
|
|
|
|
|
|
|
/*-------------------*/
|
|
|
|
|
|
|
|
#include<stdio.h>
|
|
|
|
|
|
|
|
#include<stdlib.h>
|
|
|
|
|
|
|
|
#include<time.h>
|
|
|
|
|
|
|
|
void randseedInit(){
|
|
|
|
|
|
|
|
srand(time(NULL));
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
void makeNSeq(int _seq[],int n){//产生1-n的不重复序列(传入一个数组名指针)
|
|
|
|
|
|
|
|
int ct[9] = {0, 0, 0, 0, 0, 0, 0, 0, 0};
|
|
|
|
|
|
|
|
int i;
|
|
|
|
|
|
|
|
for (i = 0; i < n; i++) {
|
|
|
|
|
|
|
|
int t = rand() % n;
|
|
|
|
|
|
|
|
while(ct[t=rand() % n]);
|
|
|
|
|
|
|
|
ct[t]++;
|
|
|
|
|
|
|
|
_seq[i] = t+1;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
void initBoard(int _board[9][9]){
|
|
|
|
|
|
|
|
int i,j;
|
|
|
|
|
|
|
|
for (i = 0; i < 9;i++)
|
|
|
|
|
|
|
|
for ( j = 0; j < 9;j++)
|
|
|
|
|
|
|
|
_board[i][j] = 0;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
void makeRandomBoard(int _board[9][9]){
|
|
|
|
|
|
|
|
initBoard(_board);
|
|
|
|
|
|
|
|
int i_1;
|
|
|
|
|
|
|
|
for (i_1 = 0; i_1 < 3;i_1++){
|
|
|
|
|
|
|
|
int seq[9];
|
|
|
|
|
|
|
|
makeNSeq(seq, 9);
|
|
|
|
|
|
|
|
int i;
|
|
|
|
|
|
|
|
for ( i = 0; i < 3;i++){
|
|
|
|
|
|
|
|
int where[9];
|
|
|
|
|
|
|
|
makeNSeq(where, 9);
|
|
|
|
|
|
|
|
int idx;
|
|
|
|
|
|
|
|
for ( idx = 0; idx < 3;idx++){
|
|
|
|
|
|
|
|
_board[i_1 * 3 + i][where[idx+i*3]-1] = seq[idx+i*3];
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
void printBoad(int _board[9][9]){
|
|
|
|
|
|
|
|
int i;
|
|
|
|
|
|
|
|
for ( i = 0; i < 9;i++){
|
|
|
|
|
|
|
|
if(!(i%3))printf("|-----------------------|\n");
|
|
|
|
|
|
|
|
int j;
|
|
|
|
|
|
|
|
for ( j = 0; j < 9;j++){
|
|
|
|
|
|
|
|
if(!(j%3))
|
|
|
|
|
|
|
|
printf("| ");
|
|
|
|
|
|
|
|
if(_board[i][j]==0)
|
|
|
|
|
|
|
|
printf(". ");
|
|
|
|
|
|
|
|
else
|
|
|
|
|
|
|
|
printf("%d ", _board[i][j]);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
printf("|\n");
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
printf("|-----------------------|\n");
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
int main(){
|
|
|
|
|
|
|
|
randseedInit();
|
|
|
|
|
|
|
|
int board[9][9];
|
|
|
|
|
|
|
|
makeRandomBoard(board);
|
|
|
|
|
|
|
|
printBoad(board);
|
|
|
|
|
|
|
|
}
|