PLEASE READ MY POST AND QUESTION BEFORE POSTING. ONLY C++ NOT JAVA. Also I need this program to run and output all valid words. Program NEEDS TO ATTACH TEXT FILE WORDS.TXT, that part of the question. You can make up a word.txt with 10 random words to test program, but please ADD WORDS.TXT. Thank you.
Solved 1 Answer
See More Answers for FREE
Enhance your learning with StudyX
Receive support from our dedicated community users and experts
See up to 20 answers per week for free
Experience reliable customer service
Here is your answer Run the code below   // C++ program for Boggle game #include <cstring> #include <iostream> using namespace std;    #define M 3 #define N 3    // Let the given dictionary be following string dictionary[] = { "GEEKS", "FOR", "QUIZ", "GO" }; int n = sizeof(dictionary) / sizeof(dictionary[0]);    // A given function to check if a given string is present in // dictionary. The implementation is naive for simplicity. As // per the question dictionary is given to us. bool isWord(string& str) {     // Linearly search all words     for (int i = 0; i < n; i++)         if (str.compare(dictionary[i]) == 0)             return true;     return false; }    // A recursive function to print all words present on boggle void findWordsUtil(char boggle[M][N], bool visited[M][N], int i,                    int j, string& str) {     // Mark current cell as visited and append current character     // to str     visited[i][j] = true;     str = str + boggle[i][j];        // If str is present in dictionary, then print it     if (isWord(str))         cout << str << endl;        // Traverse 8 adjacent cells of boggle[i][j]     for (int row = i - 1; row <= i + 1 && row < M; row++)         for (int col = j - 1; col <= j + 1 && col < N; col++)             if (row >= 0 && col >= 0 && !visited[row][col])                 findWordsUtil(boggle, visited, row, col, str);        // Erase current character from string and mark visited     // of current cell as false     str.erase(str.length() - 1);     visited[i][j] = false; }    // Prints all words present in dictionary. void findWords(char boggle[M][N]) {     // Mark all characters as not visited     bool visited[M][N] = { { false } };        // Initialize current string     string str = "";        // Consider every character and look for all words     // starting with this character     for (int i = 0; i < M; i++)         for (int j = 0; j < N; j++)             findWordsUtil(boggle, visited, i, j, str); }    // Driver program to test above function int main() {     char boggle[M][N] = { { 'G', 'I', 'Z' },                           { 'U', 'E', 'K' },                           { 'Q', 'S', 'E' } };        cout << "Following words of dictionary are presentn";     findWords(boggle);     return 0; }       Note that the above solution may print the same word multiple times. For example, if we add “SEEK” to the dictionary, it is printed multiple times. To avoid this, we can use hashing to keep track of all printed words. In below set 2, I changed this Trie based optimized solution:       // 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; } tHANK YOU! ...