To draw a 9 x9 sudoku, now through loop to check whether a certain position is empty (0), if it is, in rows, columns, scratchable latex for candidate number,
From the number 1 began to check for the first time, every time to fill out one of the first four digits of the ninth, stuck not to continue, a fan,
CodePudding user response:
# include & lt; stdio.h># include & lt; Stdlib. H>
# include & lt; Time. H>
Int main () {
Int arr [9] [9]={0};//specification sudoku size
int i, j;//I: one-dimensional subscript j: two-dimensional subscript
Int label_1, label_2 label_3;
//label_1: whether the current row label_2 fill condition: the current label_3: current scratchable latex
Srand (time (NULL));//the random number seed
For (int num=1; Num<=9; Num++) {//in turn, fill in the Numbers 1 to 9
For (int count=1; Count<=9; Count++) {//each Numbers into nine times
Do {
I=rand () % 9;//a one-dimensional subscript
J=rand () % 9;//the two-dimensional subscript
Label_1=0;//the initial value
Label_2=0;
Label_3=0;
If (arr [I] [j].=0) continue;
For (int k=0; K<9. K++)
If (arr [I] [k]. Num=) label_1 + +;
For (int m=0; M<9. M++)
If (arr [m] [j]. Num=) label_2 + +;
For (int p=I/3 * 3; pFor (int q=j/3 * 3; Q
} while (label_1 & lt; 9 | | label_2 & lt; 9 | | label_3 & lt; 9);
Arr [I] [j]=num;
}
}
Printf (" \ n \ n \ n ");
for (int i=0; I<9. I++) {
for (int j=0; J<9. J++) {
Printf (" % d ", arr [I] [j]);
If ((j + 1) % 3==0) printf (" ");
}
Printf (" \ n \ n ");
If ((I + 1) % 3==0) printf (" \ n ");
}
return 0;
}