We have discussed recursion and in particular backtracking algorithms (such as Eight Queens Problem). In this assignment you will get some practice at recursive programming by writing a backtracking algorithm to find phrases in a puzzle.
"Word search" puzzles are common in newspapers, on restaurant placemats, and even in their own collections within books. These vary in format from puzzle to puzzle, but one common format is as follows: A user is presented with a two-dimensional grid of letters and a list of words. The user must find all of the words from the list within the grid, indicating where they are by drawing ovals around them. Words may be formed in any direction: up, down, left or right (we will not allow diagonals even though most puzzles do allow them) but all of the letters in a word must be in a straight line.
This idea can be extended to allow phrases to be embedded in the grids. However, requiring an entire phrase to be in a straight line on the board is not practical, as the dimensions of the board would need to be overly large. Therefore, we will still require the letters in each word to be in a straight line, but we are allowed to change direction between words.
For example, we might be given the following 10x10 grid of letters:
a b s t r a c t i j a t a d t d a t a j t b c d c a g h t j y b c d a t g h y j p b c d r a g h p j e b c d t f t h e j s a r e s f a h s j a r e d b f e h a j r b c d a f n h r j e r e a l l y r e j
and the following phrases:
abstract
abstract data types
abstract data types are really neat
abstract data types are really great
Assume the grid starts in the upper left corner with position (0,0), and that the coordinates are (row, column). Searching would yield:
abstract
found in two places:
abstract data types
found in two places:
abstract data types are really neat
found at:
abstract data types are really great
not foundYour task is to write a Java program to read in a grid of letters from a file, and then interactively allow a user to enter phrases until he or she wants to quit. For each phrase your program must output whether or not the phrase is found and, if found, specifically where it is located.
The grid of letters for your program will be stored in a text file formatted as follows:
R
) and columns, C
, in the grid.R
lines containing C
lower case characters eachFor example:
The user input will be phrases of words, with a single space between each word and no punctuation. Each phrase will be entered on a single line. The user may enter either upper or lower case letters, but the string should be converted to lower case before searching the grid. The program will end when the user enters no data for the current phrase (i.e. hits <Enter> without typing any characters beforehand). For example, user input could look like:
too
DO
at too
so DOO
That last example is the user just pressing enter.
If a phrase is not found in the grid, the output should simply state that.
If a phrase is found in the grid, your program must find one occurrence of the phrase, and the output must indicate this fact in two ways:
For example, for the 10x10 grid above and the phrase "abstract data types
" your output would be:
Your search algorithm must be a recursive, backtracking algorithm. Note that you do not need recursion to match the letters within individual words (although you may do this recursively if you prefer). Where the recursion is necessary is when moving from one word to the next, since it is here where you may change direction. To make the program more consistent (and easier to grade), we will have the following requirements for the recursive process:
abstract data
", this is not a valid solution: [(8,4) to (1,4)], [(1,5) to (4,5)]. This solution is not legal because we proceeded right from the "T" of "abstract" to find the "D" in "data", but then proceeded down to find the remaining letters in "data".abstract data types are
", this is not a valid solution: [(8,4) to (1,4)], [(1,3) to (1,0)], [(2,0) to (6,0)], [(7,0) to (7,2)]. This solution is not legal because we proceeded down from the "S" of "types" to find the "A" in "are", but then proceeded right to find the remaining letters in "are".The 10x10 grid shown above is available here: sample.txt. Test it with the phrases above; your results should match the results shown in file sample-out.txt.
There are two opportunities for extra credit. You may do one or both (or neither).
Complete the Assignment Information Sheet.
Make sure you submit all of the following in a single .zip file to get full credit. Name your main program Assign3.java.
As with Assignment 1 and 2, the idea for your submission is that your TA can compile and run your programs without any additional files, so be sure to test them thoroughly before submitting them. If you use an IDE for development, make sure your program runs from the command line without the IDE before submitting it. If you cannot get the program working as specified, clearly indicate any changes you made and clearly indicate why, so that the TA can best give you partial credit.
Submit your final zip file to CourseWeb in the Assignment 3 folder.
The grading rubric can be found here: Rubric (doc).
The assignment is due Thursday, October 29 by 11:59 pm. As with all programming assignments, you have unlimited uploads (before the deadline), so you may upload the assignment before the deadline. If you later decide to upload another, you may do so without penalty (as long as it's before the assignment deadline). The last submission uploaded will be the one graded.
If you would like ungraded feedback on a programming assignment, you may send an email to your TA or the instructor and ask for feedback; please send your code as well. If your question is basically "Are there any problems with my program?" or "Can you check my code?" tell us what you've already done to test your program; provide the output from the test runs of your program.
For more advice on submitting your assignment, see the Programming Assignments section of the Tips for Success page.