Such problem was given me at my university, maybe someone will have interesting algorithm how to solve the problem. There're several solutions for it on stackoverflow, but none of them is ok (because they're looping for all possibilities).
Problem: Find all possible combinations for allocating given words into given table, grid ( rectangular, see input below ). There should be no free cells and word can be located from left to right or up to down. Words can't be located word after word in a row (column).
Input: rectangular area, example:
+-----+
| * |
| |
+-----+
or
+-----+
| * |
| |
| *|
| * |
+-----+
Then after that different words are typed ( next input data ) to fill that grid such as:
cdi
zobxzst
tdxic
r
sc
zro
and etc ...
Number is initially unknown but inputs until end of stdin - active EOF.
Output: if one solution exists then output that possible solution within filled grid. If no solution or number of solutions then output 0 or that exact number.
Example:
(table inputted)
+-----+
| * |
| |
| *|
| * |
| * *|
| * |
| |
+-----+
Then words cdi
zobxzst
tdxic
r
sc
zro
rgfvacd
oikf
df
x
c
r
xvf
ogish
za
sh
fc
hh
h
bfkh
(Each as input, but here separated by spaces.)
Output ( 1 solution only ):
+-----+
|zro*h|
|ogish|
|bfkh*|
|xvf*r|
|za*c*|
|sc*df|
|tdxic|
+-----+
Important note: The inputted grid is limited by 16 (!) cells only, number of words is less than 60. I wrote an algorithm which search through all possible combinations but it didn't work for me as execution time is limited (by 10 sec I guess) and this problem couldn't be solved with rough algorithm ( e.g. 15 on 15 grid and about 60! possible or more possible permutations which can be processed about 1 day? on ordinary 2GHz PC ).
Maybe there exists another unique solution. Maybe this problem is more mathematic then programming, maybe possible to use some left-right combos compared with up-down? or maybe weighted cells?
P.S. I have 3 weeks to solve that problem, if not, I can post solution here after 3 weeks ( good news ) ;)
There're several solutions for it on stackoverflow, but none of them is ok (because they're looping for all possibilities).
My idea is probably wrong as well then: but here's an idea off the top of my head.
I wrote an algorithm which search through all possible combinations
If there are too many, then the problem is probably that you are also searching/looping through impossible combinations as well as possible combination.
For example when you place a word like "zro" in the top-left corner, there are very few "possible" combinations which can be placed in the vertical words below it:
Therefore:
Summary:
I'm suggesting that you solve the grid, starting from an initial word.
Instead of testing each words in the top-left corner, a better (e.g. because it moe quickly eliminates impossibilities) way to generate the starting position[s] is:
rgfvacd
)