Scrabble tile checking

前端 未结 5 1415
无人共我
无人共我 2021-01-30 09:06

For tile checking in scrabble, you make four 5x5 grids of letters totalling 100 tiles. I would like to make one where all 40 horizontal and vertical words are valid. The set of

5条回答
  •  心在旅途
    2021-01-30 09:24

    Here's how I would try this. First construct a prefix tree.

    Pick a word and place it horizontally on top. Pick a word and place it vertically. Alternate them until exhausted options. By alternating you start to fix the first letters and eliminating lots of mismatching words. If you really do find such square, then do a check whether they can be made with those pieces.

    For 5x5 squares: after doing some thinking it can't be worse than O(12000!/11990!) for random text words. But thinking about it a little bit more. Every time you fix a letter (in normal text) you eliminate about 90% (an optimistic guess) of your words. This means after three iterations you've got 12 words. So the actual speed would be

    O(n * n/10 * n/10 * n/100 * n/100 * n/1000 * n/1000 ...
    which for 12000 elements acts something like n^4 algorithm
    

    which isn't that bad.

    Probably someone can do a better analysis of the problem. But the search for words should still converge quite quickly.

    There can be more eliminating done by abusing the infrequent letters. Essentially find all words that have infrequent letters. Try to make a matching positions for each letters. Construct a set of valid letters for each position.

    For example, let's say we have four words with letter Q in it.

     AQFED, ZQABE, EDQDE, ELQUO
    
     this means there are two valid positionings of those:
    
     xZxxx
     AQFED
     xAxxx   ---> this limits our search for words that contain [ABDEFZ] as the second letter
     xBxxx
     xExxx
    
     same for the other
    
     EDQDE   ---> this limits our search for words that contain [EDLU] as the third letter
     ELQUO
    
     all appropriate words are in union of those two conditions
    

    So basically, if we have multiple words that contain infrequent letter X in word S at position N, means that other words that are in that matrix must have letter that is also in S in position n.

    Formula:

    • Find all words that contain infrequent letter X at position 1 (next iteration 2, 3... )
    • Make a set A out of the letters in those words
    • Keep only those words from the dictionary that have letter from set A in position 1
    • Try to fit those into the matrix (with the first method)
    • Repeat with position 2

提交回复
热议问题