-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathSudoku.java
267 lines (217 loc) · 6.37 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
package sudokuNorvig;
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.Hashtable;
import java.util.TreeMap;
import java.util.TreeSet;
public class Sudoku {
String digits = "123456789";
String rows = "ABCDEFGHI";
String cols = "123456789";
char[][] board;
String[] squares;
HashMap<String, ArrayList<TreeSet<String>>> units;
HashMap<String, TreeSet<String>> peers;
public Sudoku(String s1){
squares = new String[s1.length()];
units = new HashMap<>();
peers = new HashMap<>();
board = new char[9][9];
for (int i = 0; i < rows.length(); i++) {
for (int j = 0; j < cols.length(); j++){
squares[i*9 + j] = "" + rows.charAt(i) + cols.charAt(j);
board[i][j] = s1.charAt(i*9 + j);
}
}
for (int i = 0; i < rows.length(); i++) {
for (int j = 0; j < cols.length(); j++){
String curr = squares[i*9 + j];
// row neighbors
TreeSet<String> l1 = new TreeSet<>();
// col neighbors
TreeSet<String> l2 = new TreeSet<>();
for (int k = 0; k < 9; k++){
l1.add(squares[i*9 + k]);
l2.add(squares[j + k*9]);
}
// block neighbors
TreeSet<String> l3 = new TreeSet<>();
int top = i / 3;
int left = j / 3;
for (int a = 0; a < 3; a++){
int ind1 = top*3 + a;
for (int b = 0; b < 3; b++){
int ind2 = left*3 + b;
l3.add(squares[ind1*9 + ind2]);
}
}
ArrayList<TreeSet<String>> bigset = new ArrayList<>();
bigset.add(l1);bigset.add(l2);bigset.add(l3);
units.put(curr, bigset);
TreeSet<String> united = new TreeSet<>();
united.addAll(l1); united.addAll(l2); united.addAll(l3);
united.remove(curr);
peers.put(curr, united);
}
}
}
public HashMap<String, String> eliminate(HashMap<String, String> values, String s, String d){
// already eliminated
if (!values.get(s).contains(d))
return values;
String newVal = values.get(s).replace(d, "");
values.put(s, newVal);
// nothing exist to put here
if (values.get(s).length() == 0)
return null;
else if (values.get(s).length() == 1){ // only one possibility to put here
String d2 = values.get(s).charAt(0) + "";
for (String peer : peers.get(s)) {
if (eliminate(values, peer, d2) == null){
return null; // could not eliminated from peers, sth is wrong
}
}
}
// look possible values in my block
for (TreeSet<String> unit : units.get(s) ) {
TreeSet<String> dplaces = new TreeSet<>();
for (String s2 : unit){
if (values.get(s2).contains(d)){
dplaces.add(s2);
}
}
if (dplaces.size() == 0){
return null;
}else if (dplaces.size() == 1){ // we found the right place for d
if (assign(values, dplaces.first(), d) == null){
return null;
}
}
}
return values;
}
public HashMap<String, String> assign(HashMap<String, String> values, String s, String d){
String otherVals = values.get(s).replace(d, "");
for (int i = 0; i < otherVals.length(); i++){
String d2 = otherVals.charAt(i) + "";
if (eliminate(values, s, d2) == null)
return null;
}
return values;
}
public void printBoard(){
System.out.println();
for (int i = 0; i < 9; i++){
for (int j = 0; j < 9; j++){
System.out.print(board[i][j] + " ");
}
System.out.println();
}
}
public void printPossibles(HashMap<String, String> values){
System.out.println();
for (int i = 0; i < 9; i++){
for (int j = 0; j < 9; j++){
System.out.print(values.get(squares[i*9 + j]) + " ");
}
System.out.println();
}
}
public HashMap<String, String> parseBoard() {
HashMap<String, String> values = new HashMap<>();
for (int i = 0; i < squares.length; i++){
String s = squares[i];
values.put(s, "123456789");
}
for (int i = 0; i < 9; i++){
for (int j = 0; j < 9; j++){
if (board[i][j] != '.' && board[i][j] != '0'){
if (assign(values, squares[i*9 + j], board[i][j] + "") == null){
return null;
}
}
}
}
return values;
}
public HashMap<String, String> search(HashMap<String, String> values){
if (values == null)
return null;
boolean allDone = true;
for (String t : values.values()){
if (t.length() != 1){
allDone = false;
break;
}
}
if (allDone)
return values;
String minPossible = "";
int minPosssibleCnt = 10;
for (String s : squares){
int size = values.get(s).length();
if (size > 1 && size < minPosssibleCnt){
minPossible = s;
minPosssibleCnt = size;
}
}
String possibleArr = values.get(minPossible);
for (int ind = 0; ind < possibleArr.length(); ind++){
String d = possibleArr.charAt(ind) + "";
HashMap<String, String> ves = new HashMap<>(values);
HashMap<String, String> res = search(assign(ves, minPossible, d));
if (res != null)
return res;
}
return null;
}
public static void main(String[] args) {
// TODO Auto-generated method stub
String fileName = "E:\\yusuf\\projects\\eclipseJava\\sudokuNorvig\\src\\sudokuNorvig\\top95.txt";
ArrayList<String> strs = new ArrayList<>();
try (BufferedReader br = new BufferedReader(new FileReader(fileName))) {
String line;
while ((line = br.readLine()) != null) {
//System.out.println(line);
strs.add(line);
}
} catch (IOException e) {
e.printStackTrace();
}
int norvig = 0, backtracking = 0;
long nTime = 0, bTime = 0;
for (int i = 0; i < strs.size(); i++){
String s = strs.get(i);
Sudoku sudo1 = new Sudoku(s);
SudokuBacktracker sudo2 = new SudokuBacktracker(s);
long time1 = System.currentTimeMillis();
HashMap<String, String> res = sudo1.search(sudo1.parseBoard());
//sudo1.printPossibles(res);
long time2 = System.currentTimeMillis();
long deltaT1 = time2 - time1;
nTime += deltaT1;
time1 = System.currentTimeMillis();
boolean b1 = sudo2.solve();
time2 = System.currentTimeMillis();
long deltaT2 = time2 - time1;
bTime += deltaT2;
if (res == null){
System.out.println("norvik fail");
}
if (!b1){
System.out.println("backtracking failed fail");
}
if (deltaT1 < deltaT2){
norvig++;
}else if (deltaT2 < deltaT1){
backtracking++;
}
}
System.out.println("norvig:" + norvig + " backtracking: " + backtracking);
System.out.println("total time (ms):" + nTime + " vs " + bTime);
}
}