algorithmcrosswordlanguage-agnostic

Algorithm to generate a crossword


Given a list of words, how would you go about arranging them into a crossword grid?

It wouldn't have to be like a "proper" crossword puzzle which is symmetrical or anything like that: basically just output a starting position and direction for each word.


Solution

  • I came up with a solution which probably isn't the most efficient, but it works well enough. Basically:

    1. Sort all the words by length, descending.
    2. Take the first word and place it on the board.
    3. Take the next word.
    4. Search through all the words that are already on the board and see if there are any possible intersections (any common letters) with this word.
    5. If there is a possible location for this word, loop through all the words that are on the board and check to see if the new word interferes.
    6. If this word doesn't break the board, then place it there and go to step 3, otherwise, continue searching for a place (step 4).
    7. Continue this loop until all the words are either placed or unable to be placed.

    This makes a working, yet often quite poor crossword. There were a number of alterations I made to the basic recipe above to come up with a better result.