I found a puzzle in http://www.puzzles.ca/wordsearch/transportation.html where one has to find word in a grid and (s)he can read words from 8 directions. The following question raised to my mind:
We have been given a set of words. Find an algorithm which puts those words in n x m
grid where n
and m
are given. Does anyone have suggestions for an algorithm to create suitable grid as the problem seems difficult if size of the grid is only just enough to fit alphabets to the grid and words overlaps each others?
An algorithm is described in this SO question also
https://stackoverflow.com/a/23435654/3591273
Hope this helps
UPDATE: Summary of an algorithm (as given in previous link)
Randomly select the first empty wordslot to be filled from the grid and fill it with a suitable word
Find all empty wordslots that have intersections to already filled wordslots
Sort them by a constraint ratio (eg number of available solutions for each one)
Loop through the empty slots of previous step and try a number of candidate words (from the available words)
Select the wordslot and the word to fill that retains grid consistency (ie grid has a solution after this word slot is filled with this word) and also the number of solutions in next step is maximum (this minimises bactracks in next steps) and go to step 2
If no word found in previous step, try to backtrack to a previous word and use an alternative candidate (unless available candidates are exhausted)
Optionally reset any word slots that might need reset after the backtrack (ie mark them as empty again) and go to step 2
If no backtrack found then this configuration has no solution
If all empty slots are filled you have one solution
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With