|
|
|
@ -0,0 +1,123 @@
|
|
|
|
|
#include<stdio.h>
|
|
|
|
|
#include<stdlib.h>
|
|
|
|
|
#include<time.h>
|
|
|
|
|
|
|
|
|
|
#define N 9
|
|
|
|
|
|
|
|
|
|
void print(int matrix[9][9]);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
int main()
|
|
|
|
|
{
|
|
|
|
|
srand(time(NULL));
|
|
|
|
|
|
|
|
|
|
int matrix[9][9]={0};
|
|
|
|
|
int i=1,j=1;
|
|
|
|
|
int col=0;
|
|
|
|
|
int val=0;
|
|
|
|
|
|
|
|
|
|
int k=0,l=0; //此循环变量用于判断随机生成的数是否重复
|
|
|
|
|
int isrepeat=0;
|
|
|
|
|
for(i=0;i<9;i++) //每一行
|
|
|
|
|
{
|
|
|
|
|
for(j=0;j<3;j++) //三个位置
|
|
|
|
|
{
|
|
|
|
|
label_col:
|
|
|
|
|
col=rand()%9;
|
|
|
|
|
if(matrix[i][col]!=0)
|
|
|
|
|
{
|
|
|
|
|
goto label_col; //如果随机生成的列对应的值不等于0,则重新生成随机列
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
if(i>=0&&i<3)
|
|
|
|
|
{
|
|
|
|
|
label_val1:
|
|
|
|
|
val=rand()%9+1; //生成随机数值
|
|
|
|
|
isrepeat=0; //此变量用于记录随机数值是否重复
|
|
|
|
|
for(k=0;k<3;k++) //此循环用于判断生成的值是否重复
|
|
|
|
|
{
|
|
|
|
|
for(l=0;l<9;l++)
|
|
|
|
|
{
|
|
|
|
|
if(val==matrix[k][l])
|
|
|
|
|
{
|
|
|
|
|
isrepeat=1;
|
|
|
|
|
goto label_val1;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
else if(i>=3&&i<6)
|
|
|
|
|
{
|
|
|
|
|
label_val2:
|
|
|
|
|
val=rand()%9+1; //生成随机数值
|
|
|
|
|
isrepeat=0;
|
|
|
|
|
for(k=3;k<6;k++)
|
|
|
|
|
{
|
|
|
|
|
for(l=0;l<9;l++)
|
|
|
|
|
{
|
|
|
|
|
if(val==matrix[k][l])
|
|
|
|
|
{
|
|
|
|
|
isrepeat=1;
|
|
|
|
|
goto label_val2;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
else if(i>=6&&i<9)
|
|
|
|
|
{
|
|
|
|
|
label_val3:
|
|
|
|
|
val=rand()%9+1; //生成随机数值
|
|
|
|
|
isrepeat=0;
|
|
|
|
|
for(k=6;k<9;k++)
|
|
|
|
|
{
|
|
|
|
|
for(l=0;l<9;l++)
|
|
|
|
|
{
|
|
|
|
|
if(val==matrix[k][l])
|
|
|
|
|
{
|
|
|
|
|
isrepeat=1;
|
|
|
|
|
goto label_val3;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
if(isrepeat==0)
|
|
|
|
|
{
|
|
|
|
|
matrix[i][col]=val;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
print(matrix);
|
|
|
|
|
// for(i=0;i<9;i++)
|
|
|
|
|
// {
|
|
|
|
|
// for(j=0;j<9;j++)
|
|
|
|
|
// {
|
|
|
|
|
// printf("%d ",matrix[i][j]);
|
|
|
|
|
// }
|
|
|
|
|
// printf("\n");
|
|
|
|
|
// }
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
void print(int matrix[9][9])
|
|
|
|
|
{
|
|
|
|
|
int i=0,j=0;
|
|
|
|
|
printf("|-----------------------------|\n");
|
|
|
|
|
for ( i = 0; i < N; i++) {
|
|
|
|
|
printf("|");
|
|
|
|
|
for (j = 0; j < N; j++) {
|
|
|
|
|
|
|
|
|
|
printf("%2d ", matrix[i][j]);
|
|
|
|
|
if ((j + 1) % 3 == 0 ) printf("|");
|
|
|
|
|
}
|
|
|
|
|
printf("\n");
|
|
|
|
|
if ((i + 1) % 3 == 0 && i < 8) {
|
|
|
|
|
printf("|-----------------------------|\n");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
printf("|-----------------------------|\n");
|
|
|
|
|
}
|