Write a function USING MATLAB that helps solve word search puzzles. Your functio
ID: 3765692 • Letter: W
Question
Write a function USING MATLAB that helps solve word search puzzles. Your function should search a 2-dimensional array of characters to find a specified word. If the word is found, the function should return the indices of the first character of the word and the direction of the word in the puzzle (e.g. across, down, diagonals, and their reverses). If the word is not found, the function should return values indicating that fact, such as -1 for all return values.
Example: Suppose you are given the following puzzle
Command window should look like this
>> [row,column,direction] = wordsearch(puzzle,’algorithm’)
row = 1
column = 4
direction =
across (left to right)
Explanation / Answer
a word search puzzle (not a crossword puzzle) I'd have at least 4 nested for loops. One over rows, one over columns, and one over angles. For each character, for each angle, extract a complete line through every place in the array that it can. Like horizontal, vertical, and two diagonals. Then on those 4 strings, use strfind() to search it for all unused words.
You do not need 4 nested loops. You only need unnested loops.
For any one extracted vector of characters, you can use strfind() of both the forward word and the reverse word. If there is a match then your knowledge of the section you are examining together with the direction will allow you to find the index of the first character; return it and the direction.
If you want to get fancier or more efficient, pull out a "zig-zag scan" routine from the File Exchange and use it against the character array padded on all sides with a character known not to appear in the search string (could even be space). You can search the resulting character vector of all the diagonal elements with a single strfind() (and search for the reversed word with a second strfind() call). The rest is just book-keeping about converting the indices located to original matrix positions.
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.