I enrolled in the Algorithms, Part II course on Coursera, and one of the assignments is to solve the Boggle game: http://coursera.cs.princeton.edu/algs4/assignments/boggle.html
The honor code requires that I don't publicly post the solution, so here's the pseudocode of the basic algorithm instead.
visit:
word <- board[i][j]
start <- dictionary.match(word, start)
if start is not null
visited[i][j] <- true
word <- prefix + word
if word is longer than min required length
words <- words + word
for (x, y) ∊ adj(i, j)
if not visited(x, y)
visit (x, y)
visited[i][j] <- false
The dictionary is implemented using a Trie.
The above code works, and I passed the assignment, but then I came across this blog post that claims a faster solution using dynamic programming:
It turns out, we can use a nifty dynamic programming technique to quickly check whether a word (from the dictionary in this case) can be constructed from the board or not!
Here is core point of the dynamic programming idea:
For a word of length k to be found (end location) at the [i, j]-th location of the board, the k-1'th letter of that word must be located in one of the adjacent cells of [i, j].
The base case is k = 1.
A letter of length 1 will be found (end location) in the [i, j]-th cell of the board of the only letter in the word matches the letter in the [i, j]-th location of the board.
Once our dynamic programming table is populated with the base case, we can build on top of that for any word of length k, k > 1.
Unfortunately, the author did a poor job of explaining, and I'm not able to follow his solution. I'd like to though, and hoping someone here could explain it to me.
P.S.:
Not a duplicate of this question, as that one doesn't use DP; please, keep those duplicate-happy fingers in check.
There's sufficient effort demonstrated on my part and not asking anyone to do my homework. I already have a solution of my own. What I'm interested in is learning a better way of solving the problem, if one exists.
Thanks!