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.

73 lines
1.3 KiB

#include <stdio.h>
#include <stdlib.h>
#include <time.h>
void print(int board[9][9]){
printf("|-----------------------|\n");
int i,j;
for(i=0;i<9;i++){
printf("| ");
for(j=0;j<9;j++){
if(board[i][j]==0) printf(". ");
else printf("%d ",board[i][j]);
if(j%3==2) printf("| ");
}
printf("\n");
if(i%3==2){
printf("|-----------------------|\n");
}
}
}
void fill(int board[9][9]){
//ÿ<><C3BF><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>1~9
int i,start;
for(start=0;start<9;start+=3){
int used[10]={}; //<2F><>¼1~9<><39><EFBFBD>Ƿ<EFBFBD><C7B7>ظ<EFBFBD>
//ÿ<><C3BF><EFBFBD><EFBFBD><EFBFBD><EFBFBD>3<EFBFBD><33><EFBFBD><EFBFBD>
for(i=start;i<start+3;i++){
int j=0;
while(j<3){
int num=rand()%9+1; //<2F><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>1~9֮<39><D6AE><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
//<2F>жϸ<D0B6><CFB8><EFBFBD><EFBFBD>Ƿ<EFBFBD><C7B7>ظ<EFBFBD>
if(used[num]==0){
used[num]=1;
board[i][j]=num;
j++;
}
}
}
}
//<2F><>ÿ<EFBFBD>е<EFBFBD><D0B5><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
for(i=0;i<9;i++)
{
//<2F><><EFBFBD><EFBFBD>ѡȡ<D1A1><C8A1><EFBFBD><EFBFBD>λ<EFBFBD>÷<EFBFBD><C3B7><EFBFBD>
int a,b,c;
do{
a=rand()%9;
b=rand()%9;
c=rand()%9;
}while(a==b||a==c||b==c);
//<2F><><EFBFBD><EFBFBD><EFBFBD>е<EFBFBD>a,b,c<><63><EFBFBD><EFBFBD><EFBFBD>ֱ<EFBFBD><D6B1><EFBFBD>ǰ<EFBFBD><C7B0><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
int t[3],j;
for(j=0;j<3;j++) t[j]=board[i][j];
for(j=0;j<3;j++) board[i][j]=0;
board[i][a]=t[0];
board[i][b]=t[1];
board[i][c]=t[2];
}
}
int main(){
srand(time(NULL));
int board[9][9]={};
fill(board);
print(board);
return 0;
}