Cross Convolution

면접 대비

시간 제한1.5초메모리 제한1024 MB

요약
홀수 크기의 십자 모양 커널을 주어진 보폭으로 N×M 행렬 위에 놓을 때, 커널이 덮는 칸들의 합을 모든 유효 위치에 대해 출력한다.
난이도

보통10점 중 4점

유형
누적 합, 행렬, 구현
정답자
아직 제출이 없습니다

문제

You are given an N×MN \times M matrix AA. You are also given a special kernel of size K×KK \times K, and a stride SS. Your task is to implement a convolution-like operation using a kernel with unique properties. The kernel must always remain completely within the matrix boundaries during its operation.

Specifically, the unique features of a kernel in this problem are:

  • The kernel size KK is always an odd number.
  • The kernel is filled with 11 along the horizontal and vertical axes passing through the center and 00 elsewhere.

For example, the following is the kernel when the size is 33 and 55, respectively.

\[\begin{bmatrix} 0 & 1 & 0 \\ 1 & 1 & 1 \\ 0 & 1 & 0 \end{bmatrix} \qquad \begin{bmatrix} 0 & 0 & 1 & 0 & 0 \\ 0 & 0 & 1 & 0 & 0 \\ 1 & 1 & 1 & 1 & 1 \\ 0 & 0 & 1 & 0 & 0 \\ 0 & 0 & 1 & 0 & 0 \end{bmatrix}\]

The stride determines the step size for moving the kernel across the matrix. The kernel moves across the matrix based on the given stride. At each valid position (where the kernel fits entirely within the matrix boundaries), the element-wise sum of the matrix elements covered by the kernel is calculated.

Calculate a new matrix BB containing the sums from all valid kernel positions. The size of BB should be (N−KS+1)×(M−KS+1)\left(\frac{N-K}{S}+1\right) \times \left(\frac{M-K}{S}+1\right).

입력

The first line contains four space-separated integers: NN and MM, denoting the size of the image; K K, denoting the kernel size; and SS, denoting the stride. (1≤N,M≤2,000;1 \le N,M \le 2\\,000; 1≤K,S≤min⁡⁡N,M;1 \le K, S \le \min⁡\\{N,M\\}; K≡1(mod2);K \equiv 1 \pmod 2; SS is a divisor of gcd⁡(N−K,M−K)\gcd(N-K,M-K))

The following NN lines of input contain NMNM integers, where each line has MM space-separated integers, denoting the value of the matrix AA. Here, the jj-th integer of the ii-th line denotes A_ijA\_{ij}. (−106≤A_ij≤106-10^6 \le A\_{ij} \le 10^6)

출력

Output (N−KS+1)\left(\frac{N-K}{S}+1\right) lines denoting the resulting matrix BB after applying the kernel with the given stride.

Each line should contain (M−KS+1)\left(\frac{M-K}{S}+1\right) space-separated integers. The jj-th integer of the ii-th line should represent B_ijB\_{ij}​.

예제2

  1. 예제 1

    입력
    5 5 3 1
    1 2 3 4 5
    5 6 7 8 9
    9 8 7 6 5
    4 3 2 1 0
    0 1 2 3 4
    
    예상 출력
    28 31 34
    33 30 27
    18 15 12
    
  2. 예제 2

    입력
    5 5 3 2
    1 2 3 4 5
    5 6 7 8 9
    9 8 7 6 5
    4 3 2 1 0
    0 1 2 3 4
    
    예상 출력
    28 34
    18 12