A board path and a word prefix advance together
Picture
Find dictionary words on a letter board by stepping horizontally or vertically without reusing a cell within one word. The board describes possible movement; the trie describes possible spellings. Search advances through both structures at once and stops when either forbids the next step.
Predict
Two paths arrive at the same cell and trie node, but have used different board cells. Can they always share a cached answer keyed only by (cell, trie_node)?
Reveal the reasoning
No. The used cells change which next moves are legal. Those two coordinates omit relevant history. A path-local visited set is necessary for this search; a sound memoization scheme would have to account for that information too.
Go deeper: share prefix work and restore board history
Assume a rectangular board of single-character strings and nonempty dictionary words. Return each found word once, regardless of how many board paths spell it.
def find_words(board, words):
if not board or not board[0]:
return []
root = {}
for word in words:
node = root
for char in word:
node = node.setdefault(char, {})
node[None] = word
rows, cols = len(board), len(board[0])
found, used = set(), set()
def visit(r, c, node):
if not (0 <= r < rows and 0 <= c < cols) or (r, c) in used:
return
char = board[r][c]
if char not in node:
return
child = node[char]
if None in child:
found.add(child[None])
used.add((r, c))
for dr, dc in [(1, 0), (-1, 0), (0, 1), (0, -1)]:
visit(r + dr, c + dc, child)
used.remove((r, c))
for r in range(rows):
for c in range(cols):
visit(r, c, root)
return sorted(found)
board = [["c", "a"], ["r", "t"]]
assert find_words(board, ["cat", "car", "cr", "catr", "caca"]) == ["cat", "catr", "cr"]
assert find_words([["a"]], ["a", "aa", "a"]) == ["a"]
The used-cell set is restored before a sibling route runs. The board and trie are not destructively pruned, so the same dictionary works from every starting cell. A set handles duplicate found words under this output contract.
Let S be total dictionary characters and L the maximum word length, capped by the number of board cells for useful search depth. A loose upper bound is O(S + RC·4**L) search work, plus sorting output. Trie pruning can reduce actual work substantially but does not eliminate the exponential worst case. Trie space is O(S); active path/stack space is O(L) excluding outputs.
Analogy limit: sharing a prefix representation is useful without implying that all board paths at that prefix have equivalent futures.
Change one rule
Return every board path spelling every word, not just the distinct words. Is a result set of word strings still enough?
Check the variation
No. Preserve coordinate paths and emit a copy for each completed match. A word may correspond to many required outputs, increasing both time and storage.
Connect
Budget DP adds a small resource summary; subset DP shows the cost when a whole used set matters. Recognition combines representation, search, and sufficient state.