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
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]){
|
|
//每三行填充1~9
|
|
int i,start;
|
|
for(start=0;start<9;start+=3){
|
|
int used[10]={}; //记录1~9数是否重复
|
|
//每行填充3个数
|
|
for(i=start;i<start+3;i++){
|
|
int j=0;
|
|
while(j<3){
|
|
int num=rand()%9+1; //随机生成1~9之间的数
|
|
//判断该数是否重复
|
|
if(used[num]==0){
|
|
used[num]=1;
|
|
board[i][j]=num;
|
|
j++;
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
//将每行的数打乱
|
|
for(i=0;i<9;i++)
|
|
{
|
|
//随机选取三个位置放数
|
|
int a,b,c;
|
|
do{
|
|
a=rand()%9;
|
|
b=rand()%9;
|
|
c=rand()%9;
|
|
}while(a==b||a==c||b==c);
|
|
|
|
//将该行第a,b,c个数分别与前三个数交换
|
|
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;
|
|
}
|
|
|