|
|
|
@ -1,2 +1,453 @@
|
|
|
|
|
# SDJZXJTU
|
|
|
|
|
first step:
|
|
|
|
|
#include <stdio.h>
|
|
|
|
|
#include <time.h>
|
|
|
|
|
#include <stdlib.h>
|
|
|
|
|
int main()
|
|
|
|
|
{
|
|
|
|
|
int i,j;
|
|
|
|
|
char a[9][9]={ {'5','3','4','6','7','8','9','1','2'},
|
|
|
|
|
{'6','7','2','1','9','5','3','4','8'},
|
|
|
|
|
{'1','9','8','3','4','2','5','6','7'},
|
|
|
|
|
{'8','5','9','7','6','1','4','2','3'},
|
|
|
|
|
{'4','2','6','8','5','3','7','9','1'},
|
|
|
|
|
{'7','1','3','9','2','4','8','5','6'},
|
|
|
|
|
{'9','6','1','5','3','7','2','8','4'},
|
|
|
|
|
{'2','8','7','4','1','9','6','3','5'},
|
|
|
|
|
{'3','4','5','2','8','6','1','7','9'}};
|
|
|
|
|
for(i=0;i<9;i++)
|
|
|
|
|
{
|
|
|
|
|
for(j=0;j<9;j++)
|
|
|
|
|
{
|
|
|
|
|
printf("%c",a[i][j]);
|
|
|
|
|
}
|
|
|
|
|
printf("\n") ;
|
|
|
|
|
}
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
second step:
|
|
|
|
|
#include <stdio.h>
|
|
|
|
|
#include <time.h>
|
|
|
|
|
#include <stdlib.h>
|
|
|
|
|
int main()
|
|
|
|
|
{
|
|
|
|
|
srand(time(NULL));
|
|
|
|
|
int a[9][9]={0};
|
|
|
|
|
int b[3][3]={0};
|
|
|
|
|
int c[9]={0};
|
|
|
|
|
int d[9]={0};
|
|
|
|
|
int e[9]={0};
|
|
|
|
|
int i=0;
|
|
|
|
|
for(i=0;i<9;i++)
|
|
|
|
|
{
|
|
|
|
|
c[i]=i+1;
|
|
|
|
|
}
|
|
|
|
|
for(i=0;i<9;i++)
|
|
|
|
|
{
|
|
|
|
|
int j=rand()%9;
|
|
|
|
|
int temp=c[i];
|
|
|
|
|
c[i]=c[j];
|
|
|
|
|
c[j]=temp;
|
|
|
|
|
}
|
|
|
|
|
for(i=0;i<9;i++)
|
|
|
|
|
{
|
|
|
|
|
d[i]=i+1;
|
|
|
|
|
}
|
|
|
|
|
for(i=0;i<9;i++)
|
|
|
|
|
{
|
|
|
|
|
int j=rand()%9;
|
|
|
|
|
int temp=d[i];
|
|
|
|
|
d[i]=d[j];
|
|
|
|
|
d[j]=temp;
|
|
|
|
|
}
|
|
|
|
|
for(i=0;i<9;i++)
|
|
|
|
|
{
|
|
|
|
|
e[i]=i+1;
|
|
|
|
|
}
|
|
|
|
|
for(i=0;i<9;i++)
|
|
|
|
|
{
|
|
|
|
|
int j=rand()%9;
|
|
|
|
|
int temp=d[i];
|
|
|
|
|
e[i]=e[j];
|
|
|
|
|
e[j]=temp;
|
|
|
|
|
}/*<2A><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>*/
|
|
|
|
|
/*for(i=0;i<9;i++)
|
|
|
|
|
{
|
|
|
|
|
printf("%d",c[i]);
|
|
|
|
|
}
|
|
|
|
|
printf("\n");
|
|
|
|
|
for(i=0;i<9;i++)
|
|
|
|
|
{
|
|
|
|
|
printf("%d",d[i]);
|
|
|
|
|
}
|
|
|
|
|
printf("\n");
|
|
|
|
|
for(i=0;i<9;i++)
|
|
|
|
|
{
|
|
|
|
|
printf("%d",e[i]);
|
|
|
|
|
}<7D><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>*/
|
|
|
|
|
int p=0;
|
|
|
|
|
for(i=0;i<3;i++)
|
|
|
|
|
{
|
|
|
|
|
int j;
|
|
|
|
|
for(j=0;j<3;j++)
|
|
|
|
|
{
|
|
|
|
|
a[i][c[p]-1]=d[p];
|
|
|
|
|
p++;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
p=0;
|
|
|
|
|
for(i=3;i<6;i++)
|
|
|
|
|
{
|
|
|
|
|
int j;
|
|
|
|
|
for(j=0;j<3;j++)
|
|
|
|
|
{
|
|
|
|
|
a[i][d[p]-1]=e[p];
|
|
|
|
|
p++;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
p=0;
|
|
|
|
|
for(i=6;i<9;i++)
|
|
|
|
|
{
|
|
|
|
|
int j;
|
|
|
|
|
for(j=0;j<3;j++)
|
|
|
|
|
{
|
|
|
|
|
a[i][e[p]-1]=c[p];
|
|
|
|
|
p++;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
for(i=0;i<9;i++)
|
|
|
|
|
{
|
|
|
|
|
int j;
|
|
|
|
|
for(j=0;j<9;j++)
|
|
|
|
|
{
|
|
|
|
|
printf("%d",a[i][j]);
|
|
|
|
|
}
|
|
|
|
|
printf("\n");
|
|
|
|
|
}
|
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
third step:
|
|
|
|
|
#include <stdio.h>
|
|
|
|
|
#include <stdlib.h>
|
|
|
|
|
#include <time.h>
|
|
|
|
|
void shudu(int a[9][9]) {
|
|
|
|
|
int flag = 0;
|
|
|
|
|
int z = 0;
|
|
|
|
|
int i, j, k, l;
|
|
|
|
|
int count[10] = {0};
|
|
|
|
|
int m[9];
|
|
|
|
|
for (i = 0; i < 9; i++) {
|
|
|
|
|
int count[10] = {0};
|
|
|
|
|
for (j = 0; j < 9; j++) {
|
|
|
|
|
if (a[i][j] != 0)
|
|
|
|
|
count[a[i][j]]++;
|
|
|
|
|
}
|
|
|
|
|
for (j = 0; j < 9; j++) {
|
|
|
|
|
if (count[j + 1] >= 2) {
|
|
|
|
|
flag = 1;
|
|
|
|
|
z = j + 1;
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
if (flag == 1)
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
if (flag == 1) {
|
|
|
|
|
printf("False:Invalid initialSudoku matrix\nThe number %d in the row %d has been used!,z,j");
|
|
|
|
|
return ;
|
|
|
|
|
}
|
|
|
|
|
for (i = 0; i < 9; i++) {
|
|
|
|
|
int count[10] = {0};
|
|
|
|
|
for (j = 0; j < 9; j++) {
|
|
|
|
|
if (a[j][i] != 0)
|
|
|
|
|
count[a[j][i]]++;
|
|
|
|
|
}
|
|
|
|
|
for (j = 0; j < 9; j++) {
|
|
|
|
|
if (count[j + 1] >= 2) {
|
|
|
|
|
flag = 1;
|
|
|
|
|
z = j + 1;
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
if (flag == 1)
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
if (flag == 1) {
|
|
|
|
|
printf("FalseInvalid initial Sudoku matrix\nThe number %d in the col %d has been used!", z, j);
|
|
|
|
|
return ;
|
|
|
|
|
}
|
|
|
|
|
for (i = 0; i < 3; i++) {
|
|
|
|
|
for (j = 0; j < 3; j++) {
|
|
|
|
|
for (k = 0; k < 3; k++) {
|
|
|
|
|
for (l = 0; l < 3; l++) {
|
|
|
|
|
m[k * 3 + l] = a[j * 3 + k][i * 3 + l];
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
for (k = 0; k < 9; k++)
|
|
|
|
|
count[k] = 0;
|
|
|
|
|
for (k = 0; k < 9; k++) {
|
|
|
|
|
if (m[k] != 0)
|
|
|
|
|
count[m[k]]++;
|
|
|
|
|
}
|
|
|
|
|
for (k = 0; k < 9; k++) {
|
|
|
|
|
if (count[k] >= 2) {
|
|
|
|
|
flag = 1;
|
|
|
|
|
z = k;
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
if (flag == 1)
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
if (flag == 1)
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
if (flag == 1) {
|
|
|
|
|
printf("False:Invalid initial Sudoku matrix!\nThe number %d in the block %d has been used!", z, j);
|
|
|
|
|
return ;
|
|
|
|
|
} else
|
|
|
|
|
printf("Ture:Valid initial sudoku matrix");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
int main() {
|
|
|
|
|
int i,j;
|
|
|
|
|
int board0[9][9] = {{5, 3, 0, 0, 7, 0, 0, 0, 0},
|
|
|
|
|
{6, 0, 0, 1, 9, 5, 0, 0, 0},
|
|
|
|
|
{0, 9, 8, 0, 0, 0, 0, 6, 0},
|
|
|
|
|
{8, 0, 0, 0, 6, 0, 0, 0, 3},
|
|
|
|
|
{4, 0, 0, 8, 0, 3, 0, 0, 1},
|
|
|
|
|
{7, 0, 0, 0, 2, 0, 0, 0, 6},
|
|
|
|
|
{0, 6, 0, 0, 0, 0, 2, 8, 0},
|
|
|
|
|
{0, 0, 0, 4, 1, 9, 0, 0, 5},
|
|
|
|
|
{0, 0, 0, 0, 8, 0, 0, 7, 9}
|
|
|
|
|
};
|
|
|
|
|
printf("The original Sudoku matrix:\n");
|
|
|
|
|
for (i = 0; i < 9; i++) {
|
|
|
|
|
for (j = 0; j < 9; j++) {
|
|
|
|
|
printf("%d ", board0[i][j]);
|
|
|
|
|
}
|
|
|
|
|
printf("\n");
|
|
|
|
|
}
|
|
|
|
|
printf("\n");
|
|
|
|
|
shudu(board0);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
fourth step:
|
|
|
|
|
|
|
|
|
|
#include <stdio.h>
|
|
|
|
|
#include <stdlib.h>
|
|
|
|
|
#define N 3
|
|
|
|
|
#define M 9
|
|
|
|
|
#include<stdio.h>
|
|
|
|
|
int x[M][M]={{5, 3, 0, 0, 7, 0, 0, 0, 0},
|
|
|
|
|
{6, 0, 0, 1, 9, 5, 0, 0, 0},
|
|
|
|
|
{0, 9, 8, 0, 0, 0, 0, 6, 0},
|
|
|
|
|
{8, 0, 0, 0, 6, 0, 0, 0, 3},
|
|
|
|
|
{4, 0, 0, 8, 0, 3, 0, 0, 1},
|
|
|
|
|
{7, 0, 0, 0, 2, 0, 0, 0, 6},
|
|
|
|
|
{0, 6, 0, 0, 0, 0, 2, 8, 0},
|
|
|
|
|
{0, 0, 0, 4, 1, 9, 0, 0, 5},
|
|
|
|
|
{0, 0, 0, 0, 8, 0, 0, 7, 9}};
|
|
|
|
|
void Solve(int r, int c);
|
|
|
|
|
int checkedbox(int r);
|
|
|
|
|
int checked(int r, int c, int num);
|
|
|
|
|
int* getData(int r, int c,int *data);
|
|
|
|
|
void show(void);
|
|
|
|
|
void Solve(int r, int c)
|
|
|
|
|
{
|
|
|
|
|
int i;
|
|
|
|
|
if (r == M) {
|
|
|
|
|
show();
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
if (c == 0 && (r == N || r == N*2)) {
|
|
|
|
|
if (checkedbox(r) == 0) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
if (c == M) {
|
|
|
|
|
Solve(r+1, 0);
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
if (x[r][c] == 0) {
|
|
|
|
|
for (i = 1; i <= M; i++) {
|
|
|
|
|
if (checked(r,c,i) == 1) {
|
|
|
|
|
x[r][c]=i;
|
|
|
|
|
Solve(r, c+1);
|
|
|
|
|
x[r][c]=0;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
else{
|
|
|
|
|
Solve(r, c+1);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
int checkedbox(int r) {
|
|
|
|
|
int i,j,c;
|
|
|
|
|
int *data = malloc(sizeof(int)*M);
|
|
|
|
|
for (c = 0; c < M; c+=N) {
|
|
|
|
|
data = getData(r,c,data);
|
|
|
|
|
for(i = 0; i < M-1 ; i++) {
|
|
|
|
|
for(j = i+1 ; j < M ; j++) {
|
|
|
|
|
if(data[i] == data[j]) {
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
free(data);
|
|
|
|
|
return 1;
|
|
|
|
|
}
|
|
|
|
|
int* getData(int r, int c,int *data){
|
|
|
|
|
int i,j,k = 0;
|
|
|
|
|
for (i = r-N; i < r ; i++) {
|
|
|
|
|
for(j = c; j < c+N ; j++){
|
|
|
|
|
data[k++] = x[i][j];
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return data;
|
|
|
|
|
}
|
|
|
|
|
int checked(int r, int c, int num) {
|
|
|
|
|
int j = 0,flag = 1;
|
|
|
|
|
for (j = 0; j < M; j++) {
|
|
|
|
|
if (x[j][c]==num || x[r][j]==num) {
|
|
|
|
|
flag = 0;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return flag;
|
|
|
|
|
}
|
|
|
|
|
void show(void) {
|
|
|
|
|
int i,j;
|
|
|
|
|
for (i = 0; i < M; i++) {
|
|
|
|
|
for (j = 0; j < M; j++) {
|
|
|
|
|
printf("%d ",x[i][j]);
|
|
|
|
|
}
|
|
|
|
|
printf("\n");
|
|
|
|
|
}
|
|
|
|
|
printf("\n");
|
|
|
|
|
}
|
|
|
|
|
int main(void)
|
|
|
|
|
{
|
|
|
|
|
int a[9][9]={{5, 3, 0, 0, 7, 0, 0, 0, 0},
|
|
|
|
|
{6, 0, 0, 1, 9, 5, 0, 0, 0},
|
|
|
|
|
{0, 9, 8, 0, 0, 0, 0, 6, 0},
|
|
|
|
|
{8, 0, 0, 0, 6, 0, 0, 0, 3},
|
|
|
|
|
{4, 0, 0, 8, 0, 3, 0, 0, 1},
|
|
|
|
|
{7, 0, 0, 0, 2, 0, 0, 0, 6},
|
|
|
|
|
{0, 6, 0, 0, 0, 0, 2, 8, 0},
|
|
|
|
|
{0, 0, 0, 4, 1, 9, 0, 0, 5},
|
|
|
|
|
{0, 0, 0, 0, 8, 0, 0, 7, 9}};
|
|
|
|
|
int i,j;
|
|
|
|
|
printf("The original Sudoku matrix: \n");
|
|
|
|
|
for(i=0;i<9;i++)
|
|
|
|
|
{
|
|
|
|
|
for(j=0;j<9;j++)
|
|
|
|
|
{
|
|
|
|
|
printf("%d ",a[i][j]);
|
|
|
|
|
}
|
|
|
|
|
printf("\n");
|
|
|
|
|
}
|
|
|
|
|
int det=0;/*0=<3D>ԡ<EFBFBD>1=<3D><><EFBFBD><EFBFBD>*/
|
|
|
|
|
for(i=0;i<9;i++)
|
|
|
|
|
{
|
|
|
|
|
int d1[9]={1,2,3,4,5,6,7,8,9};
|
|
|
|
|
int d2[9]={0};
|
|
|
|
|
for(j=0;j<9;j++)
|
|
|
|
|
{
|
|
|
|
|
int t=1;
|
|
|
|
|
for(t=1;t<=9;t++)
|
|
|
|
|
{
|
|
|
|
|
if(a[i][j]==t) d2[t-1]++;
|
|
|
|
|
}
|
|
|
|
|
for(t=0;t<9;t++)
|
|
|
|
|
{
|
|
|
|
|
if(d2[t]>=2)
|
|
|
|
|
{
|
|
|
|
|
det=1;
|
|
|
|
|
printf("False:Invalid initial Sudoku matrix!\n");
|
|
|
|
|
printf("The number %d in the row %d has been used!\n",t+1,i+1);
|
|
|
|
|
printf("No solution!");
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
if(det==1) break;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
if(det==0)
|
|
|
|
|
{
|
|
|
|
|
for(i=0;i<9;i++)
|
|
|
|
|
{
|
|
|
|
|
int d1[9]={1,2,3,4,5,6,7,8,9};
|
|
|
|
|
int d2[9]={0};
|
|
|
|
|
for(j=0;j<9;j++)
|
|
|
|
|
{
|
|
|
|
|
int t=1;
|
|
|
|
|
for(t=1;t<=9;t++)
|
|
|
|
|
{
|
|
|
|
|
if(a[j][i]==t) d2[t-1]++;
|
|
|
|
|
}
|
|
|
|
|
for(t=0;t<9;t++)
|
|
|
|
|
{
|
|
|
|
|
if(d2[t]>=2)
|
|
|
|
|
{
|
|
|
|
|
det=1;
|
|
|
|
|
printf("False:Invalid initial Sudoku matrix!\n");
|
|
|
|
|
printf("The number %d in the col %d has been used!\n",t+1,i+1);
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
if(det==1) break;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
if(det==0)
|
|
|
|
|
{
|
|
|
|
|
int number = 0;
|
|
|
|
|
int row=0 ;
|
|
|
|
|
for(i=0;i<9;i=i+3)
|
|
|
|
|
{
|
|
|
|
|
for(row=0;row<9;row=row+3)
|
|
|
|
|
{
|
|
|
|
|
int d1[9]={1,2,3,4,5,6,7,8,9};
|
|
|
|
|
int d2[9]={0};
|
|
|
|
|
for(j=i;j<i+3;j++)
|
|
|
|
|
{
|
|
|
|
|
number++;
|
|
|
|
|
int q;
|
|
|
|
|
for(q=row;q<row+3;q++)
|
|
|
|
|
{
|
|
|
|
|
int t=1;
|
|
|
|
|
for(t=1;t<=9;t++)
|
|
|
|
|
{
|
|
|
|
|
if(a[j][q]==t) d2[t-1]++;
|
|
|
|
|
}
|
|
|
|
|
for(t=0;t<9;t++)
|
|
|
|
|
{
|
|
|
|
|
if(d2[t]>=2)
|
|
|
|
|
{
|
|
|
|
|
det=1;
|
|
|
|
|
printf("False:Invalid initial Sudoku matrix!\n");
|
|
|
|
|
printf("The number %d in the block %d has been used!\n",t+1,number);
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
if(det==1) break;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
if(det==0)
|
|
|
|
|
{printf("True:Valid initial Sudoku matrix!\n");
|
|
|
|
|
printf("The solution of Sudoku matrix:\n");}
|
|
|
|
|
Solve(0,0);
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|