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.
53 lines
886 B
53 lines
886 B
#include<stdio.h>
|
|
#include<stdlib.h>
|
|
#include<time.h>
|
|
|
|
int main(){
|
|
|
|
srand(time(NULL));
|
|
int board[9][9];
|
|
|
|
int num[9]={1,2,3,4,5,6,7,8,9};
|
|
for(int i =0;i<9;i++)
|
|
{
|
|
if(i%3==0)
|
|
{
|
|
for(int j =0;j<9;j++)
|
|
{
|
|
int k =rand()%9;
|
|
int temp = num[j];
|
|
num[j] = num[k];
|
|
num[k] = temp;
|
|
}
|
|
}
|
|
for(int j = 0;j<9;j++) //定义矩阵各元素为0
|
|
{
|
|
board[i][j] = 0;
|
|
}
|
|
for(int m = 0;m<3;m++)
|
|
{
|
|
int index;
|
|
do{
|
|
index = rand()%9;
|
|
}while(board[i][index]!=0);
|
|
board[i][index] = num[3*(i%3)+m];
|
|
}
|
|
}
|
|
|
|
printf("The original Sudoku matrix: \n"); //格式化输出
|
|
for(int i=0;i<9;i++){
|
|
if(i%3==0){
|
|
printf("-------------\n");
|
|
}
|
|
for(int j=0;j<9;j++){
|
|
if(j%3==0||j==9){
|
|
printf("|");
|
|
}
|
|
printf("%d",board[i][j]);
|
|
}
|
|
printf("|\n");
|
|
}printf("-------------\n");
|
|
|
|
return 0;
|
|
}
|