-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsudoku_solver.cpp
331 lines (270 loc) · 7.98 KB
/
sudoku_solver.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
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
#include <iostream>
#include <ostream>
#include <string>
#include <fstream>
#include <chrono>
#include "functions.h"
using std::cin, std::cout, std::endl;
using std::ostream, std::ifstream;
using std::string;
using std::chrono::high_resolution_clock, std::chrono::duration_cast, std::chrono::microseconds;
Board operator<<(ostream& os, Board& b);
Board* solve_board(Board& b);
Boards_Array* possibilities(Board& b);
Board* backtrack(Boards_Array* ba);
void copy_board(Board& original, Board& new_one);
bool is_valid(Board& b);
bool solved(Board& b);
bool rows_good(Board& b);
bool cols_good(Board& b);
bool squares_good(Board& b);
int main() {
// open board input
string filename;
cout << "Please enter file name: ";
cin >> filename;
ifstream ifs(filename);
if(!ifs.is_open()) {
cout << "Sorry! Failed to open " << filename << endl;
return -1;
}
auto start = high_resolution_clock::now();
Board x;
int input;
int row = 0;
int col = 0;
while(ifs >> input) {
x.board[row][col] = input;
if (col < 8) col++;
else {
row++;
col = 0;
}
}
// cout << x;
// Solve board
solve_board(x);
// possibilities(x);
auto stop = high_resolution_clock::now();
auto duration = duration_cast<microseconds>(stop - start);
cout << "This function took: " << duration.count() << " microseconds to run" << endl;
return 0;
}
Board* backtrack(Boards_Array* ba) {
if (ba->size == 0 || ba->index == ba->size) {
// return bad borad - to do
// cout << "backtrack delete";
delete[] ba->next;
ba->next = nullptr;
delete[] ba;
ba = nullptr;
return nullptr;
}
else {
// do backtrack search on board
// cout << ba.next[ba.index];
Board* test_board = solve_board(ba->next[ba->index]);
if(test_board != nullptr) {
// cout << test_board;
delete[] ba->next;
ba->next = nullptr;
delete[] ba;
ba = nullptr;
return test_board;
} else {
ba->index++;
return backtrack(ba);
}
}
}
Board* solve_board(Board& b) {
if(solved(b)) {
cout << b;
return &b;
}
else {
// do backtrack search
// cout << b;
Boards_Array* possible = possibilities(b);
return backtrack(possible);
}
}
Boards_Array* possibilities(Board& b) {
// cout << b;
// returns pointer to a new array that contains next possible boards
int empty_row = -1;
int empty_col = -1;
for(int row = 0; row < 9; row++) {
for(int col = 0; col < 9; col++) {
if(b.board[row][col] == 0) {
empty_row = row;
empty_col = col;
break;
}
}
if(empty_row != -1) break;
}
// cout << "empty row " << empty_row << endl;
// cout << "empty col " << empty_col << endl;
int valid_possibilities[9] {0};
int count = 0;
if (empty_row != -1) {
for(int i = 1; i < 10; i++) {
b.board[empty_row][empty_col] = i;
// cout << b;
if(is_valid(b)) {
// cout << "is valid!" << endl;
valid_possibilities[count] = i;
count++;
}
}
}
// cout << "count: " << count;
// create a pointer to new array of boards of length count
Boards_Array* next_possibilities = new Boards_Array;
(*next_possibilities).next = new Board[count];
(*next_possibilities).size = count;
// copy position from original board to new one
// replace
for(int i = 0; i < count; i++) {
copy_board(b, (*next_possibilities).next[i]);
(*next_possibilities).next[i].board[empty_row][empty_col] = valid_possibilities[i];
}
// cout << "Hi" << endl;
return next_possibilities;
}
void copy_board(Board& original, Board& new_one) {
for(int row = 0; row < 9; row++) {
for(int col = 0; col < 9; col++) {
new_one.board[row][col] = original.board[row][col];
}
}
}
Board operator<<(ostream& os, Board& b) {
os << " - - - - - - - - - - - -" << endl;
for(int row = 0; row < 9; row++) {
os << "|";
for(int col = 0; col < 9; col++) {
if(b.board[row][col] == 0) {
os << " :";
}
else {
os << " " << b.board[row][col];
}
if ((col - 2) % 3 == 0) {
os << " |";
}
}
os << endl;
if ((row - 2) % 3 == 0) {
os << " - - - - - - - - - - - -" << endl;
}
}
}
bool is_valid(Board& b) {
return rows_good(b) && cols_good(b) && squares_good(b);
}
bool rows_good(Board& b) {
int values[9] {0};
int count = 0;
int cell_value = 0;
for(int row = 0; row < 9; row++) {
for(int col = 0; col < 9; col++) {
cell_value = b.board[row][col];
for(int i = 0; i < count; i++) {
if(cell_value != 0 && cell_value == values[i]) {
// cout << "failed rows: " << row << " " << col << endl;
return false;
}
// cout << "hi there" << endl;
}
// add value to values
if(cell_value != 0) {
// cout << "suppp";
values[count] = cell_value;
count++;
}
}
// reset values[] and count for next row
for(int i = 0; i < count; i++) {
values[i] = 0;
}
count = 0;
}
// cout << "what's good" << endl;
return true;
}
bool cols_good(Board& b) {
int values[9] {0};
int count = 0;
int cell_value;
for(int col = 0; col < 9; col++) {
for(int row = 0; row < 9; row++) {
cell_value = b.board[row][col];
for(int i = 0; i < count; i++) {
cell_value = b.board[row][col];
if(cell_value == values[i]) {
// cout << "failed cols: " << row << " " << col << endl;
return false;
}
}
if(cell_value != 0) {
// cout << "sup";
values[count] = cell_value;
count++;
}
}
// reset values and count for next column
for(int i = 0; i < count; i++) {
values[i] = 0;
}
count = 0;
}
return true;
}
bool squares_good(Board& b) {
int values[9] {0};
int count = 0;
int row_coord, col_coord, cell_value;
for(int h_offset = 0; h_offset < 3; h_offset++) {
for(int v_offset = 0; v_offset < 3; v_offset++) {
for(int col = 0; col < 3; col++) {
for(int row = 0; row < 3; row++) {
row_coord = row + 3*v_offset;
col_coord = col + 3*h_offset;
cell_value = b.board[row_coord][col_coord];
for(int i = 0; i < count; i++) {
if(cell_value != 0 && cell_value == values[i]) {
// cout << "failed squares: " << row << " " << col << endl;
return false;
}
}
if(cell_value != 0) {
values[count] = cell_value;
count++;
}
}
}
// reset values[] and count
for(int i = 0; i < count; i++) {
values[i] = 0;
}
count = 0;
}
}
return true;
}
bool solved(Board& b) {
// board is tautologically solved if a] it's valid b] every square is filled
if (!is_valid(b)) {
return false;
}
for(int row = 0; row < 9; row++) {
for(int col = 0; col < 9; col++) {
if(b.board[row][col] == 0) {
return false;
}
}
}
return true;
}