The local ACM Newsletter has decided to start running a crossword puzzle in each issue. Instead of purchasing premade puzzles, however, the editors would like to be able to make puzzles in various shapes and with words of their own choosing. They've turned to you for the development of the puzzles ... and as a programmer, you've decided to turn to your computer and solve the problem once and for all.
A crossword grid looks like this:
..#......
#.#......
#########
#.#.#...#
#.#.....#
######..#
#.#......
Each continuous row or column of two or more hash marks represents a single word, one letter per mark. Where rows and columns cross, the letter in the shared mark must be the same.
Your goal is to write a program that fills a given grid with a given set of words such that:
- every mark in the grid contains a letter;
- no other cells in the grid contain a letter;
- every word in the word list appears once and only once in the grid with no unused words left over; and
- no words (or other sequences of letters) not in the word list appear in the grid
Words inside of other words (BRIGHT inside of BRIGHTLY, for example) do not count as an appearance of the sub-word. You may assume that every word in the word list is unique in that puzzle. You may also assume that there is at most one possible layout for a given grid and word list.