Word Searchword Search Puzzle in Python Programming
Word Searchword Search Puzzle in Python Programming
1. is_valid_move**
- Input: `board` (2D list of characters), `row` (current row),
`col` (current column), `visited` (2D list indicating visited
positions)
- Output: Boolean (True if the move is valid, False otherwise)
- Checks if the move is within the board boundaries and the
position has not been visited.
2. search_word**
- Input: `board` (2D list of characters), `word` (the word to
search), `row` (current row), `col` (current column), `visited`
(2D list indicating visited positions)
- Output: Boolean (True if the word is found, False otherwise)
- Base case: If the word is empty, return True (word found).
- Check if the current position is a valid move and the
current character matches the first character of the word.
- Mark the current position as visited.
- Explore in all four directions (up, down, left, right)
recursively, checking if the remaining characters match.
- Backtrack by marking the current position as not visited
before returning.
3. word_search
- Input: `board` (2D list of characters), `word` (the word to
search)
- Output: Boolean (True if the word is found, False otherwise)
- Create a 2D list `visited` to track visited positions.
- Iterate through each position on the board.
- Call `search_word` for each position to check if the word
can be found starting from that position.
4. **Example usage**
- Define a 2D list `grid` representing the board.
- Create a list `words_to_search` containing the words to
search.
- Iterate through each word and print whether it is found or
not using the `word_search` function.
visited[row][col] = False
return False
for i in range(rows):
for j in range(cols):
if search_word(board, word, i, j, visited):
return True
return False
# Example usage:
grid = [
['A', 'B', 'C', 'E'],
['S', 'F', 'C', 'S'],
['A', 'D', 'E', 'E']
]
Found: SEE