-
Notifications
You must be signed in to change notification settings - Fork 1
/
generator.cpp
187 lines (183 loc) · 5.27 KB
/
generator.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
//This code is entire copyright of Pandey A. and Kumar S. 2018
//Dated 2nd October 2018
#include<iostream>
#include<cstdio>
#include<cstdlib>
#include<fstream>
#include<vector>
#include<cstring>
#include<ctime>
#include<thread>
#include<chrono>
using namespace std;
//Maps (i,j,k) to all the integers from 1 to 729
int val(int i, int j, int k){
return 81*i + 9*j + k + 1;
}
void invoke(){
FILE* fp;
fp = fopen("input.txt", "w");
for( int i=0 ; i<9 ; i++ ) fprintf(fp,". . . . . . . . .\n");
fclose(fp);
std::system("g++ -std=c++11 sudoku.cpp");
std::system("./a.out");
}
//isunique check if problem[][] has a unique solution[][] or not
int isunique(int problem[][9], int solution[][9], int len){
FILE *fp1;
FILE *fp2;
int values[9][9], nonzero = 0;
//Stores fixed elements in a vector
//First row for val(i,j,k)
//Second row for colour [1,2,3...9]->[0,1,2,3...,8]
vector<int> fixed[2];
fp2 = fopen( "sat.txt" , "w" );
for( int i=0 ; i<9 ; i++ ){
for(int j=0 ; j<9 ; j++ ){
values[i][j] = problem[i][j];
if ( values[i][j] != 0 ){
fixed[0].push_back( val(i, j, values[i][j]-1) );
fixed[1].push_back( values[i][j] - 1 );
nonzero++;
}
}
}
//Number of variables and clauses fed to minisat
int variables = 9*9*9 ;
int clauses = (9*9) + (9*9) + (9*9) + (36*81) + nonzero +(9*2)+ (len>0) ;
fprintf(fp2,"p cnf %d %d\n", variables, clauses);
//Checking for rows (9x9)
for( int i=0 ; i<9 ; i++ ){
for( int k=0; k<9 ; k++ ){
for( int j=0; j<9; j++){
fprintf(fp2,"%d ",val(i,j,k));
}
fprintf(fp2,"0\n");
}
}
//Checking for columns (9x9)
for( int j=0 ; j<9 ; j++ ){
for( int k=0; k<9 ; k++ ){
for( int i=0; i<9; i++){
fprintf(fp2,"%d ",val(i,j,k));
}
fprintf(fp2,"0\n");
}
}
//Checking for blocks (9x9)
for( int k=0 ; k<9 ; k++ ){
for( int block=0 ; block<9 ; block++ ){
int right = 3 * (block%3);
int down = (block>2)*3;
for( int i=right; i<right+3; i++){
for( int j=down; j<down+3; j++) fprintf(fp2,"%d ",val(i,j,k));
}
fprintf(fp2,"0\n");
}
}
//Checking if no two have same colour (81 x 9 choose 2 = 81x36 )
for(int i=0 ; i<9 ; i++){
for( int j=0 ; j<9 ; j++){
for(int k=0; k<9; k++){
for(int l=k+1; l<9; l++){
fprintf(fp2,"-%d -%d 0\n",val(i,j,k),val(i,j,l));
}
}
}
}
//Checking fixed values (nonzero)
for( int i=0 ; i<nonzero ; i++ ){
fprintf(fp2,"%d 0\n", fixed[0][i]);
}
//Checking main diagonals (9x2)
for( int k=0 ; k<9 ; k++ ){
for( int i=0 ; i<9 ; i++ ) fprintf(fp2,"%d ",val(i,i,k));
fprintf(fp2,"0\n");
}
for( int k=0 ; k<9 ; k++ ){
for( int i=0 ; i<9 ; i++ ) fprintf(fp2,"%d ",val(i,8-i,k));
fprintf(fp2,"0\n");
}
//Ensuring if solution is not solution[][]
for( int i=0 ; i<9 ; i++ ){
for( int j=0 ; j<9 ; j++ ){
if( problem[i][j]==0 ) fprintf(fp2,"-%d ",val(i,j,solution[i][j]-1));
}
}
if(len>0) fprintf(fp2,"0\n");
fclose(fp2);
fp1=fopen("ans.txt","r");
//Execute minisat
std::system("minisat sat.txt ans.txt > garbage.txt");
//Check if UNSAT
char s[10];
fscanf(fp1,"%s",s);
fclose(fp1);
if(s[0]=='U') return 1;
return 0;
}
int main(){
//Clock Start
clock_t start=clock();
//invokes sudoku.cpp to give a random sudoku in o.txt
invoke();
FILE* fpo;
fpo=fopen("o.txt","r");
int solution[9][9];
int problem[9][9];
int fix[9][9];
int temp,tempx,tempy,x,y,len=81;
//Takes input from "o.txt" i.e. a solved sudoku
for( int i=0 ; i<9 ;i++ ){
for( int j=0 ; j<9 ; j++ ){
fscanf(fpo,"%d",&solution[i][j]);
problem[i][j]=0;
}
}
//Seed value set to CPU time
srand(time(NULL));
//This loop invokes randomization to form a tentative solution
//in which the first n-1 elements do not give a unique solution
//but the addition of n-th element gives a unique solution
//NOTE: This set of clues may not be minimal. Call this S.
while(1){
do{
x=rand()%9;
y=rand()%9;
}while(problem[x][y]!=0);
problem[x][y]=solution[x][y];
len--;
if(isunique(problem,solution,len)==1){ break; }
}
//It is quite evident that if (n-1) do not give a unique solution
//then n-th element must belong to S', minimal set which is a subset of S
//This loop segregates two types of point, one which must be in S'
//the other which can be removed.
//For a proof to this algorithm refer to readme.txt
for( int i=0 ; i<9 ; i++ ){
for( int j=0 ; j<9 ; j++ ){
if( problem[i][j]!=0 ){
int temp=problem[i][j];
problem[i][j]=0;
len++;
if( isunique(problem,solution,len)==0 ){
len--;
problem[i][j]=temp;
}
}
}
}
//The minimal sudoku is delivered
printf("Minimal clue problem for solved sudoku in o.txt:\n");
for(int i=0;i<9;i++){
for(int j=0;j<9;j++){
if(problem[i][j]==0) printf(". ");
else printf("%d ",problem[i][j]);
}
printf("\n");
}
fclose(fpo);
//Clock over
printf("---------%f seconds--------\n",(double)(clock()-start)/CLOCKS_PER_SEC);
return 0;
}