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.

56 lines
1.2 KiB

#include "SudokuMatrix.h"
#include <stdio.h>
/**
* @brief
*
* @param a
* @param b
*/
void Swap(int*a, int *b){
int temp = *a;
*a = *b;
*b = temp;
}
/**
* @brief int
*
* @param array
* @param length
*/
void Shuffle(int* array, int length){
if (length != 0){
for (int i = length - 1; i > 0; i--) {
int j = rand() % (i + 1);
Swap(&array[i], &array[j]);
}
}
}
void RandomMatrix(SudokuMatrix* matrix){
int random_row[9] = {1,2,3,4,5,6,7,8,9};
int random_place[9] = {0, 1, 2, 3, 4, 5, 6, 7, 8};
// 重置矩阵内容为全0
for (int i=0;i<9;i++){
for (int j=0;j<9;j++){
matrix->matrix[i][j] = 0;
}
}
for (int i=0;i<3;i++){
// 先生成这九个数的顺序,再随机生成这九个数应该在哪里。
Shuffle(random_row,9);
for (int j=0;j<3;j++){
Shuffle(random_place,9);
for (int k=0;k<3;k++){
matrix->matrix[i*3+j][random_place[k]] = random_row[j*3+k];
}
}
}
}