To make this problem easier to solve, I'll break this down into smaller, easier problems. Note that I am not including code/algorithms, as I believe that will not help here (If we wanted the best Code, there would be indexes and databases and black magic that makes your head explode just seeing it). Instead, this answer tries to answer the question by talking about methods of thought that will help the OP tackle this problem (and future ones) using the method that works best for the reader.
What you need to know
This answer assumes you know how to do the following
- Create and use Objects that have properties and functions
- Pick a data structure that works (not necessarily good) for what you want to do with its contents.
Modeling your space
So, it's easy enough to load your crossword into an n by m matrix (2D array, hereby 'grid'), but this is very heard to work with pragmatically. So lets start by parsing your crossword from a grid to a legitimate object.
As far as your program needs to know, each entry in the crossword has 4 properties.
- An X-Y coordinate in the grid for the first letter
- A direction (down or across)
- Word length
- Word value
- Map of bound indexes
- Key: Index of word that is shared with another entry
- Value: Entry that index is shared with
- (You can make this a tuple and include the shared index from the other entry for easy refrence)
You can find these in the grid based on these rules while scanning.
- If Row_1_up is closed and Row_1_down is open, this is the start index of a down word. (scan down for for length. For bound indexes, left or right space will be open. scan left to get linked entry coord-id)
- Same as 1 but rotated for across words (You can do this at the same time as the scan for 1)
In your crossword object, you can store the entries using the coordinate+direction as the key for easy reference and easy conversion to/from text grid form.
Using your model
You should now have an object containing a collection of crossword entries, which contain their relevant index bindings. You now need to find a set of values that will satisfy all your entries.
Your entry objects should have helper methods like isValidEntry(str)
that checks for the given value, and the current state of the crossword, can I put this word here? By making each object in your model responsible for its own level of logic, the code for the problem one thought layer up can just call the logic without worrying about it's implementation (in this example, Your solver doesn't have to worry about the logic of is a value valid, it can just ask isValidEntry
for that)
If you have done the above right, solving the problem is then a simple matter of iterating over all words for all entries to find a solution.
List of sub problems
For reference, here is my list of sub problems that you need to write something to solve.
- How can I ideally model my work-space that is easy for me to work with?
- For each piece of my model, what does it need to know? What logic can it handle for me?
- How can I transform my text input into a usable model object?
- How do I solve my problem using my model objects? (For you, it is iterate all words/all entries to find a valid set. Maybe using recursion)