[Huawei computer test] 2023 real test paper B (python) - word search - find it

1. Title

Topic description:

Find It is a mini-game where you need to find a given word in a matrix.
Assuming that the word HELLOWORD is given, as long as H->E->L->L->O->W->O->R can be found in the matrix Words formed by ->L->D will pass.
Please note that English letters are case-sensitive, and you can only walk up, down, left, and right, and cannot go back.

2. Input and output

Input description:
The first line of the input contains two integers n and m (0 < n,m < 21), respectively representing a matrix of n rows and m columns, otherwise output "NO". If the given word can be connected in the matrix, then output the position of the first letter of the given word in the matrix (the row and the several columns), Output description: from row 3 to n+ Row 2 refers to a string matrix of length m containing uppercase and lowercase English letters.
Row 2 is a word W whose length does not exceed 100 (a given word W will only appear once in the entire matrix),



3. Example

Example 1  
Import import export example 仅调试、后台题题题例文帳に追加> 5 5 HELLOWORLD CPUCY EKLQH CHELL LROWO DGRBC Output: 3 2









4. Requirements

Time limit: 1 second for C/C++, 2 seconds for other languages
Space limit: 262144K for C/C++, 524288K for other languages
64bit IO Format: %lld

5. Problem-solving ideas

  1. First, we need to iterate through each position in the matrix and find the position that matches the first letter of the given word.
  2. For each matching position, we can use depth-first search (DFS) to determine whether it can be connected to the given word.
  3. During the DFS process, we need to determine whether the current position is out of bounds, whether it has been visited, and whether the letter at the current position matches the next letter of the given word.
  4. If it can be connected to form the given word, we output the position of the first letter in the matrix; otherwise, output "NO".

6. Reference code 

# -*- coding: utf-8 -*-
'''
@File    :   2023-B-单词搜索-找到它.py
@Time    :   2023/12/15 17:13:34
@Author  :   mgc 
@Version :   1.0
@Desc    :   None
'''

def find_word_position(n, m, word, matrix):
    directions = [(0, 1), (0, -1), (1, 0), (-1, 0)]  # 上下左右四个方向的偏移量

    def dfs(row, col, index):
        if index == len(word):  # 已经匹配到给定单词的最后一个字母
            return True

        if row < 0 or row >= n or col < 0 or col >= m:  # 当前位置越界
            return False

        if matrix[row][col] != word[index]:  # 当前位置的字母与给定单词的下一个字母不匹配
            return False

        temp = matrix[row][col]  # 临时保存当前位置的字母
        matrix[row][col] = '#'  # 将当前位置标记为已访问

        for dx, dy in directions:
            if dfs(row + dx, col + dy, index + 1):  # 沿着四个方向继续搜索
                return True

        matrix[row][col] = temp  # 恢复当前位置的字母
        return False

    for i in range(n):
        for j in range(m):
            if matrix[i][j] == word[0]:  # 找到与给定单词首字母匹配的位置
                if dfs(i, j, 0):  # 使用DFS判断是否能够连成给定的单词
                    return f"{i+1} {j+1}"  # 输出首字母在矩阵中的位置

    return "NO"

# 测试样例
n, m = map(int, input().split())
word = input()
matrix = []
for _ in range(n):
    row = list(input())
    matrix.append(row)

result = find_word_position(n, m, word, matrix)
print(result)

Guess you like

Origin blog.csdn.net/u014481728/article/details/135021802