ONLY WORDS OF LENGTH 3 or MORE ARE TO BE OUTPUTTED Solving the game of Boggle ca
ID: 3819272 • Letter: O
Question
ONLY WORDS OF LENGTH 3 or MORE ARE TO BE OUTPUTTED
Solving the game of Boggle can be done elegantly with recursion and backtracking. Backtracking is a technique whereby an algorithm recognizes it is impossible or unnecessary to search any deeper into the search space from a given point. An example of this is finding your way through a maze. When you hit a dead end you mark that last step as a dead end and avoid going down that path. A fast and efficient solution to Boggle requires a hueristic that helps you recognize a dead end and save vast amounts of wasted searches (and thus vast amounts of time).
Boggle is a board game that uses 16 dice arranged in a 4 x 4 grid. There is also a 5x5 version. Each die has 6 faces with a letter of the alphabet on it. Occasionally the string "Qu" will appear on a dice face. A typical board might look like this.
A typical Boggle game usually starts by shaking the dice on the board thus creating a new grid of letters on the game board. The object of the game is for each player to form as many valid dictionary words as possible by connecting adjacent letters in any direction without any cycles. Think of how a King can mode an a chessboard - across, up, down or diagonal. You can generate words by connecting letters in any direction as long as you don't create cycles. Thus in a 4x4 grid you could form words a long as 16 characters (well.. 17 if you hit a "Qu" dice).
You will be surprised at home many unique strings you can generate from even a 2 x 2 grid. A 2 x 2 grid can generate 64 unique strings. A 3 x 3 can generate over 10,305 and a 4 x 4 generates over 12 million. A 5 x 5 generates over 115 billion. Larger grids are astronomical. There is no known closed from expression to calculate the number of strings that can be formed from an N x N grid. The only way to calculate that number is to generate all those strings with a program and count them as you form them.
Here is an exhaustiove list of the words that can be found in a 2x2 grid of letters:
2x2--64_WORDS.txt
Here is an exhaustiove list of the words that can be found in a 3x3 grid of letters:
3x3--10305_WORDS.txt
A Heuristic:
In order to avoid forming all possible strings to find the real (dictionary) ones, you will need a heuristic to prune the search space. You must recognize that you should not bother forming new words that start with a word you have already searched for but failed to find if the failed search word was not a prefix of any word encountered in the search.
The Assignment
Your program MUST produce exactly the same output my solution produces on each boggle input file. You are to hard code the dictionary.txt filename somewhere inside your program and always load that file as your dictionary of legal words. For this assignment there is only one dictionary file we will ever use. So, go ahead and hard code the "dictionary.txt" filename in your code. Your program should be executed as follows:
The only output is to be the real dictionary words you found in the grid. One word per line. No other output of any kind. The words must be unique, no duplicates and they must appear in sorted order ascending from top to bottom. Please see my correct output files below and make sure your solution has the same exact words as mine.
dictionary.txt 172822 words. Your reference dictionary
INPUT FILES TO RUN YOUR SOLUTION ON
A good solution will find all the dictionary words in the grid in under 1/4 of a second from the time the program started running till it printed out the last valid word to the screen.
4x4-board.txt all real dictionary words you should find -> 4x4-board-output.txt
5x5-board.txt all real dictionary words you should find -> 5x5-board-output.txt
10x10-board.txt all real dictionary words you should find -> 10x10-board-output.txt
O E R S R S H C H Qu S A No L TExplanation / Answer
//BOGGLE GAME(FINDING ALL WORDS PRESENT IN THE BOARD)
#include<iostream>
#include<cstring>
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 givem 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] = {{'O','E','R','S'},
{'R','S','H','C'},
{'H','QU','S','A'},
{'N','O','L','T'}
};
cout << "Following words of dictionary are present ";
findWords(boggle);
return 0;
}
#include<iostream>
#include<cstring>
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 givem 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] = {{'O','E','R','S'},
{'R','S','H','C'},
{'H','QU','S','A'},
{'N','O','L','T'}
};
cout << "Following words of dictionary are present ";
findWords(boggle);
return 0;
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.