-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path25x25_solver.cpp
416 lines (346 loc) · 9.62 KB
/
25x25_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
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
#include <cstdio>
#include <unordered_set>
#include <map>
#include <array>
#pragma GCC optimize("Ofast")
#pragma GCC target("avx,avx2,fma")
using namespace std;
const int grid_size = 25; //board size
char guesses[grid_size] = {'1','2','3','4','5','6','7','8','9','A','B','C','D','E','F','G','H','I', 'J','K','L','M','N','O','P'};
int guess = 0;
unordered_set<char> notes[grid_size][grid_size] = {};
void populate_grid(char grid[grid_size][grid_size]){
for (int i=0;i<grid_size;++i){
for (int j=0;j<grid_size;++j){
scanf(" %c", &grid[i][j]);
if (grid[i][j] != '0'){
guess++;
}
}
}
}
void display_grid(char grid[grid_size][grid_size]){
for (int i=0;i<grid_size;++i){
printf("%c ", grid[i][0]);
for (int j=1;j<grid_size-1; ++j){
printf("%c ", grid[i][j]);
}
printf("%c\n", grid[i][grid_size-1]);
}
}
bool valid_sudoku(char grid[grid_size][grid_size], char input, int row, int col){//something is wrong here
int box_start_row = (row < 5) ? 0 : (row < 10) ? 5 : (row < 15) ? 10 : (row < 20) ? 15 : 20; //sets the row start of box
int box_start_col = (col < 5) ? 0 : (col < 10) ? 5 : (col < 15) ? 10 : (col < 20) ? 15 : 20; //sets the col start of the box
for (int i=0;i<grid_size;i++){
char temp_row = grid[row][i];
char temp_col = grid[i][col];
//change the col every 4 rows
if (!(i%5) && i != 0){
box_start_col += 1;
}
char temp_box = grid[box_start_row + (i % 5)][box_start_col];
if (temp_row == input || temp_col == input || temp_box == input){
return false;
}
}
return true;
}
void remove_notes(char grid[grid_size][grid_size], int row, int col, char input){
int box_start_row = (row < 5) ? 0 : (row < 10) ? 5 : (row < 15) ? 10 : (row < 20) ? 15 : 20; //sets the row start of box
int box_start_col = (col < 5) ? 0 : (col < 10) ? 5 : (col < 15) ? 10 : (col < 20) ? 15 : 20; //sets the col start of the box
for (int i=0;i<grid_size;i++){
if (grid[row][i] == '0'){
notes[row][i].erase(input);
}
if (grid[i][col] == '0'){
notes[i][col].erase(input);
}
//change the col every 4 rows
if (!(i%5) && i != 0){
box_start_col += 1;
}
if (grid[box_start_row + (i % 5)][box_start_col] == '0'){
notes[box_start_row + (i % 5)][box_start_col].erase(input);
}
}
}
void remove_notes(char grid[grid_size][grid_size], int row, int col_left, int col_right, char input, bool row_note){
if (row_note){
for (int i=0;i<grid_size;i++){
if (i != col_left && i != col_right && grid[row][i] == '0'){
notes[row][i].erase(input);
}
}
}
else{
for (int i=0;i<grid_size;i++){
if (i != col_left && i != col_right && grid[i][row] == '0'){
notes[i][row].erase(input);
}
}
}
}
bool best_guess(char grid[grid_size][grid_size], int &row, int &col){
int min = grid_size;
bool completed_grid = true;
for (int i=0;i<grid_size;++i){
for (int j=0;j<grid_size;++j){
if (grid[i][j] == '0'){
if (notes[i][j].size()<min){
row = i;
col = j;
min = notes[i][j].size();
completed_grid = false;
}
}
}
}
return completed_grid;
}
//for naked pairs/triples/etc we can just check if any bitstring is identical to another
//in the same row/col/box and if its true remove those candidates from the other ones not neaded
void build_notes(char grid[grid_size][grid_size]){
for (int i=0;i<grid_size;i++){
for (int j=0;j<grid_size;j++){
if (grid[i][j] == '0'){
for (int guess=0;guess<grid_size; guess++){
if (valid_sudoku(grid, guesses[guess], i, j)){
notes[i][j].insert(guesses[guess]);
}
}
}
}
}
}
bool hidden_single(char grid[grid_size][grid_size]){ //check box, check row and check col
bool changed = false;
for (int i=0;i<grid_size;i++){
map<char, int> found_row;
map<char, int> found_col;
for (int j=0;j<grid_size;j++){
if (grid[i][j] == '0'){
for (char num : notes[i][j]){
found_row[num]++;
}
}
if (grid[j][i] == '0'){
for (char num : notes[j][i]){
found_col[num]++;
}
}
}
//Hidden single for row
for (auto num : found_row){
if (num.second == 1){
for (int col=0;col<grid_size;col++){
if (grid[i][col] == '0'){
if (notes[i][col].find(num.first) != notes[i][col].end()){
grid[i][col] = num.first;
remove_notes(grid, i, col, num.first);
changed = true;
guess++;
}
}
}
}
}
//hidden single for all columns
for (auto num : found_col){
if (num.second == 1){
for (int row=0;row<grid_size;row++){
if (grid[row][i] == '0'){
if (notes[row][i].find(num.first) != notes[row][i].end()){
grid[row][i] = num.first;
remove_notes(grid, row, i, num.first);
changed = true;
guess++;
}
}
}
}
}
}
return changed;
}
//if notes == notes [col + 1] remove notes from others
bool naked_tuples(char grid[grid_size][grid_size]){
bool changed = false;
for (int i=0;i<grid_size;i++){
for (int j=0;j<grid_size - 1;j++){
if (notes[i][j].size() == 2){
for (int k=j+1;k<grid_size;k++){
if (notes[i][j] == notes[i][k]){
changed = true;
for (auto x : notes[i][j]){
remove_notes(grid, i, j, k, x, true);
}
}
}
}
}
}
for (int i=0;i<grid_size;i++){
for (int j=0;j<grid_size - 1;j++){
if (notes[j][i].size() == 2){
for (int k=j+1;k<grid_size;k++){
if (notes[j][i] == notes[k][i]){
changed = true;
for (auto x : notes[j][i]){
remove_notes(grid, i, j, k, x, false);
}
}
}
}
}
}
return changed;
}
//if a set of numbers is only found in 2 col/row we can eliminate everything else
//check the intersection of each unordered set, if they intersect with a size greater than 2 we will
//know that its a hidden tuple
//we then replace the notes with that difference
unordered_set<char> unordered_set_difference(unordered_set<char> left, int row, int col_left, int col_right, bool row_note){
if (row_note){
for (int i=0;i<grid_size;i++){
if (i != col_left && i != col_right){
for (auto element : notes[row][i]){
left.erase(element);
}
}
}
}
else{
for (int i=0;i<grid_size;i++){
if (i != col_left && i != col_right){
for (auto element : notes[i][row]){
left.erase(element);
}
}
}
}
return left;
}
unordered_set<char> unordered_set_intersection(unordered_set<char> left, unordered_set<char> right){
unordered_set<char> result;
for (auto element : left){
if (right.count(element) == 1){
result.emplace(element);
}
}
return result;
}
bool hidden_tuples(char grid[grid_size][grid_size]){
bool changed = false;
for (int i=0;i<grid_size;i++){
for (int j=0;j<grid_size - 1;j++){
if (notes[i][j].size()>=2){
for (int k=j+1;k<grid_size;k++){
if (notes[i][j].size() > 2 || notes[i][k].size() > 2){
unordered_set<char> intersection = unordered_set_intersection(notes[i][j], notes[i][k]);
if (intersection.size() >= 2){
unordered_set<char> difference = unordered_set_difference(intersection, i, j, k, true);
if (difference.size() == 2){
notes[i][j] = notes[i][k] = difference;
changed = true;
break;
}
}
}
}
}
}
}
for (int i=0;i<grid_size;i++){
for (int j=0;j<grid_size - 1;j++){
if (notes[j][i].size()>=2){
for (int k=j+1;k<grid_size;k++){
if (notes[j][i].size() > 2 || notes[k][i].size() > 2){
unordered_set<char> intersection = unordered_set_intersection(notes[j][i], notes[k][i]);
if (intersection.size() >= 2){
unordered_set<char> difference = unordered_set_difference(intersection, i, j, k, false);
if (difference.size() == 2){
notes[j][i] = notes[k][i] = difference;
changed = true;
break;
}
}
}
}
}
}
}
return changed;
}
bool naked_single(char grid[grid_size][grid_size]){
char input;
bool changed = false;
for (int i=0;i<grid_size;++i){
for (int j=0;j<grid_size;++j){
if (grid[i][j] == '0'){
if (notes[i][j].size() == 1){ //theres only one option here
guess++;
changed = true;
input = *notes[i][j].begin();
grid[i][j] = input; //make the guess
remove_notes(grid, i, j, input);
}
}
}
}
return changed;
}
void natural_deduction(char grid[grid_size][grid_size]){
while (true){
if(hidden_tuples(grid)){
continue;
}
if (naked_single(grid)){
continue;
}
if(guess == grid_size*grid_size){
break;
}
if (hidden_single(grid)){
continue;
}
if(guess == grid_size*grid_size){
break;
}
if(naked_tuples(grid)){
continue;
}
if(hidden_tuples(grid)){
continue;
}
break;
}
}
bool solve_grid(char grid[grid_size][grid_size]){
int row = 0, col = 0;
if (guess == grid_size*grid_size || best_guess(grid, row, col)){
return true;
}
for (int i=0;i<grid_size;i++){
if (valid_sudoku(grid, guesses[i], row, col)){
grid[row][col] = guesses[i];
if (solve_grid(grid)){
return true;
}
grid[row][col] = '0';
}
}
return false;
}
int main(){
char grid[grid_size][grid_size];
populate_grid(grid);
build_notes(grid);
natural_deduction(grid);
if (solve_grid(grid)){
display_grid(grid);
}
else{
printf("No Solution\n");
}
return 0;
}