-
Notifications
You must be signed in to change notification settings - Fork 0
/
simpleSolveFunctions.c
379 lines (295 loc) · 14.6 KB
/
simpleSolveFunctions.c
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
//
// Created by Jamie on 27/10/2017.
//
#include <stdio.h>
#include "global.h"
#include "cellFunctions.h"
#include "utilityFunctions.h"
extern int steps;
/**
* Solves hidden singles in all rows of the given sudoku and returns the amount of changes made
*
* @param p Sudoku pointer
* @return Number of changes made
*/
int solveRows(int ***p) {
Cell cell;
Block block;
int startSteps = steps;
for (int row = 0; row < size; row++) { // Traveling down
cell.row = row;
// Remove suggestions for values in the same row, column or block.
for (int column = 0; column < size; column++) { // Traveling right
cell.column = column;
setBlockForCell(&block, cell);
if (findFinalCellValue(p, cell) == 0) {
// If this is only cell in this row, column or block that can have a value set it to that value
for (int option = 1; option <= size; option++) {
if (p[cell.column][cell.row][option] == 1) {
if (cellsWithSuggestionInColumn(p, cell.column, option) == 1
|| cellsWithSuggestionInRow(p, cell.row, option) == 1
|| cellsWithSuggestionInBlock(p, block, option) == 1) {
steps++;
if (outputSolveSteps) {
printf("[Step %i]: Found and solved hidden single in row %i for cell %i, %i. Setting value to %i \n", steps ,cell.row, cell.column, cell.row, option);
}
setFinalCalculatedValue(p, cell, option);
}
}
}
}
}
}
return steps - startSteps;
}
/**
* Solves hidden singles in all columns of the given sudoku and returns the amount of changes made
*
* @param p Sudoku pointer
* @return Number of changes made
*/
int solveColumns(int ***p) {
Cell cell;
Block block;
int startSteps = steps;
for (int column = 0; column < size; column++) { // Traveling right
cell.column = column;
for (int row = 0; row < size; row++) { // Traveling down
cell.row = row;
setBlockForCell(&block, cell);
if (findFinalCellValue(p, cell) == 0) {
// If this is only cell in this row, column or block that can have a value set it to that value
for (int option = 1; option <= size; option++) {
if (p[cell.column][cell.row][option] == 1) {
if (cellsWithSuggestionInColumn(p, cell.column, option) == 1
|| cellsWithSuggestionInRow(p, cell.row, option) == 1
|| cellsWithSuggestionInBlock(p, block, option) == 1) {
steps++;
if (outputSolveSteps) {
printf("[Step %i]: Found and solved hidden single in column %i for cell %i, %i. Setting value to %i \n", steps, cell.column, cell.column, cell.row, option);
}
setFinalCalculatedValue(p, cell, option);
}
}
}
}
}
}
return steps - startSteps;
}
/**
* Solves hidden singles in all blocks of the given sudoku and returns the amount of changes made
*
* @param p Sudoku pointer
* @return Number of changes made
*/
int solveBlocks(int ***p) {
Cell cell;
Block block;
int startSteps = steps;
for (int c = 0; c < sizeRoot; c++) {
block.blockColumn = c;
for (int r = 0; r < sizeRoot; r++) {
block.blockRow = r;
for (int columnOffset = 0; columnOffset < sizeRoot; columnOffset++) {
cell.column = (block.blockColumn * sizeRoot) + columnOffset;
for (int rowOffset = 0; rowOffset < sizeRoot; rowOffset++) {
cell.row = (block.blockRow * sizeRoot) + rowOffset;
if (findFinalCellValue(p, cell) == 0) {
// If this is only cell in this row, column or block that can have a value set it to that value
for (int option = 1; option <= size; option++) {
if (p[cell.column][cell.row][option] == 1) {
if (cellsWithSuggestionInColumn(p, cell.column, option) == 1
|| cellsWithSuggestionInRow(p, cell.row, option) == 1
|| cellsWithSuggestionInBlock(p, block, option) == 1) {
steps++;
if (outputSolveSteps) {
printf("[Step %i]: Found and solved hidden single in block %i %i for cell %i, %i. Setting value to %i \n", steps, block.blockColumn, block.blockRow, cell.column, cell.row, option);
}
setFinalCalculatedValue(p, cell, option);
}
}
}
}
}
}
}
}
return steps - startSteps;
}
/** Finds cell suggestions to the order entered constrained in certain blocks and columns or rows and eliminates suggestions that are in disallowed places then returns the number of changes made
*
* @param p Sudoku pointer
* @param order Order of solutions to find
* @return Number of changes made
*/
int solveSuggestionBlockLines(int ***p, int order) {
int counter[order];
for (int i = 0; i < order; i++) {
counter[i] = 0;
}
Block blocks[order];
int blocksSelected = 1;
int startSteps = steps;
do {
int correctOrder = 1;
for (int j = 0; j < order; j++) {
blocks[j].blockColumn = counter[j] % sizeRoot;
blocks[j].blockRow = (counter[j] - blocks[j].blockColumn) / sizeRoot;
}
for (int j = sizeRoot - 1; j >= 0; j--) {
if (blocks[j].blockColumn > 0 || blocks[j].blockRow > 0) {
blocksSelected = j + 1;
break;
}
}
int columnSelection = 1;
int rowSelection = 1;
if (blocksSelected > 1) {
for (int j = 0; j < blocksSelected - 1; j++) {
if (blocks[j].blockRow * sizeRoot + blocks[j].blockColumn >=
blocks[j + 1].blockRow * sizeRoot + blocks[j + 1].blockColumn) {
correctOrder = 0;
}
if (blocks[j].blockColumn != blocks[j + 1].blockColumn) {
columnSelection = 0;
}
if (blocks[j].blockRow != blocks[j + 1].blockRow) {
rowSelection = 0;
}
}
}
// If block combination has not been used before and is valid.
if (blocksSelected == order && correctOrder && (columnSelection || rowSelection)) {
Cell cell;
if (columnSelection) {
int leftColumn = blocks[0].blockColumn;
for (int option = 1; option <= size; option++) {
int columnsUsed[sizeRoot];
for (int j = 0; j < sizeRoot; j++) {
columnsUsed[j] = 0;
}
for (int j = 0; j < blocksSelected; j++) {
for (int columnOffset = 0; columnOffset < sizeRoot; columnOffset++) {
int column = columnOffset + (blocks[j].blockColumn * sizeRoot);
for (int rowOffset = 0; rowOffset < sizeRoot; rowOffset++) {
int row = rowOffset + (blocks[j].blockRow * sizeRoot);
if (p[column][row][option] == 1) {
columnsUsed[columnOffset] = 1;
break;
}
}
}
}
int usedColumnCount = 0;
for (int c = 0; c < sizeRoot; c++) {
if (columnsUsed[c] == 1) {
usedColumnCount++;
}
}
if (usedColumnCount == blocksSelected) {
for (int c = 0; c < sizeRoot; c++) {
if (columnsUsed[c] == 1) {
// If this column was used we must eliminate this option from rows of the column that are not in the selected blocks
int madeChange = 0;
for (int row = 0; row < size; row++) {
int rowInSelection = 0;
for (int j = 0; j < sizeRoot; j++) {
if (row >= (blocks[j].blockRow * sizeRoot) && row < (blocks[j].blockRow + 1) * sizeRoot) {
rowInSelection = 1;
break;
}
}
if (!rowInSelection && (p[(leftColumn * sizeRoot) + c][row][option] == 1)) {
cell.row = row;
cell.column = (leftColumn * sizeRoot) + c;
if (canCellWright(p, cell)) {
if (!madeChange) {
madeChange = 1;
steps++;
if (outputSolveSteps) {
printf("[Step %i]: Found constrained row suggestion of order %i eliminated %i from blocks",
steps, order, option);
for (int j = 0; j < blocksSelected; j++) {
printf(" %i, %i ", blocks[j].blockColumn, blocks[j].blockRow);
}
printf("\n");
}
}
eliminatePossibleFromCell(p, cell, option);
}
}
}
}
}
}
}
}
if (rowSelection) {
int leftRow = blocks[0].blockRow;
for (int option = 1; option <= size; option++) {
int rowsUsed[sizeRoot];
for (int j = 0; j < sizeRoot; j++) {
rowsUsed[j] = 0;
}
for (int j = 0; j < blocksSelected; j++) {
for (int rowOffset = 0; rowOffset < sizeRoot; rowOffset++) {
int row = rowOffset + (blocks[j].blockRow * sizeRoot);
for (int columnOffset = 0; columnOffset < sizeRoot; columnOffset++) {
int column = columnOffset + (blocks[j].blockColumn * sizeRoot);
if (p[row][column][option] == 1) {
rowsUsed[rowOffset] = 1;
break;
}
}
}
}
int usedRowCount = 0;
for (int c = 0; c < sizeRoot; c++) {
if (rowsUsed[c] == 1) {
usedRowCount++;
}
}
if (usedRowCount == blocksSelected) {
for (int c = 0; c < sizeRoot; c++) {
if (rowsUsed[c] == 1) {
// If this row was used we must eliminate this option from columns of the row that are not in the selected blocks
int madeChange = 0;
for (int column = 0; column < size; column++) {
int columnInSelection = 0;
for (int j = 0; j < sizeRoot; j++) {
if (column >= (blocks[j].blockColumn * sizeRoot) && column < (blocks[j].blockColumn + 1) * sizeRoot) {
columnInSelection = 1;
break;
}
}
if (!columnInSelection && (p[(leftRow * sizeRoot) + c][column][option] == 1)) {
cell.column = column;
cell.row = (leftRow * sizeRoot) + c;
eliminatePossibleFromCell(p, cell, option);
if (canCellWright(p, cell)) {
if (!madeChange) {
madeChange = 1;
steps++;
if (outputSolveSteps) {
printf("[Step %i]: Found constrained row suggestion of order %i eliminated %i from blocks",
steps, order, option);
for (int j = 0; j < blocksSelected; j++) {
printf(" %i, %i ", blocks[j].blockColumn, blocks[j].blockRow);
}
printf("\n");
}
}
eliminatePossibleFromCell(p, cell, option);
}
}
}
}
}
}
}
}
}
} while (!nBitCounterAddOne(order, counter, size));
return steps - startSteps;
}