COS 226 Programming Assignment 4

Word searching

Write a program to solve word searching puzzles, where words from a dictionary are to be found in a two-dimensional array of letters. For example, you might be given the following array:
                     m v j l i x a p e
                     j h b x e e n p p
                     h k t t h b s w y
                     r w a i n u y z h
                     p p f x r d z k q
                     t p n l q o y j y
                     a n h a p f g b g
                     h x m s h w y l y
                     u j f j h r s o a
Your program is to find words from the dictionary that can be spelled out in the puzzle by starting at any character, then moving in a straight line up, down, left, right, or on any of the four diagonals. For example, the above array contains the word algorithm because it can be spelled out by starting the lower right corner and moving up and to the left; and it contains the word syzygy because it can be spelled out by starting at the s on the third row and moving down.

The purpose of this assignment is to give you more experience working with strings and to give you an opportunity to put to work the algorithms design skills that you are learning.

For consistency, consider only words that are four or more letters long, and use the following input format: from standard input, read the integer N, then N lines of N characters each containing the character array, then the dictionary, with words separated so that successive calls to scanf("\%s"...) read successive words. For example, if you have a file mysquare with an 8-by-8 puzzle, then the command (echo "8"; cat mysquare; cat /usr/dict/words) | a.out should print out the words in the dictionary that are in the puzzle.

We have covered a number of different algorithms that can be used effectively to solve this problem. Your task is to study the problem; come up with a strategy for solving it using some of the algorithms and data structures that you have learned; implement and test your solution; and then explain and defend your design in the writeup. A good writeup is particularly important for this assignment, because we expect to see a variety of solution strategies. You need to clearly explain not just what you did, but also why you did it, backed up with estimates of resource costs. Also, you should clearly explain whatever limitations you need to place on N and on the dictionary size, and stick to our usual running time constraint of 10 seconds or so. As usual, your goal is to find a decent method which works within reasonable bounds. You need not waste effort squeezing time and space out of a bad algorithm or implementing complex or highly tuned code. This problem can be solved with 100 or so lines of code.

Hints. You can write and debug the scaffolding that you will need to read and write the character array, read and write the dictionary, and so forth without even thinking about the problem. The code for marching through all the possible words in the puzzle can be tedious, and you can start on that early, perhaps debugging it by calling a routine nextword that just prints out each word.

Due: 11:59PM Thursday, March 5.