FilmFunhouse

Location:HOME > Film > content

Film

Detecting Word Presence in a Character Matrix Across Multiple Directions

January 30, 2025Film1428
Detecting Word Presence in a Character Matrix Across Multiple Directio

Detecting Word Presence in a Character Matrix Across Multiple Directions

The challenge of finding a specific word within a character matrix that may be oriented in any direction is a common contest problem. By understanding how to approach such a problem, we can create a robust algorithm that efficiently handles various orientations and boundaries. This article will explore the methods to solve this problem, providing practical examples in Python.

Problem Statement

The task involves determining whether a given word can be found within a 2D character matrix. The word can be present horizontally, vertically, or diagonally. This inclusivity makes the problem inherently more complex but also more interesting.

Algorithm Overview

To tackle this problem, we need to:

Define the matrix Specify the possible directions for search Implement the search function Add boundary checks

Step-by-Step Implementation

Step 1: Define the Matrix

A 2D list is used to represent the character matrix.

Step 2: Define Directions

We need to specify the possible directions for the search, including right, left, down, up, and the four diagonals.

Step 3: Implement the Search Function

The function will iterate through each cell in the matrix and check for the word in all directions. It will also include boundary checks to ensure the search does not go out of the matrix's boundaries.

Sample Python Implementation

def word_search(matrix, word):
    if not matrix or not matrix[0]:
        return False
    rows  len(matrix)
    cols  len(matrix[0])
    word_length  len(word)
    # Directions: right, left, down, up, diagonal down-right, diagonal down-left, diagonal up-right, diagonal up-left
    directions  [
        (0, 1),  # right
        (0, -1), # left
        (1, 0),  # down
        (-1, 0), # up
        (1, 1),  # diagonal down-right
        (1, -1), # diagonal down-left
        (-1, 1), # diagonal up-right
        (-1, -1) # diagonal up-left
    ]
    def search_from(x, y, dx, dy):
        for k in range(word_length):
            new_x  x   k * dx
            new_y  y   k * dy
            if new_x  rows or new_y  cols or matrix[new_x][new_y] ! word[k]:
                return False
        return True
    for i in range(rows):
        for j in range(cols):
            for dx, dy in directions:
                if search_from(i, j, dx, dy):
                    return True
    return False

Example Usage

Consider the following matrix and word:

matrix  [
    ['A', 'B', 'C', 'E'],
    ['S', 'F', 'C', 'S'],
    ['A', 'D', 'E', 'E']
]
word  "SEE"
print(word_search(matrix, word))   # Output: True

In this example, the word "SEE" is found in the second row, moving down and to the right.

Explanation of the Code

Matrix and Word: The matrix variable holds the character grid, and word is the string you want to search for. Directions: The directions list contains tuples that represent the increments for each direction (row, column). Search Function: The search_from function checks if the word can be formed starting from the given coordinates (x, y) in the specified direction (dx, dy). Iterate Through the Matrix: The nested loops go through each cell in the matrix and try to find the word by calling the search_from function for each direction.

Complexity Analysis

The time complexity of this solution is (O(N times M times L)), where (N) is the number of rows, (M) is the number of columns, and (L) is the length of the word. This complexity is efficient for reasonable matrix sizes. However, for larger matrices, optimizations such as memoization or more advanced algorithms might be necessary.

Feel free to modify the code as needed for your specific contest requirements or to solve similar problems in other programming languages.