-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBoggle.cpp
More file actions
296 lines (250 loc) · 6.47 KB
/
Boggle.cpp
File metadata and controls
296 lines (250 loc) · 6.47 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
#include <iostream>
#include <vector>
#include <string>
#include <unordered_set>
#include <unordered_map>
// QUESTION: https://www.geeksforgeeks.org/boggle-find-possible-words-board-characters/ (DFS based solution)
// https://www.geeksforgeeks.org/boggle-set-2-using-trie/
/**
* Using a memory efficient trie. Instead of storing an array of pointers to alphabets
* in every node, we store the subsequent characters in a hash map => memory efficient. The process
* of finding words in the board is different here:
*
* 1) Build a trie based on all strings in the dictionary
* 2) For each character in the board, we need to know if that is a starting alphabet to any word
* in the dictionary. For this, the trie root must point to a map for the character. If this entry
* is null, we can discard the character in the board for being the starting alphabet
* 3) If the root has an entry, pass that entry into a recursive helper function that will find all subsequent
* alphabets. Here again, we look at 8 different positions from the board. The next move is safe if the
* coordinate is valid, it is not visited AND the current trie has an entry for the next character. Instead
* of generating all moves from the board, we iterate over all entries in the current trie's map and check to see
* if moving in any of the 8 directions generates a potential subsequent character
* 4) We need to keep track of visited nodes
* 5) We have found a word if the trie passed to the recursive function is a leaf
* 6) To avoid duplicate strings which are found, we store the results in an unordered_set
*/
class Trie
{
private:
std::unordered_map<char, Trie*> character;
bool isleaf;
private:
void deleteHelper(Trie*& curr)
{
for (auto& iter : curr->character)
{
Trie* toDel = iter.second;
char c = iter.first;
deleteHelper(toDel);
if (toDel->isleaf || !hasChildren(toDel))
{
std::cout << "Destroying " << c << std::endl;
delete toDel;
toDel = nullptr;
}
}
curr->character.clear();
}
public:
Trie()
: isleaf(false)
{
}
~Trie()
{
for (auto& iter : character)
{
Trie* temp = iter.second;
deleteHelper(temp);
std::cout << "Destroying " << iter.first << std::endl;
delete temp;
temp = nullptr;
}
}
void insert(std::string str)
{
Trie* curr = this;
for (int i = 0; i < str.length(); ++i)
{
if (curr->character.find(str[i]) == curr->character.end())
{
std::cout << "Inserting " << str[i] << std::endl;
curr->character[str[i]] = new Trie();
}
curr = curr->character[str[i]];
}
curr->isleaf = true;
}
bool hasChildren(Trie* curr)
{
return !curr->character.empty();
}
bool hasEntry(char c)
{
return character.find(c) != character.end();
}
Trie* getTrie(char c)
{
return character[c];
}
bool isLeaf() const
{
return isleaf;
}
const std::unordered_map<char, Trie*> getMap() const
{
return character;
}
};
class MemoryEfficientBoggle
{
private:
Trie root;
const std::vector<std::vector<char>>& board;
const std::vector<std::string>& dictionary;
std::unordered_set<std::string> foundwords;
std::vector<std::vector<bool>> visited;
std::vector<int> xcoords;
std::vector<int> ycoords;
int rows;
int cols;
private:
bool isSafe(int x, int y, char c)
{
return x >= 0 && x < rows && y >= 0 && y < cols && !visited[x][y] && board[x][y] == c;
}
void searchBoard(Trie* curr, int x, int y, std::string str)
{
visited[x][y] = true;
str += board[x][y];
if (curr->isLeaf())
{
foundwords.insert(str);
}
for (auto& iter : curr->getMap())
{
char ch = iter.first;
Trie* adjmap = iter.second;
for (int i = 0; i < xcoords.size(); ++i)
{
int newx = x + xcoords[i];
int newy = y + ycoords[i];
if (isSafe(newx, newy, ch))
searchBoard(adjmap, newx, newy, str);
}
}
visited[x][y] = false;
}
public:
MemoryEfficientBoggle(const std::vector<std::vector<char>>& b, const std::vector<std::string>& d)
: root(Trie()), board(b), dictionary(d)
{
rows = b.size();
cols = b[0].size();
xcoords = { 1, -1, 0, 0, 1, 1, -1, -1 };
ycoords = { 0, 0, 1, -1, 1, -1, 1, -1 };
visited = std::vector<std::vector<bool>>(rows, std::vector<bool>(cols, false));
// Populate trie
for (auto& iter : dictionary)
{
root.insert(iter);
}
}
void find()
{
for (int i = 0; i < rows; ++i) {
for (int j = 0; j < cols; ++j) {
if (root.hasEntry(board[i][j]))
{
std::string str;
searchBoard(root.getTrie(board[i][j]), i, j, str);
}
}
}
std::cout << "Words in dictionary(using trie) are" << std::endl;
for (auto iter : foundwords)
{
std::cout << iter << std::endl;
}
}
};
class WordFinder
{
private:
const std::vector<std::vector<char>>& board;
const std::vector<std::string>& dictionary;
std::unordered_set<std::string> foundwords;
std::vector<std::vector<bool>> visited;
std::vector<int> xcoords;
std::vector<int> ycoords;
int rows;
int cols;
private:
bool isSafe(int x, int y)
{
return x >= 0 && x < rows && y >= 0 && y < cols && !visited[x][y];
}
bool isInDictionary(const std::string& str)
{
for (auto iter : dictionary)
{
if (strcmp(iter.c_str(), str.c_str()) == 0)
return true;
}
return false;
}
void findHelper(int x, int y, std::string str)
{
visited[x][y] = true;
str += board[x][y];
foundwords.insert(str);
for (size_t i = 0; i < xcoords.size(); ++i)
{
int newx = x + xcoords[i];
int newy = y + ycoords[i];
if (isSafe(newx, newy))
findHelper(newx, newy, str);
}
visited[x][y] = false;
}
public:
WordFinder(const std::vector<std::vector<char>>& b, const std::vector<std::string>& d)
: board(b), dictionary(d)
{
rows = b.size();
cols = b[0].size();
xcoords = {1, -1, 0, 0, 1, 1, -1, -1};
ycoords = {0, 0, 1, -1, 1, -1, 1, -1};
visited = std::vector<std::vector<bool>>(rows, std::vector<bool>(cols, false));
}
void find()
{
std::string str;
for (int i = 0; i < board.size(); ++i) {
for (int j = 0; j < board[0].size(); ++j) {
findHelper(i, j, str);
}
}
std::cout << "Words in dictionary are" << std::endl;
for (auto iter : foundwords)
{
if (isInDictionary(iter))
std::cout << iter << std::endl;
}
}
};
int main()
{
std::vector<std::vector<char>> board
{
{'M', 'S', 'E', 'F'},
{'R', 'A', 'T', 'D'},
{'L', 'O', 'N', 'E'}
};
std::vector<std::string> dictionary{"START", "NOTE", "SAND", "STONED"};
WordFinder obj1(board, dictionary);
obj1.find();
MemoryEfficientBoggle obj2(board, dictionary);
obj2.find();
return 0;
}