Word Search Puzzle Solver
You've been tasked with building a program that can solve a classic word search puzzle. Given a grid of letters and a list of words to find, your program should determine if each word exists within the grid. This is a common problem in algorithmic challenges and can be found in various puzzle games and educational tools.
Problem Description
The goal is to implement a function that takes a 2D array (grid) of characters and a list of strings (words) as input. The function should return a boolean value for each word indicating whether it can be found in the grid. A word can be formed by adjacent letters horizontally, vertically, or diagonally. The same letter cell may not be used more than once within the formation of a single word.
Key Requirements:
- The search should consider all eight possible directions: horizontal (left-to-right, right-to-left), vertical (top-to-bottom, bottom-to-top), and diagonal (all four directions).
- The output should indicate, for each word in the input list, whether it was found in the grid.
Expected Behavior:
For each word in the input words list, the function should return TRUE if the word exists in the board and FALSE otherwise.
Edge Cases:
- Empty grid or empty word list.
- Words that are longer than the dimensions of the grid.
- Words that share letters with other words but cannot be formed.
- Single-letter words.
Examples
Example 1:
Input:
board = [
['A','B','C','E'],
['S','F','C','S'],
['A','D','E','E']
]
words = ["ABCCED", "SEE", "ABCB"]
Output:
results = [TRUE, TRUE, FALSE]
Explanation:
- "ABCCED" can be found starting at (0,0) and moving right, down, right, down, right.
- "SEE" can be found starting at (1,3) and moving diagonally up-left.
- "ABCB" cannot be found. Although 'A', 'B', 'C' are present, a valid path to form "ABCB" without reusing cells does not exist.
Example 2:
Input:
board = [
['C','A','A'],
['A','A','A'],
['B','C','D']
]
words = ["AAB"]
Output:
results = [TRUE]
Explanation:
- "AAB" can be found starting at (0,1) and moving right to (0,2) and then down to (1,2).
Example 3:
Input:
board = [
['a']
]
words = ["a"]
Output:
results = [TRUE]
Explanation:
- The single-letter word "a" is present in the single-cell grid.
Constraints
boardwill be a 2D array of characters.boardwill have at least one row and one column.- The dimensions of
board(number of rows and columns) will be between 1 and 6, inclusive. wordswill be a list of strings.- The length of each word in
wordswill be between 1 and 10, inclusive. - Each character in
boardandwordswill be an uppercase or lowercase English letter. - The number of words in
wordswill be between 1 and 5, inclusive. - The total number of characters across all words will not exceed 50.
Notes
Consider using a recursive Depth First Search (DFS) approach to explore possible paths for each word. For each cell in the grid, if it matches the first character of a word, initiate a DFS from that cell. The DFS function should explore adjacent cells, ensuring that it does not revisit cells already used in the current path and that it stays within the grid boundaries.
A helper function to check if a given cell is valid for the current step of the search (within bounds, matches character, not visited) might be beneficial. You might also consider a mechanism to mark visited cells temporarily during the DFS and unmark them when backtracking.