C++ Programming Given the word find puzzle below, locate and print out all four
ID: 3692816 • Letter: C
Question
C++ Programming
Given the word find puzzle below, locate and print out all four letter words. The words can appear in their normal form or reversed and both horizontal or vertical. (no diagonal words) Use a two dimensional char array to hold the puzzle. Process one line at a time. And then one column at a time. (HINT: Extract the characters to form strings and use the programs you did earlier in the term to search for words.) You should have a function that determines if a sequence of letters constitutes a word. You may want to use the binary search algorithm to search the dictionary of four letter words. The following puzzle contains ten lines with ten letters in each line.
pxabackegs
esorelated
vmoonkcent
zrllablead
msrkradmcf
floortjukv
strxmicedc
qutamktsud
rxquitmmuy
datasetlas
Explanation / Answer
// COLUMNS++ programs to search a inputWord in a 2D gridPuzzle
#include<bits/stdc++.h>
using namespace std;
// gridPuzzle rows and columns
#define ROWS 10
#define COLUMNS 10
// this is to setting directions
int horizontal[] = { -1, -1, -1, 0, 0, 1, 1, 1 };
int vertical[] = { -1, 0, 1, -1, 1, -1, 0, 1 };
//binary searching for the inputWord
bool startSearchWords(char gridPuzzle[ROWS][COLUMNS], int row, int col, string inputWord)
{
// checking with first character
if (gridPuzzle[row][col] != inputWord[0])
return false;
int len = inputWord.length();
// searching for this word in all 8 directions
for (int direction = 0; direction < 8; direction++)
{
int k, rd = row + horizontal[direction], cd = col + vertical[direction];
// checking for remaining characters
for (k = 1; k < len; k++)
{
// break....out of bounds
if (rd >= ROWS || rd < 0 || cd >= COLUMNS || cd < 0)
break;
// no match..break
if (gridPuzzle[rd][cd] != inputWord[k])
break;
// chamging direction
rd += horizontal[direction], cd += vertical[direction];
}
//if word found
if (k == len)
return true;
}
return false;
}
void searchPattern(char gridPuzzle[ROWS][COLUMNS], string inputWord)
{
// searching word in input drid and if found printing position
for (int row = 0; row < ROWS; row++)
for (int col = 0; col < COLUMNS; col++)
if (startSearchWords(gridPuzzle, row, col, inputWord))
cout << "Four Letter word found at " << row << ", "
<< col << endl;
}
int main()
{
char gridPuzzle[ROWS][COLUMNS] = {"pxabackegs","esorelated","vmoonkcent","zrllablead","msrkradmcf","floortjukv","strxmicedc","qutamktsud","rxquitmmuy","datasetlas"};
searchPattern(gridPuzzle, "data");
cout << endl;
searchPattern(gridPuzzle, "sore");
return 0;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.