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.

184 lines
4.5 KiB

#include<stdio.h>
#include<string.h>
#include <stdlib.h>
#include <time.h>
#define HANG 9
#define LIE 9
void printing(int matrix[HANG][LIE]) {
for (int i = 0; i < HANG; ++i) {
if (i % 3 == 0 && i != 0) {
printf("----------------------|\n");
}
for (int j = 0; j < LIE; ++j) {
if (j % 3 == 0 && j != 0) {
printf("| ");
}
if (matrix[i][j] == '0') {
printf("0 ");
}
else {
printf("%c ", matrix[i][j]);
}
if (j == 8) {
printf("|\n");
}
}
}
printf("----------------------|\n");
}
int is_valid(int board[9][9], int row, int col, int num) {// <20><><EFBFBD><EFBFBD><EFBFBD>У<EFBFBD>
for (int i = 0; i < 9; i++) {
if (board[row][i] == num) {
return 0;
}
}
for (int i = 0; i < 9; i++) {//<2F><><EFBFBD><EFBFBD><EFBFBD><EFBFBD> <20><>
if (board[i][col] == num) {
return 0;
}
}
int startr = row - row % 3;
int startc = col - col % 3;
for (int i = 0; i < 3; i++) {
for (int j = 0; j < 3; j++) {
if (board[startr + i][startc + j] == num) {
return 0;
}
}
}
return 1;
}//<2F><><EFBFBD><EFBFBD> <20><>
int solve_sudoku(int board[9][9]) {
for (int row = 0; row < 9; row++) {
for (int col = 0; col < 9; col++) {
if (board[row][col] == 0) {
for (int num = 1; num <= 9; num++) {
if (is_valid(board, row, col, num)) {
board[row][col] = num;
if (solve_sudoku(board)) {
return 1;
}
board[row][col] = 0;
}
}
return 0;
}
}
}
return 1;
}
//<2F><><EFBFBD><EFBFBD>
int main(){
srand(time(NULL)); // <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
int board[HANG][LIE];
for (int i = 0; i < 9; ++i) {
for (int j = 0; j < 9; ++j) {
board[i][j] = '0';
}
}
for (int i = 0; i < 9; ++i)
{
int A= 0, B=0;
int num[3];
int posi[3];
while (B< 3)
{
int number = rand() % 9 + 1;
if (B < 3 && !( num[2] == number|| num[1] == number ||num[0] == number ))
{
num[B++] = number;
}
}
while (A < 3)
{
int place = rand() % LIE;
if ( (A < 3)&&board[i][place] == '0')
{
posi[A++] = place;
}
}
for (int k = 0; k < 3; ++k)
{
board[i][posi[k]] = num[k] + '0';
}
}printing(board); // <20><>ӡ<EFBFBD><D3A1><EFBFBD><EFBFBD>
int a=0,b=0,c=0,d=1;
for(int i=0;i<9;i++){
for(int m=1;m<=9;m++){
for(int j=0;j<9;j++){
if(board[i][j]==m){
a=a+1;
if(a>1){
printf("False:Invalid initial Sudoku matrix!\n");
printf("The number %d in the row %d has been used!\n",m,i+1);
return 0;
}
}
}a=0;
}
}
//<2F>е<EFBFBD><D0B5>ж<EFBFBD> <20><>
for(int start=0;start<=6;){
for(int end=0;end<=6;){
for(int m=1;m<9;m++){
for(int i=start;i<3;i++){
for(int j=end;j<3;j++){
if(board[i][j]==m){
c=c+1;}
if(c>1){
printf("False:Invalid initial Sudoku matrix!\n");
printf("The number %d in the block %d has been used!\n",m,d);
return 0;
}
}
}
c=0;
}
end=end+3;
d=d+1;
}
start=start+3;
}
//<2F><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>жϣ<D0B6>
for(int i=0;i<9;i++){
for(int m=1;m<9;m++){
for(int j=0;j<9;j++){
if(board[j][i]==m){
b=b+1;}
if(b>1){
printf("False:Invalid initial Sudoku matrix!\n");
printf("The number %d in the col %d has been used!\n",m,i+1);
return 0;
}
}b=0;
}b=0;
}
printf("True:Valid initial Sudoku matrix!\n");
//<2F><><EFBFBD>Ƕ<EFBFBD><C7B6><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>жϣ<D0B6><CFA3>Ƿ<EFBFBD>Ϊ<EFBFBD><CEAA><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
if (solve_sudoku(board)) {
printf("The solution of Sudoku matrix:\n");
printf("|-----------------------|\n");
for(int i=0;i<9;i++){
printf("| ");
for(int j=0;j<9;j++){
printf("%d ",board[i][j]);
if((j+1)%3==0){
printf("| ");
}
}
printf("\n");
if((i+1)%3==0) {
printf("|-----------------------|\n");
}
//<2F><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
}
}else {
printf("No solution!\n");}
return 0;
}