Woodstock Blog

a tech blog for general algorithmic interview questions

[Google] Crosswod Solver

Question

link

Given a wordlist like this:

1. ccaa
1. baca
1. baaa
1. bbbb

and a Grid like this:

X X 
XXXX
X X 
XXXX

Now solve this crossword. One possible solution:

b c 
baca
b a 
baaa

Solution

The corssword problem is NP-Complete, so your best shot is brute force: just try all possibilities, and stop when a possibility is a valid. Return failure when you exhausted all possible solutions.

Code

Pseudo code for brute force. (this just serve as a guide, not a complete/correct solution)

solve(words,grid):
   if words is empty:
       if grid.isValudSol():
          return grid
       else:
          return None
   for each word in words:
       possibleSol <- grid.fillFirst(word)
       ret <- solve(words\{word},possibleSol)
       if (ret != None):
          return ret
   return None