-
Notifications
You must be signed in to change notification settings - Fork 0
/
Sudoku.java
337 lines (295 loc) · 7.33 KB
/
Sudoku.java
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
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
/***************************
* @author UTKARSH KUMAR
* ID: 2017B2A71008P
***************************/
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;
public class Sudoku {
public static void main(String[] args) throws FileNotFoundException
{
int[][] GRID = { { 0,1,4,0,0,0,0,0,5 }, { 0,0,0,0,0,4,0,0,8}, {9,0,0,5,2,0,0,1,4},
{ 0,0,2,6,9,0,5,0,0}, {0,0,0,0,0,0,0,0,0 }, {0,0,6,0,8,5,2,0,0 },
{ 8,9,0,0,7,3,0,0,2 }, {2,0,0,9,0,0,0,0,0}, { 5,0,0,0,0,0,8,7,0 } };
Scanner in = new Scanner(System.in);
System.out.println("Choose of the following options:\n1. Solve default GRID\n2. Provide a text file");
System.out.print("Your Choice (1/2): ");
int choice = in.nextInt();
if(choice == 2)
{
System.out.print("\nFile Name (for example: sudoku.txt): ");
String filename = in.next();
FileReader fr = new FileReader(filename);
Scanner inFile = new Scanner(fr);
for(int i = 0; i<9; i++)
{
for(int j = 0; j<9; j++)
{
GRID[i][j] = inFile.nextInt();
}
}
inFile.close();
}
in.close();
new Solve(GRID);
}
}
class Pos
{
int ln;
int col;
Pos(int ln, int col)
{
this.ln=ln;
this.col = col;
}
}
class Solve {
private int[][] GRID = new int[9][9];
int[][][] Domain = new int[9][9][10];
int assignmentNumber = 0;
long BacktrackCount = 0;
public void InitializeDomain()
{
for(int i = 0; i< 9; i++)
for(int j = 0; j< 9; j++)
Domain[i][j][0] = 27;
for(int i = 0; i< 9; i++)
{
for(int j = 0; j<9; j++)
{
if(GRID[i][j] != 0)
{
Pos p = new Pos(i, j);
int val = GRID[i][j];
markPlace(p, val, -1);
}
}
}
}
Solve(int[][] GRID)
{
this.GRID = GRID;
InitializeDomain();
System.out.println("Given GRID:");
for(int i = 0; i< 9; i++)
{
for(int j = 0; j<9; j++)
{
System.out.print(GRID[i][j]+" ");
}
System.out.println();
}
long startTime = System.currentTimeMillis();
boolean done = backtrack();
long totalTime = System.currentTimeMillis() - startTime;
if(done)
{System.out.println("FINAL GRID:");
for(int i = 0; i< 9; i++)
{
for(int j = 0; j<9; j++)
{
System.out.print(GRID[i][j]+" ");
}
System.out.println();
}}
else
System.out.println("No Solution");
System.out.println("Performance Stats:\nTime Taken: "+ totalTime+ " ms\nBacktracks: "+BacktrackCount);
System.exit(1);
}
private void markPlace(Pos p, int val, int mark)
{
//mark row peers
int regR = p.ln-p.ln%3;
int regC = p.col-p.col%3;
for(int i=0; i<9; i++)
{
if(p.col - i != 0 )
{
Domain[p.ln][i][val] += mark;
Domain[p.ln][i][0] += mark;
}
if(p.ln - i != 0 )
{
Domain[i][p.col][val] += mark;
Domain[i][p.col][0] += mark;
}
if((regR+i/3)-p.ln != 0 && (regC+i%3)-p.col != 0 )
{
Domain[regR+i/3][regC+i%3][val] += mark;
Domain[regR+i/3][regC+i%3][0] += mark;
}
}
}
private boolean ForwardChecking(Pos p, int val)
{
if (Domain[p.ln][p.col][val] == 0)
{
//GRID[p.ln][p.col] = val;
markPlace(p, val, -1);
if (isSolutionPossible(p, val))
{
GRID[p.ln][p.col] = val;
// System.out.println("assigned "+val);
return true;
}
}
return false;
}
private boolean isSolutionPossible(Pos p, int val)
{
int regR = p.ln-p.ln%3;
int regC = p.col-p.col%3;
//boolean possible = true;
for(int i=0; i<9; i++)
{
if(Domain[p.ln][i][0] == 0 || Domain[i][p.col][0] == 0 || Domain[regR+i/3][regC+i%3][0] == 0)
{
markPlace(p, val, 1);
return false;
}
if((p.col - i != 0 && Domain[p.ln][i][0] == 1 && Domain[p.ln][i][val] == 0) ||
(p.ln - i != 0 && Domain[i][p.col][i] == 1 && Domain[i][p.col][val] == 0) ||
(((regR+i/3)-p.ln != 0 && (regC+i%3)-p.col != 0) && Domain[regR+i/3][regC+i%3][0] == 1 &&
Domain[regR+i/3][regC+i%3][val] == 0 ))
{
markPlace(p, val, 1);
return false;
}
}
return true;
}
private boolean backtrack()
{
Pos p = SelectUnassignedVariable();
if(p == null)
{
return true;
}
int[] domainRetain = new int[10];
for(int i = 0; i< 10; i++)
{
domainRetain[i] = Domain[p.ln][p.col][i];
}
while (domainRetain[0] > 0)
{
int val = SelectDomainValue(p, domainRetain);
if(val == 0)
return false;
boolean assigned = ForwardChecking(p, val);
if(assigned)
{
boolean done = backtrack();
if(done) return done;
else {
GRID[p.ln][p.col] = 0;
markPlace(p, val, 1);
}
}
BacktrackCount++;
domainRetain[val] += -1;
domainRetain[0] += -1;
}
return false;
}
private Pos SelectUnassignedVariable()
{
List<Pos> VariableList = MRVList();
Pos p = MaxDegreeV(VariableList);
return p;
}
List<Pos> MRVList()
{
int min = 27 + 1;
ArrayList<Pos> list = new ArrayList<Pos>();
for (int i = 0; i < 9; i++)
{
for (int j = 0; j < 9; j++)
{
if (((GRID[i][j] == 0)) && (Domain[i][j][0] == min))
{
list.add(new Pos(i, j));
continue;
}
if (((GRID[i][j] == 0)) && (Domain[i][j][0] < min))
{
list.clear();
min = Domain[i][j][0];
list.add(new Pos(i, j));
}
}
}
return list;
}
Pos MaxDegreeV(List<Pos> MRVS)
{
int deg = -1;
Pos p = null;
for (int i = 0; i < MRVS.size(); i++)
{
Pos t = MRVS.get(i);
int count = 0;
int regR = t.ln-t.ln%3;
int regC = t.col-t.col%3;
for(int j=0; j<9; j++)
{
if(t.col - j != 0 && GRID[t.ln][j] == 0)
{
count++;
}
if(t.ln - j != 0 && GRID[j][t.col] == 0)
{
count++;
}
if(((regR+j/3)-t.ln != 0 && (regC+j%3)-t.col != 0)&& GRID[regR+j/3][regC+j%3] == 0)
{
count++;
}
}
if (count > deg)
{
deg = count;
p = MRVS.get(i);
}
}
return p;
}
private int SelectDomainValue(Pos t, int[] domainRetain)
{
int val = 0;
int comp = 28;
for(int i = 1; i< 10; i++)
{
int count = 0;
int regR = t.ln-t.ln%3;
int regC = t.col-t.col%3;
if(domainRetain[i] == 0)
{
for(int j=0; j<9; j++)
{
if(t.col - j != 0 && Domain[t.ln][j][i] == 0)
{
count++;
}
if(t.ln - j != 0 && Domain[j][t.col][i] == 0)
{
count++;
}
if(((regR+j/3)-t.ln != 0 && (regC+j%3)-t.col != 0)&& Domain[regR+j/3][regC+j%3][i] == 0)
{
count++;
}
}
if(count<comp)
{
val = i;
comp = count;
}
}
}
return val;
}
}