Hard Matrices

Word Search

Receives a rectangular grid of single letters and a target word, and returns whether the word can be traced through the grid by moving to horizontally or vertically adjacent cells, never reusing the same cell twice within one trace. From every cell it tries a depth-first search that matches the word letter by letter, marking each cell used along the current path and unmarking it (backtracking) whenever a path dead-ends, so a failed attempt never blocks a different starting cell or direction from reusing that cell. Returns true as soon as one full trace matches the word; returns false only after every starting cell and every direction has been exhausted. An empty word is trivially found (true); an empty grid cannot contain any word (false).

Visualization

Input

Algorithm code

Custom input

Saved inputs

References