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
56 lines
1.2 KiB
1 year ago
|
#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];
|
||
|
}
|
||
|
}
|
||
|
}
|
||
|
}
|