A survey drone flies a rectangular field in a fixed grid pattern, recording one integer elevation reading (in centimeters relative to a reference plane, so a value can be negative) for every cell it passes over. The raw readings are noisy, so before the terrain map is produced, every cell's reading is replaced by a smoothed value: the floor of the average of that cell's own reading together with every one of its up to eight orthogonal and diagonal neighbors that actually lies inside the grid. An interior cell averages over 9 readings, an edge cell over 6, and a corner cell over 4. Given the raw grid, produce the smoothed grid.
m and n — the number of rows and columns in the grid.m lines: n integers — the raw elevation readings of that row.Print m lines, each containing n integers separated by single spaces — the smoothed grid, where every output value equals floor(S / c), with S the sum of the cell and its existing neighbors and c the number of cells summed (rounding toward negative infinity, matching integer floor division).
Example 1
Input
3 3 1 1 1 1 0 1 1 1 1
Expected
0 0 0 0 0 0 0 0 0
Explanation
Every cell of this 3x3 grid smooths to 0. The center cell averages all nine readings: eight 1s and one 0 sum to 8, and floor(8/9) = 0. A corner cell such as the top-left one averages its 2x2 block of readings {1,1,1,0} = 3, and floor(3/4) = 0 as well; every edge and corner cell works out the same way.
Example 2
Input
2 3 50 80 50 80 50 80
Expected
65 65 65 65 65 65
Explanation
Every cell in this 2x3 grid smooths to 65. The top-left corner cell averages its 2x2 block {50,80,80,50}, summing to 260 over 4 cells: floor(260/4) = 65. The top-middle cell averages all six cells (every column is within one step of it): {50,80,50,80,50,80} sums to 390 over 6 cells: floor(390/6) = 65. Every other cell works out the same way by symmetry.
Ready to solve this?
Sign in to open the editor, run your code against the sample tests, and submit against the full test suite.
Sign in to solve →