-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsolver.cpp
301 lines (231 loc) · 8.75 KB
/
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
#include "solver.h"
#include <stdlib.h>
#include <iostream>
#include "decision.h"
#include "logger.h"
namespace sudoku {
Solver::Solver( QVector< QVector<int> > const in_fixed_numbers )
{
for(int i=0; i<NUM_ENTRIES; i++) {
QVector<int> row;
for(int j=0; j<NUM_ENTRIES; j++) {
row.push_back( in_fixed_numbers[i][j] );
}
fixed_numbers.push_back(row);
}
}
Solver::~Solver()
{
}
void Solver::setup()
{
// setup a set with all numbers
QSet< int > all_numbers;
for( int i=1; i<=NUM_ENTRIES; i++ ) {
all_numbers.insert( i );
}
// initialize blocks to have all numbers
for( int i=0; i<NUM_ENTRIES; i++ ) {
blocks.push_back( all_numbers );
}
// convert integer array to two-dimensional QVector
for( int i=0; i<NUM_ENTRIES; i++ ) {
QVector< int > row;
QVector< QSet< int > > numbers_to_try_row;
for( int j=0; j<NUM_ENTRIES; j++ ) {
int const fixed_number = fixed_numbers[i][j];
row.push_back(fixed_number);
QSet<int> emptySet;
numbers_to_try_row.push_back( emptySet );
int const block_number = SIDE*(i/SIDE) + (j/SIDE);
blocks[block_number].remove( fixed_number );
}
filled_grid.push_back( row );
numbers_to_try.push_back( numbers_to_try_row );
}
}
bool Solver::solve()
{
setup();
// check if starting numbers are valid
QList<Decision> decisions;
bool addedDecisions=false;
if( !isValid(addedDecisions, decisions) ) {
std::cerr << "Starting numbers are invalid!" << std::endl;
reverseDecisions( decisions );
return false;
}
return recursiveSolve(0);
}
bool Solver::recursiveSolve( int depth )
{
if( isFilled() ) {
return true;
}
Logger::getInstance().log( "***** recursing to depth " + QString::number( depth ) + " *****" );
QList<Decision> decisions;
bool addedDerivations;
do {
addedDerivations=false;
if( !isValid(addedDerivations, decisions) ) {
Logger::getInstance().log( "Not a valid solution." );
reverseDecisions( decisions );
return false;
}
} while( addedDerivations );
if( isFilled() ) {
return true;
}
int row=0;
int column=0;
bool found_empty_cell = false;
for(int i=0; i<NUM_ENTRIES && !found_empty_cell; i++ ) {
for(int j=0;j<NUM_ENTRIES; j++ ) {
if( filled_grid[i][j]==0 ) {
row = i;
column = j;
found_empty_cell = true;
break;
}
}
}
if( !found_empty_cell ) {
Logger::getInstance().log( "Did not find empty cell." );
reverseDecisions( decisions );
return false;
} else {
QSet<int> numbers_to_try_cell( numbers_to_try[row][column] );
for( QSet<int>::const_iterator const_it=numbers_to_try_cell.begin(); const_it!=numbers_to_try_cell.end(); const_it++) {
Decision candidate(row,column,*const_it);
decisions.push_back(candidate);
filled_grid[row][column]=*const_it;
Logger::getInstance().log( "Trying " + QString::number( *const_it ) + " at " + QString::number( row ) + "," + QString::number( column ) );
if( recursiveSolve(depth+1) ) {
return true;
} else {
Logger::getInstance().log( "***** unsuccessful: back at depth " + QString::number( depth ) + " *****" );
// removing try
filled_grid[row][column]=0;
decisions.pop_back();
}
}
}
// backtracking to higher recursion level
reverseDecisions( decisions );
return false;
}
void Solver::reverseDecisions( QList<Decision> const & decisions )
{
for(QList<Decision>::const_iterator const_it=decisions.begin(); const_it!=decisions.end(); const_it++ ) {
filled_grid[const_it->getX()][const_it->getY()] = 0;
}
}
bool Solver::isFilled() const
{
for(int i=0; i<NUM_ENTRIES; i++) {
for(int j=0; j<NUM_ENTRIES; j++) {
if( filled_grid[i][j]==0 ) {
return false;
}
}
}
return true;
}
bool Solver::isValidRow( QVector< QVector< int > > const &rFilledGrid, int const row, QSet< int > &rUnusedNumbers ) const
{
for( int i=1; i<=NUM_ENTRIES; i++ ) {
rUnusedNumbers.insert(i);
}
for( int col=0; col<NUM_ENTRIES; col++) {
if( rFilledGrid[row][col]!=0 && !rUnusedNumbers.remove( rFilledGrid[row][col] ) ) { // number already seen in the row
Logger::getInstance().log( "Checking Rows: Cell " + QString::number( row ) + "," + QString::number( col ) + " value: " + QString::number( rFilledGrid[row][col] ) + " is not valid!");
return false;
}
}
return true;
}
bool Solver::isValidColumn( QVector< QVector< int > > const &rFilledGrid, int const col, QSet< int > &rUnusedNumbers ) const
{
for( int i=1; i<=NUM_ENTRIES; i++ ) {
rUnusedNumbers.insert(i);
}
for( int row=0; row<NUM_ENTRIES; row++) {
if( rFilledGrid[row][col]!=0 && !rUnusedNumbers.remove( rFilledGrid[row][col] ) ) { // number already seen in the column
Logger::getInstance().log( "Checking Columns: Cell " + QString::number( row ) + "," + QString::number( col ) + " value: " + QString::number( rFilledGrid[row][col] ) + " is not valid!");
return false;
}
}
return true;
}
bool Solver::isValidBlock( QVector< QVector< int > > const &rFilledGrid, int const block, QSet< int > &rUnusedNumbers ) const
{
for( int i=1; i<=NUM_ENTRIES; i++ ) {
rUnusedNumbers.insert(i);
}
int const row_start = ( block / SIDE ) * SIDE;
int const col_start = ( block % SIDE ) * SIDE;
for( int block_pos=0; block_pos<NUM_ENTRIES; block_pos++ ) {
int const row = row_start + ( block_pos / SIDE );
int const col = col_start + ( block_pos % SIDE );
if( rFilledGrid[row][col]!=0 && !rUnusedNumbers.remove( rFilledGrid[row][col] ) ) { // number already seen in the column
Logger::getInstance().log( "Checking Blocks: Cell " + QString::number( row ) + "," + QString::number( col ) + " value: " + QString::number( rFilledGrid[row][col] ) + " is not valid!" );
return false;
}
}
return true;
}
bool Solver::isValid( bool & addedDecision, QList<Decision> & decisions )
{
QSet< int > all_numbers;
for( int num=1; num<=NUM_ENTRIES; num++ ) {
all_numbers.insert(num);
}
// check that no row contains duplicate numbers
QVector< QSet< int > > missing_numbers_per_row( NUM_ENTRIES );
for( int row=0; row<NUM_ENTRIES; row++ ) {
if( !isValidRow( filled_grid, row, missing_numbers_per_row[row] ) ) {
return false;
}
}
// check that no column contains duplicate numbers
QVector< QSet< int > > missing_numbers_per_column( NUM_ENTRIES );
for( int col=0; col<NUM_ENTRIES; col++ ) {
if( !isValidColumn( filled_grid, col, missing_numbers_per_column[col] ) ) {
return false;
}
}
// check that no SIDE*SIDE block contains duplicate numbers
QVector< QSet<int> > missing_numbers_per_block( NUM_ENTRIES );
for( int block=0; block<NUM_ENTRIES; block++ ) {
if( !isValidBlock( filled_grid, block, missing_numbers_per_block[block] ) ) {
return false;
}
}
for( int i=0; i<NUM_ENTRIES; i++ ) {
for( int j=0; j<NUM_ENTRIES; j++ ) {
if( filled_grid[i][j]==0 ){
int const block_number = SIDE*(i/SIDE) + (j/SIDE);
numbers_to_try[i][j] = missing_numbers_per_row.at(i);
numbers_to_try[i][j] = numbers_to_try[i][j].intersect( missing_numbers_per_column[j]);
numbers_to_try[i][j] = numbers_to_try[i][j].intersect( missing_numbers_per_block[block_number] );
if( numbers_to_try[i][j].size()==0 ) {
Logger::getInstance().log( "Cell " + QString::number( i ) + "," + QString::number( j ) + " has no solutions left!");
return false;
} else if( numbers_to_try[i][j].size()==1 ) {
addedDecision = true;
int const solved_value = *numbers_to_try[i][j].begin();
Logger::getInstance().log( "Found value " + QString::number( solved_value ) + " at cell " + QString::number( i ) + "," + QString::number( j ) );
filled_grid[i][j] = solved_value;
Decision decision(i,j,solved_value);
decisions.push_back( decision );
}
}
}
}
return true;
}
QVector< QVector< int > > Solver::getSolution()
{
return filled_grid;
}
} // namespace sudoku