内容中心

2026年比较好的烟台剔凿年度精选公司-青岛泰鑫伟建筑劳务有限公司

To solve this problem, we need to compute a result matrix where each element is the maximum value from a 3x3 submatrix centered around the corresponding position in the original matrix. This is a standard 3x3 max filter operation.

Approach

  1. Input Reading: First, we read the dimensions of the original matrix (rows and columns) followed by the matrix elements.
  2. Result Matrix Calculation: For each possible top-left corner of a 3x3 submatrix (starting from (0,0) up to (R-3, C-3)), compute the maximum value in that submatrix.
  3. Output: Print the result matrix, where each row contains the maximum values of the corresponding 3x3 submatrices.

Solution Code

# Read the dimensions of the matrix
R, C = map(int, input().split())

# Read the matrix
matrix = [list(map(int, input().split())) for _ in range(R)]

# Compute the result matrix
result = []
for i in range(R - 2):
    current_row = []
    for j in range(C - 2):
        max_val = -float('inf')
        # Check all elements in the 3x3 submatrix
        for di in range(3):
            for dj in range(3):
                max_val = max(max_val, matrix[i + di][j + dj])
        current_row.append(str(max_val))
    result.append(' '.join(current_row))

# Print the result
for row in result:
    print(row)

Explanation

  1. Reading Input: The first line of input gives the number of rows (R) and columns (C). The next R lines contain the elements of the matrix.
  2. Calculating Max Values: For each position (i, j) in the result matrix (which is of size (R-2)x(C-2)), we check all 9 elements in the 3x3 submatrix starting at (i, j) in the original matrix. We keep track of the maximum value in this submatrix.
  3. Output: The result matrix is constructed by joining the maximum values for each row, and each row is printed on a new line.

This approach efficiently computes the desired result with a time complexity of O(RC9), which is feasible for most practical cases given the fixed size of the 3x3 submatrix. The solution handles all edge cases where the original matrix is exactly 3x3 (resulting in a 1x1 matrix) or larger.

青岛泰鑫伟建筑劳务有限公司



作者声明:本文包含人工智能生成内容。

在线客服

在线留言
您好,很高兴为您服务,可以留下您的电话或微信吗?