|
|
|
|
//#include <stdio.h>
|
|
|
|
|
//#include <stdlib.h>
|
|
|
|
|
//#include <time.h>
|
|
|
|
|
//#include <stdbool.h>
|
|
|
|
|
//
|
|
|
|
|
//void printMatrix(int matrix[9][9]);
|
|
|
|
|
//int isInRow(int matrix[9][9], int row, int num);
|
|
|
|
|
//int isInGroup(int matrix[9][9], int startRow, int startCol, int num);
|
|
|
|
|
//bool checkRows(int matrix[9][9]);
|
|
|
|
|
//bool checkColumns(int matrix[9][9]);
|
|
|
|
|
//bool checkSubMatrices(int matrix[9][9]);
|
|
|
|
|
//bool isSudokuMatrix(int matrix[9][9]);
|
|
|
|
|
//
|
|
|
|
|
//// <20><>ӡ<EFBFBD><D3A1><EFBFBD><EFBFBD>
|
|
|
|
|
//void printMatrix(int matrix[9][9]) {
|
|
|
|
|
// for (int i = 0; i < 9; i++) {
|
|
|
|
|
// if (i == 0 || i == 3 || i == 6) {
|
|
|
|
|
// printf("|-----------------------|\n");
|
|
|
|
|
// }
|
|
|
|
|
// for (int j = 0; j < 9; j++) {
|
|
|
|
|
// if (j == 0 || j == 3 || j == 6) {
|
|
|
|
|
// printf("| ");
|
|
|
|
|
// }
|
|
|
|
|
// if (matrix[i][j] == 0) {
|
|
|
|
|
// printf(". ");
|
|
|
|
|
// }
|
|
|
|
|
// else {
|
|
|
|
|
// printf("%d ", matrix[i][j]);
|
|
|
|
|
// }
|
|
|
|
|
// if (j == 8) {
|
|
|
|
|
// printf("|\n");
|
|
|
|
|
// }
|
|
|
|
|
// }
|
|
|
|
|
// if (i == 8) {
|
|
|
|
|
// printf("|-----------------------|\n");
|
|
|
|
|
// }
|
|
|
|
|
// }
|
|
|
|
|
//}
|
|
|
|
|
//
|
|
|
|
|
//// <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ƿ<EFBFBD><C7B7><EFBFBD>ָ<EFBFBD><D6B8><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
|
|
|
|
//int isInRow(int matrix[9][9], int row, int num) {
|
|
|
|
|
// for (int i = 0; i < 9; i++) {
|
|
|
|
|
// if (matrix[row][i] == num) {
|
|
|
|
|
// return 1;
|
|
|
|
|
// }
|
|
|
|
|
// }
|
|
|
|
|
// return 0;
|
|
|
|
|
//}
|
|
|
|
|
//
|
|
|
|
|
//// <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ƿ<EFBFBD><C7B7><EFBFBD>ָ<EFBFBD><D6B8><EFBFBD><EFBFBD> 3x3 <20>Ӿ<EFBFBD><D3BE><EFBFBD><EFBFBD><EFBFBD>
|
|
|
|
|
//int isInGroup(int matrix[9][9], int startRow, int startCol, int num) {
|
|
|
|
|
// for (int i = 0; i < 3; i++) {
|
|
|
|
|
// for (int j = 0; j < 3; j++) {
|
|
|
|
|
// if (matrix[startRow + i][startCol + j] == num) {
|
|
|
|
|
// return 1;
|
|
|
|
|
// }
|
|
|
|
|
// }
|
|
|
|
|
// }
|
|
|
|
|
// return 0;
|
|
|
|
|
//}
|
|
|
|
|
//
|
|
|
|
|
//// <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD> 1 - 9 <20>Ƿ<EFBFBD><C7B7><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>һ<EFBFBD><D2BB>
|
|
|
|
|
//bool checkRows(int matrix[9][9]) {
|
|
|
|
|
// for (int i = 0; i < 9; i++) {
|
|
|
|
|
// int count[9] = { 0 };
|
|
|
|
|
// for (int j = 0; j < 9; j++) {
|
|
|
|
|
// if (matrix[i][j] > 0 && matrix[i][j] <= 9) {
|
|
|
|
|
// int num = matrix[i][j] - 1;
|
|
|
|
|
// if (count[num] > 0) {
|
|
|
|
|
// return false;
|
|
|
|
|
// }
|
|
|
|
|
// count[num]++;
|
|
|
|
|
// }
|
|
|
|
|
// }
|
|
|
|
|
// }
|
|
|
|
|
// return true;
|
|
|
|
|
//}
|
|
|
|
|
//
|
|
|
|
|
//// <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD> 1 - 9 <20>Ƿ<EFBFBD><C7B7><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>һ<EFBFBD><D2BB>
|
|
|
|
|
//bool checkColumns(int matrix[9][9]) {
|
|
|
|
|
// for (int j = 0; j < 9; j++) {
|
|
|
|
|
// int count[9] = { 0 };
|
|
|
|
|
// for (int i = 0; i < 9; i++) {
|
|
|
|
|
// if (matrix[i][j] > 0 && matrix[i][j] <= 9) {
|
|
|
|
|
// int num = matrix[i][j] - 1;
|
|
|
|
|
// if (count[num] > 0) {
|
|
|
|
|
// printf("The number %d in the col %d has been used!\n", matrix[i][j], j);
|
|
|
|
|
// return false;
|
|
|
|
|
// }
|
|
|
|
|
// count[num]++;
|
|
|
|
|
// }
|
|
|
|
|
// }
|
|
|
|
|
// }
|
|
|
|
|
// return true;
|
|
|
|
|
//}
|
|
|
|
|
//
|
|
|
|
|
//// <20><><EFBFBD><EFBFBD> 3x3 <20>Ӿ<EFBFBD><D3BE><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD> 1 - 9 <20>Ƿ<EFBFBD><C7B7><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>һ<EFBFBD><D2BB>
|
|
|
|
|
//bool checkSubMatrices(int matrix[9][9]) {
|
|
|
|
|
// for (int row = 0; row < 9; row += 3) {
|
|
|
|
|
// for (int col = 0; col < 9; col += 3) {
|
|
|
|
|
// int count[9] = { 0 };
|
|
|
|
|
// for (int i = row; i < row + 3; i++) {
|
|
|
|
|
// for (int j = col; j < col + 3; j++) {
|
|
|
|
|
// if (matrix[i][j] > 0 && matrix[i][j] <= 9) {
|
|
|
|
|
// int num = matrix[i][j] - 1;
|
|
|
|
|
// if (count[num] > 0) {
|
|
|
|
|
// printf("The number %d in the block %d has been used!\n", matrix[i][j], (row / 3) * 3 + col / 3);
|
|
|
|
|
// return false;
|
|
|
|
|
// }
|
|
|
|
|
// count[num]++;
|
|
|
|
|
// }
|
|
|
|
|
// }
|
|
|
|
|
// }
|
|
|
|
|
// }
|
|
|
|
|
// }
|
|
|
|
|
// return true;
|
|
|
|
|
//}
|
|
|
|
|
//
|
|
|
|
|
//// <20>жϾ<D0B6><CFBE><EFBFBD><EFBFBD>Ƿ<EFBFBD><C7B7><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ķ<EFBFBD><C4B6><EFBFBD>
|
|
|
|
|
//bool isSudokuMatrix(int matrix[9][9]) {
|
|
|
|
|
// return checkRows(matrix) && checkColumns(matrix) && checkSubMatrices(matrix);
|
|
|
|
|
//}
|
|
|
|
|
//
|
|
|
|
|
//int main() {
|
|
|
|
|
// printf("The original Sudoku matrix: \n");
|
|
|
|
|
// srand(time(NULL));
|
|
|
|
|
// int matrix[9][9] = { 0 };
|
|
|
|
|
// int numbers[9] = { 1, 2, 3, 4, 5, 6, 7, 8, 9 };
|
|
|
|
|
//
|
|
|
|
|
// for (int group = 0; group < 3; group++) {
|
|
|
|
|
// int usedNumbers[9] = { 0 };
|
|
|
|
|
// for (int i = 0; i < 9; i++) {
|
|
|
|
|
// usedNumbers[i] = numbers[i];
|
|
|
|
|
// }
|
|
|
|
|
//
|
|
|
|
|
// for (int i = 0; i < 9; i++) {
|
|
|
|
|
// int idx = rand() % (9 - i);
|
|
|
|
|
// int temp = usedNumbers[idx];
|
|
|
|
|
// usedNumbers[idx] = usedNumbers[8 - i];
|
|
|
|
|
// usedNumbers[8 - i] = temp;
|
|
|
|
|
// }
|
|
|
|
|
//
|
|
|
|
|
// for (int row = 0; row < 3; row++) {
|
|
|
|
|
// int count = 0;
|
|
|
|
|
// while (count < 3) {
|
|
|
|
|
// int num;
|
|
|
|
|
// do {
|
|
|
|
|
// int idx = rand() % 9;
|
|
|
|
|
// num = usedNumbers[idx];
|
|
|
|
|
// } while (num == 0 || isInRow(matrix, group * 3 + row, num) || isInGroup(matrix, group * 3, 0, num));
|
|
|
|
|
//
|
|
|
|
|
// int pos;
|
|
|
|
|
// do {
|
|
|
|
|
// pos = rand() % 9;
|
|
|
|
|
// } while (matrix[group * 3 + row][pos] != 0);
|
|
|
|
|
//
|
|
|
|
|
// matrix[group * 3 + row][pos] = num;
|
|
|
|
|
// count++;
|
|
|
|
|
// }
|
|
|
|
|
// }
|
|
|
|
|
// }
|
|
|
|
|
//
|
|
|
|
|
// for (int row = 0; row < 9; row++) {
|
|
|
|
|
// int uniqueNumbers[9] = { 0 };
|
|
|
|
|
// int uniqueCount = 0;
|
|
|
|
|
// for (int i = 0; i < 9; i++) {
|
|
|
|
|
// if (matrix[row][i] != 0 && !uniqueNumbers[matrix[row][i] - 1]) {
|
|
|
|
|
// uniqueNumbers[matrix[row][i] - 1] = 1;
|
|
|
|
|
// uniqueCount++;
|
|
|
|
|
// }
|
|
|
|
|
// }
|
|
|
|
|
// if (uniqueCount > 3) {
|
|
|
|
|
// while (uniqueCount > 3) {
|
|
|
|
|
// int idx = rand() % 9;
|
|
|
|
|
// if (matrix[row][idx] != 0 && uniqueNumbers[matrix[row][idx] - 1]) {
|
|
|
|
|
// matrix[row][idx] = 0;
|
|
|
|
|
// uniqueCount--;
|
|
|
|
|
// }
|
|
|
|
|
// }
|
|
|
|
|
// }
|
|
|
|
|
// else if (uniqueCount < 3) {
|
|
|
|
|
// while (uniqueCount < 3) {
|
|
|
|
|
// int num;
|
|
|
|
|
// do {
|
|
|
|
|
// num = rand() % 9 + 1;
|
|
|
|
|
// } while (uniqueNumbers[num - 1]);
|
|
|
|
|
// int idx;
|
|
|
|
|
// do {
|
|
|
|
|
// idx = rand() % 9;
|
|
|
|
|
// } while (matrix[row][idx] != 0);
|
|
|
|
|
// matrix[row][idx] = num;
|
|
|
|
|
// uniqueNumbers[num - 1] = 1;
|
|
|
|
|
// uniqueCount++;
|
|
|
|
|
// }
|
|
|
|
|
// }
|
|
|
|
|
// }
|
|
|
|
|
//
|
|
|
|
|
// printMatrix(matrix);
|
|
|
|
|
// bool result = isSudokuMatrix(matrix);
|
|
|
|
|
// if (result) {
|
|
|
|
|
// printf("True:Valid initial Sudoku matrix!\n");
|
|
|
|
|
// }
|
|
|
|
|
// else {
|
|
|
|
|
// printf("False:Invalid initial Sudoku matrix!\n");
|
|
|
|
|
// }
|
|
|
|
|
// return 0;
|
|
|
|
|
//}
|