forked from Ayushsinhahaha/HacktoberFest
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathboogle.cpp
172 lines (145 loc) · 4.22 KB
/
boogle.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
// C++ program for Boggle game
#include <bits/stdc++.h>
using namespace std;
// Converts key current character into index
// use only 'A' through 'Z'
#define char_int(c) ((int)c - (int)'A')
// Alphabet size
#define SIZE (26)
#define M 3
#define N 3
// trie Node
struct TrieNode {
TrieNode* Child[SIZE];
// isLeaf is true if the node represents
// end of a word
bool leaf;
};
// Returns new trie node (initialized to NULLs)
TrieNode* getNode()
{
TrieNode* newNode = new TrieNode;
newNode->leaf = false;
for (int i = 0; i < SIZE; i++)
newNode->Child[i] = NULL;
return newNode;
}
// If not present, inserts a key into the trie
// If the key is a prefix of trie node, just
// marks leaf node
void insert(TrieNode* root, char* Key)
{
int n = strlen(Key);
TrieNode* pChild = root;
for (int i = 0; i < n; i++) {
int index = char_int(Key[i]);
if (pChild->Child[index] == NULL)
pChild->Child[index] = getNode();
pChild = pChild->Child[index];
}
// make last node as leaf node
pChild->leaf = true;
}
// function to check that current location
// (i and j) is in matrix range
bool isSafe(int i, int j, bool visited[M][N])
{
return (i >= 0 && i < M && j >= 0 && j < N && !visited[i][j]);
}
// A recursive function to print all words present on boggle
void searchWord(TrieNode* root, char boggle[M][N], int i,
int j, bool visited[][N], string str)
{
// if we found word in trie / dictionary
if (root->leaf == true)
cout << str << endl;
// If both I and j in range and we visited
// that element of matrix first time
if (isSafe(i, j, visited)) {
// make it visited
visited[i][j] = true;
// traverse all childs of current root
for (int K = 0; K < SIZE; K++) {
if (root->Child[K] != NULL) {
// current character
char ch = (char)K + (char)'A';
// Recursively search reaming character of word
// in trie for all 8 adjacent cells of boggle[i][j]
if (isSafe(i + 1, j + 1, visited)
&& boggle[i + 1][j + 1] == ch)
searchWord(root->Child[K], boggle,
i + 1, j + 1, visited, str + ch);
if (isSafe(i, j + 1, visited)
&& boggle[i][j + 1] == ch)
searchWord(root->Child[K], boggle,
i, j + 1, visited, str + ch);
if (isSafe(i - 1, j + 1, visited)
&& boggle[i - 1][j + 1] == ch)
searchWord(root->Child[K], boggle,
i - 1, j + 1, visited, str + ch);
if (isSafe(i + 1, j, visited)
&& boggle[i + 1][j] == ch)
searchWord(root->Child[K], boggle,
i + 1, j, visited, str + ch);
if (isSafe(i + 1, j - 1, visited)
&& boggle[i + 1][j - 1] == ch)
searchWord(root->Child[K], boggle,
i + 1, j - 1, visited, str + ch);
if (isSafe(i, j - 1, visited)
&& boggle[i][j - 1] == ch)
searchWord(root->Child[K], boggle,
i, j - 1, visited, str + ch);
if (isSafe(i - 1, j - 1, visited)
&& boggle[i - 1][j - 1] == ch)
searchWord(root->Child[K], boggle,
i - 1, j - 1, visited, str + ch);
if (isSafe(i - 1, j, visited)
&& boggle[i - 1][j] == ch)
searchWord(root->Child[K], boggle,
i - 1, j, visited, str + ch);
}
}
// make current element unvisited
visited[i][j] = false;
}
}
// Prints all words present in dictionary.
void findWords(char boggle[M][N], TrieNode* root)
{
// Mark all characters as not visited
bool visited[M][N];
memset(visited, false, sizeof(visited));
TrieNode* pChild = root;
string str = "";
// traverse all matrix elements
for (int i = 0; i < M; i++) {
for (int j = 0; j < N; j++) {
// we start searching for word in dictionary
// if we found a character which is child
// of Trie root
if (pChild->Child[char_int(boggle[i][j])]) {
str = str + boggle[i][j];
searchWord(pChild->Child[char_int(boggle[i][j])],
boggle, i, j, visited, str);
str = "";
}
}
}
}
// Driver program to test above function
int main()
{
// Let the given dictionary be following
char* dictionary[] = { "GEEKS", "FOR", "QUIZ", "GEE" };
// root Node of trie
TrieNode* root = getNode();
// insert all words of dictionary into trie
int n = sizeof(dictionary) / sizeof(dictionary[0]);
for (int i = 0; i < n; i++)
insert(root, dictionary[i]);
char boggle[M][N] = { { 'G', 'I', 'Z' },
{ 'U', 'E', 'K' },
{ 'Q', 'S', 'E' } };
findWords(boggle, root);
return 0;
}