-
Notifications
You must be signed in to change notification settings - Fork 51
/
solver.cpp
295 lines (239 loc) · 7.26 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
/*
Matt Olson
Alex Izydorczyk
Implementation of backtracking algorithm and other game-play mechanics
*/
#include "solver.hpp"
#include <iostream>
#include <cassert>
#include <math.h>
#include <cstdlib>
#include <ctime>
using namespace std;
// Function to print the board
void Board::printPuzzle() {
int blockSize = (int)sqrt(N);
int extra_space = N / 10;
for(int i = 0; i < N; i++){
cout << "+---";
for(int j = 0; j < extra_space; j++)
cout << "-";
}
cout << "+" << endl;
for(int i = 0; i < N; i++){
cout << "| ";
for(int j = 0; j < N; j++){
if ((*this)(i,j) == 0){
cout << ".";
for(int k = 0; k < extra_space; k++)
cout << " ";
} else {
if (!isProblem(i,j)){
cout << (*this)(i,j);
} else {
cout << "\033[31m" << (*this)(i,j) << "\033[39m";
}
int numDigits = (*this)(i,j)/10;
while (numDigits < extra_space){
cout << " ";
numDigits++;
}
}
if ((j+1) % blockSize == 0){
cout << " | ";
} else {
cout << " ";
}
}
cout << endl;
if ((i+1) % blockSize == 0){
for(int j = 0; j < N; j++){
cout << "+---";
for(int k = 0; k < extra_space; k++)
cout << "-";
}
cout << "+" << endl;
}
}
}
// Check if the puzzle is complete
bool Board::checkPuzzle(){
int val = 0;
for(int i = 0; i < N; i++)
for(int j = 0; j < N; j++){
val = (*this)(i,j);
(*this)(i,j) = 0;
if(!feasible(*this, i, j, val)){
(*this)(i,j) = val;
return false;
}
(*this)(i,j) = val;
}
return true;
}
//Empty all cells that are not immutable
// immutable = prefilled by the randomly generated puzzle
void Board::clearPuzzle(){
for(int i = 0; i < N; i++)
for(int j = 0; j < N; j++)
if (!checkImmutable(i,j))
(*this)(i,j) = 0;
}
//Check if value is within size range of puzzle
//i.e. 1-9 on a 9x9 puzzle
bool Board::inBounds(int val){
if ((val > 0) && (val <= N)){
return true;
} else {
return false;
}
}
//Checks for feasibility of the board
//Not useful to solvers because can't return as soon as problem exits
//Needs to cycle through whole board to update problem cells
//so that we can return red to the user....
bool Board::feasibleUser(int row, int col, int val){
int blockSize = (int)sqrt(N);
if (row >= N){
std::cout << "You can't play off the game board!" << endl;
return false;
}
if (col >= N){
std::cout << "You can't play off the game board!" << endl;
return false;
}
bool isfeasible = true;
for(int i = 0; i < N; i++){
if((*this)(row,i) == val){
infeasible[row][i] = true;
isfeasible = false;
} else {
infeasible[row][i] = false;
}
}
for(int i = 0; i < N; i++){
if((*this)(i,col) == val){
infeasible[i][col] = true;
isfeasible = false;
} else {
infeasible[i][col] = false;
}
}
int blockRow = blockSize*(row/blockSize);
int blockCol = blockSize*(col/blockSize);
// // See if used yet in block
for(int i = 0; i < blockSize; i++){
for(int j = 0; j < blockSize; j++){
if((*this)(blockRow + i,blockCol + j) == val){
infeasible[blockRow + i][blockCol + j] = true;
isfeasible = false;
} else {
infeasible[blockRow + i][blockCol + j] = false;
}
}
}
return isfeasible;
}
// Helper function for solve: checks to see if candidate is feasible or not
bool feasible(Board &board, int row, int col, int val){
int N = board.getSize();
assert(row < N);
assert(col < N);
int blockSize = (int)sqrt(N);
// See if used yet in row
for(int j = 0; j < N; j++)
if(board(row,j) == val) return false;
// See if used yet in col
for(int i = 0; i < N; i++)
if(board(i,col) == val) return false;
// coordinates of upper-left hand corner of block that (row,col) occupies
int blockRow = blockSize*(row/blockSize);
int blockCol = blockSize*(col/blockSize);
// See if used yet in block
for(int i = 0; i < blockSize; i++)
for(int j = 0; j < blockSize; j++)
if(board(blockRow + i,blockCol + j) == val)
return false;
return true;
}
// Backtracking algorithm
// An outline of the algorithm was found on the following website
// (implementation is my own): http://moritz.faui2k3.org/en/yasss
bool solve(Board &board, int row, int col){
// N: size of the board; note N must be a perfect square!
int N = board.getSize();
assert(N == pow(sqrt(N),2));
// Check to see if we are at end of board
if(row == N)
return true;
// Skip over values that have been filled in
if(board(row,col) != 0){
if(col == (N-1)){
if(solve(board, row+1, 0)) return true;
} else {
if(solve(board, row, col+1)) return true;
}
return false;
}
// Try different values
for(int val = 1; val <= N; val++){
if(feasible(board, row, col, val)){
board(row,col) = val;
if(col == (N-1)){
if(solve(board, row+1, 0)) return true;
} else {
if(solve(board, row, col+1)) return true;
}
}
}
// We failed to find a value that works, so backtrack
board(row,col) = 0;
return false;
}
// Generate board to solve (only generates N! possible boards, could easily
// be extended to get more, but this is simple enough for now)
Board generatePuzzle(int N, int nobs){
// generate permutation of 1...n
// fill diagonal of board with this permutation
// solve board
// randomly remove enough entries to only leave nobs observed
assert(nobs <= N*N);
Board board(N);
int* perm = genPerm(N); // permuted 1...N
// fill diag of board with perm
for(int i = 0; i<N;i++)
board(i,i) = perm[i];
delete [] perm;
// solve board
bool isSolved = solve(board,0,0);
assert(isSolved); // by filling diagonal, this should never be violated
// remove N*N - nobs entries
perm = genPerm(N*N);
int x, y;
for(int i = 0; i < (N*N - nobs); i++){
x = (perm[i]-1)/N;
y = (perm[i]-1)%N;
board(x,y) = 0;
board.assignImmutable(x,y, false);
}
delete [] perm;
return board;
}
// http://www.cs.berkeley.edu/~jfc/cs174/lecs/lec2/lec2.pdf
// function to return a random permutation of integers 0,..,(N-1)
int* genPerm(int N){
// initialize array [1,...,N]
int *x = new int[N];
for(int i = 0; i < N; i++)
x[i] = i+1;
// generate random permutation of [1,...,N]
int rindex;
int temp;
for(int i = (N-1); i > 0; i--){
rindex = rand()%(i+1);
temp = x[i];
x[i] = x[rindex];
x[rindex] = temp;
}
return x;
}