2D Max Filter
InterviewTime limit1sMemory limit128 MB
For every cell of an R by C grid, output the maximum value inside the rectangular window centered on that cell, clipped at the borders.
- Level
Medium5 of 10
- Topics
- Sliding window, Queue, Matrix
- Solved
- No attempts yet
Problem
An electrical engineer wants an efficient algorithm for filtering a 2D signal. The signal is digitized into blocks arranged in rows and columns, and every block holds one integer value.
The filter has a rectangular window covering rows and columns, where and are non-negative integers. The window moves across the entire input signal and produces another 2D signal under the following rules.
- When the window center is at row , column of the input signal, the window produces one value at row , column of the output signal.
- The produced value is the maximum input value inside the region the window covers.
- The part of the window that lies outside the input signal is not considered. Only the part inside the input signal is taken into account.
Rows and columns are numbered from 1. For example, when and , the window is 3 rows by 5 columns. If its center is at row 4, column 5, the window covers the input region from row 3, column 3 to row 5, column 7, and the maximum value in that region becomes the output at row 4, column 5. If its center is at row 1, column 2, the window reaches from row -1, column -1 to row 2, column 4, but everything outside the signal is ignored, so the values actually taken into account are those from row 1, column 1 to row 2, column 4.
Given the input signal, write a program that computes the output signal of this 2D max filter.
Input
The first line contains and , the numbers of rows and columns of the input signal, separated by a space. (, )
The second line contains and , separated by a space. (, ) The window size follows from and : it is rows by columns.
Each of the next lines holds one row of the input signal, given as integers from left to right, separated by spaces. Each value is an integer between 0 and 10000.
Output
Print the output signal on lines. Each line contains the output values of that row, separated by spaces.