Write a program to read in the pattern for a crossword puzzle and the words that fill in the puzzle and to print out the crossword puzzle with the words filled in. The input values come from a file whose name the program must prompt for. The contents of this file are arranged as follows:
First line: Size of the puzzle (S) in terms of squares. S <= 20 and the puzzle is square; for example, if S is 6, then the puzzle is 6 x 6 squares, each square holds one letter or is blackened out.
Next S lines: The pattern for the puzzle, one row of squares per line. Each line holds S characters, where 0 indicates a square that should hold a letter and * indicates a square that is blackened out.
Other Lines: Each line holds one word to put into the puzzle along with the number of the square where the word starts and the direction the word goes: A (across) or D (down). For example, the line 3 D MOTEL says that the word MOTEL begins in square 3 and goes down. There is one space between the square number and the direction and one space between the direction and the word. There are enough of these records to fill in the whole puzzle, although not every clue is accounted for and there is no particular order to the fill-in words. There are no more than 100 words to be filled in.
The only squares that are numbered in a crossword are those that have a word starting in them. Such squares are numbered row by row, left to right, top to bottom. The following general rule determines if a square is numbered. A square, X, is numbered if conditions a) and b) are met or if conditions a) and c) are met.
a) X is a character square (indicated by a 0 in the input)
b) there is a character square to the immediate right of X AND
(X is in the leftmost column OR X has a blackened square to its immediate left)
c) there is a character square immediately below X AND
(X is in the top row OR X has a blackened square immediately above it)
Your program must figure out which are the numbered squares so that it can place the words into the puzzle.
Consider the following example of a possible data file. The data is shown in left column. The desired output is shown below that. Also, shown with the output are the numbers associated with each numbered square. Do not attempt to output the numbers; your task is simply to display the filled in squares.
7 0000*00 0000*00 0000*00 0000*00 **00000 0000000 000*0*0 17 A IRS 14 D RED 11 A YEWS 2 D AONE 1 D DDAY 7 A DOLE 1 A DAFT 5 D SILKEN 4 D TENSOR 3 D FLOWERS 6 D INGRATE 8 A IN 5 A SI 12 A KR 16 D OR 15 D TI
1D 2A 3F 4T * 5S 6I
7D O L E * 8I N
9A N O N * 10L G
11Y E W S * 12K R
* * 13E O 14R E A
15T 16O R R E N T
17I R S * D * E
The data in the column above is in the file PUZZLE7.TXT. The output that you produce for this puzzle should look like the following:
DAFT*SI DOLE*IN ANON*LG YEWS*KR **EOREA TORRENT IRS*D*E