-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathboggle_solver.cpp
More file actions
359 lines (281 loc) · 9.14 KB
/
boggle_solver.cpp
File metadata and controls
359 lines (281 loc) · 9.14 KB
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
/*
FAST C++ BOGGLE SOLVER
Copyright 2012 Will Jensen
https://github.com/themachineswillwin/fast-boggle-solver
machineboy2045@gmail.com
This file is part of Fast C++ Boggle Solver.
Fast C++ Boggle Solver is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
Fast C++ Boggle Solver is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with Fast C++ Boggle Solver. If not, see <http://www.gnu.org/licenses/>.
*/
#include <iostream>
#include <stdlib.h>
#include <algorithm>
#include <stdio.h>
#include <math.h>
#include <time.h>
#include <string.h>
#include <vector>
#include <locale>
using namespace std;
const int NUM_BRANCHES = 8; //max number of neighboring cubes
const int ALPHA_SIZE = 26; //number of letters in our alphabet
const int WSIZE = 20; //longest word read in dictionary
const int BORDER = ALPHA_SIZE; //surrounds the Boggle puzzle. Should be 1 > than last int in alphabet
/*
Dict is a tree (trie?).
For any prefix, it can tell you which letters
follow it and how many suffixes (children) it has.
Lets say you have a two letter prefix "AN".
If you want to know what letters could follow this prefix,
you would look at dict['A']->children['N']->children.
*/
class Trie{
public:
Trie* children[ALPHA_SIZE];
Trie* parent;
int count; //number of suffixes that share this as a root
char* word; //if this node completes a word, store it here
bool found; //has this word been found?
Trie();
};
Trie::Trie(){
count = 0;
word = NULL;
found = false;
int i;
for(i = 0; i < ALPHA_SIZE; i++){
children[i] = NULL;
}
}
Trie* dict;
int duplicates = 0;
int wordsParsed = 0;
int checkedNodes = 0;
int progress = 0;
int onePercentage;
char longestWord[WSIZE+1];
clock_t begin; //used to time search duration
int * board;
int board_size;
int puzzle_size;
int cols;
int * SEARCHED;
int * children;
vector<char*> found;
char* readF( char fname[] ){
FILE * pFile;
long lSize;
char * buffer;
size_t result;
pFile = fopen ( fname , "r" );
if (pFile==NULL) {fputs ("File error",stderr); exit (1);}
// obtain file size:
fseek (pFile , 0 , SEEK_END);
lSize = ftell (pFile);
rewind (pFile);
// allocate memory to contain the whole file:
buffer = (char*) malloc (sizeof(char)*lSize);
if (buffer == NULL) {fputs ("Memory error",stderr); exit (2);}
// copy the file into the buffer:
result = fread (buffer,1,lSize,pFile);
if (result != lSize) {fputs ("Reading error",stderr); exit (3);}
/* the whole file is now loaded in the memory buffer. */
// terminate
fclose (pFile);
// free (buffer);
return buffer;
}
void saveResults( char fname[] ){
FILE * file;
file = fopen (fname,"w");
//pretty print the board
char c;
for(int i = 0; i < board_size; i++){
c = (char) (board[i] + 'a');
if( 'q' == c ){
fputs("Qu",file);
}else if( BORDER == board[i] ){
fputs("* ",file);
}else{
fputc(c,file);
fputs(" ",file);
}
if( (i+1) % cols == 0 ) fputs("\n",file);
}
fputs("\n",file);
//print found words list
sort(found.begin(), found.end());
for(vector<char*>::iterator it = found.begin(); it != found.end(); ++it) {
fputs (*it,file);
fputs ("\n",file);
}
fclose(file);
}
void statusBar(int i){
if( (cols-2) < 100 )
return;
i += 1; //compensate for array index
if( i - progress == onePercentage ){
cout << "|" << flush;
progress += onePercentage;
}
if( i == puzzle_size ) cout << " done!" << endl;
}
//after searching the board...
//traverse the trie and push all found words into vector 'found'
//Recursive!
void getFoundWords(Trie* p){
int i;
if( p ){
if( p->found )
found.push_back( p->word );
for(i = 0; i < ALPHA_SIZE; i++){
getFoundWords( p->children[i] );
}
}
}
void * buildBoard( char boggleFile[] ){
char * buffer = readF( boggleFile );
//calculate the dimensions
int len = strlen( buffer );
cols = sqrt( len ) + 2;
children = new int[NUM_BRANCHES] {-1-cols, -cols, 1-cols, -1, 1, cols-1, cols, cols+1};
board_size = cols * cols;
puzzle_size = (cols-2) * (cols-2);
onePercentage = double(1) / 100 * puzzle_size;
board = new int[board_size];
//add border
int j = 0;
for(int i = 0; i < board_size; i++){
if( (i < cols) || //top
((i+1) % cols == 0) || //right
(i > cols * (cols -1)) || //bot
(i % cols == 0) ){ //left
board[i] = BORDER;
}else{
board[i] = buffer[j] - 'a';
j++;
}
}
}
//add word to Trie
void insertWord( char word[], const int len ){
Trie* p = dict;
int i;
for(i = 0; word[i]; i++){
int letter = word[i] - 'a'; //convert characters to ints: a=0 z=25
//combine 'qu' into single cube represented by 'q'
if( ('q' == word[i]) && ('u' == word[i+1]) )
i++;
p->count++; //track how many words use this prefix
if( !p->children[letter] )
p->children[letter] = new Trie;
p->children[letter]->parent = p;
p = p->children[letter];
}
p->word = word; //the last node completes the word, save it here
}
// load dictionary into trie
void buildTrie( char dictFile[] )
{
char * buffer = readF( dictFile );
char * word;
int len, i;
dict = new Trie;
word = strtok(buffer,"\n\t");
while (word != NULL) {
len = strlen( word );
if( len >= 3 ){ //per Boggle rules
insertWord( word, len );
wordsParsed++;
}
word = strtok (NULL, "\n\t");
}
}
//returns a Trie* to the next prefix or NULL
//keeps track of which words have been found
inline Trie* lookup(const int i, Trie* p){
p = p->children[i];
if( p && p->word ){
if( p->found ){
duplicates++;
}else{
p->found = true;
p->parent->count--; //decrement how many words are left that use this prefix
}
}
return p;
}
//depth first search. recursive!
//returns the number of NEW words found in children + self
//Words that have already been found do not count
inline bool descend(int cubeIndex, Trie* p, vector<bool> searched){
++checkedNodes;
p = lookup( board[cubeIndex], p);
if( p && p->count ){ //is this a valid prefix? Are there any remaining words that use it?
searched[cubeIndex] = true; //mark this cube as used
for(int i = 0; i < NUM_BRANCHES; i++){ //descend to each neighboring cube
int child = cubeIndex + children[i];
if((board[child] != BORDER) && !searched[child]) //faster to check here
descend(child, p, searched);
}
}
}
//for each cube on the board, perform a depth first search using descend()
void traverseBoard(){
Trie* p = dict;
vector<bool> searched; //cubes should be used only once per word
int i, j = 0;
//initialize searched to false for all cubes on the board
for(int i = 0; i < board_size; i++) searched.push_back(false);
for(int i = 0; i < board_size; i++){ //for each cube
if(board[i] != BORDER){
descend(i, p, searched); //DFS
statusBar(j);
++j;
}
}
}
int main(int argc, char* argv[]){
char boggleFile[100] = "boggle.txt";
char dictFile[100] = "mydictionary.txt";
char resultsFile[100] = "results.txt";
cout.imbue(std::locale("")); //adds commas to large numbers
if( argc > 1 )
strcpy(boggleFile, argv[1]);
if( argc > 2 )
strcpy(dictFile, argv[2]);
buildBoard( boggleFile );
cout << "================================================" << endl;
buildTrie( dictFile );
cout << wordsParsed << " words parsed in " << dictFile << endl;
cout << "Word length limit of " << WSIZE << " characters" << endl;
cout << puzzle_size << " cubes on the board" << endl;
for(int i = 0; i < 100; i++){
cout << ".";
}
cout << " 100%" << endl;
begin = clock();
traverseBoard();
getFoundWords( dict );
double end = double(clock() - begin) / CLOCKS_PER_SEC;
double speed = 0;
if( end )
speed = floor((checkedNodes/end)/1000);
cout << found.size() << " words found in "
<< end << " seconds" << endl
<< (checkedNodes/puzzle_size) << " nodes checked per cube" << endl
<< "~" << speed << " nodes checked per millisecond" << endl
<< duplicates << " duplicate words found" << endl;
saveResults( resultsFile);
cout << "Results saved to " << resultsFile << endl;
cout << "================================================" << endl << endl;
}