Kind of a Blur

Time limit1sMemory limit128 MB

Summary
Given a blurred grayscale image, recover the original pixel values under a Manhattan-distance averaging blur by solving the resulting linear system.
Level

Medium7 of 10

Topics
Math, Matrix, Implementation, Brute force
Solved
No attempts yet

Problem

Image blurring occurs when the object being captured is out of the camera's focus. Recovering the original image given only its blurred version is one of the most interesting topics in image processing; this process is called deblurring, and it is your task in this problem.

In this problem all images are grey-scale (no colours). An image is represented as a two-dimensional matrix of real numbers, where each cell holds the brightness of the corresponding pixel. Although not mathematically exact, a blurred image can be described by replacing each pixel with the average of all pixels whose Manhattan distance† from it is at most a fixed value DD (the pixel itself included). Here is how the blur of a 3×33 \times 3 image with blurring distance D=1D = 1 is computed:

blur([230172571314035])=[2+30+2532+30+17+7430+17+1332+25+7+14430+25+7+13+0517+7+13+35425+14+037+14+0+35413+0+353]=[191420121518131416]\text{blur}\begin{pmatrix}\begin{bmatrix} 2 & 30 & 17 \\ 25 & 7 & 13 \\ 14 & 0 & 35 \end{bmatrix}\end{pmatrix} = \begin{bmatrix} \frac{2+30+25}{3} & \frac{2+30+17+7}{4} & \frac{30+17+13}{3} \\ \frac{2+25+7+14}{4} & \frac{30+25+7+13+0}{5} & \frac{17+7+13+35}{4} \\ \frac{25+14+0}{3} & \frac{7+14+0+35}{4} & \frac{13+0+35}{3} \end{bmatrix} = \begin{bmatrix} 19 & 14 & 20 \\ 12 & 15 & 18 \\ 13 & 14 & 16 \end{bmatrix}

Given a blurred image, reconstruct the original image assuming it was blurred exactly as described above.

† The Manhattan distance (also called the taxicab distance) between two points is the sum of the absolute differences of their coordinates. That is, the Manhattan distance between pixels (r1,c1)(r_1, c_1) and (r2,c2)(r_2, c_2) is ∣r1−r2∣+∣c1−c2∣|r_1 - r_2| + |c_1 - c_2|.

Input

The input consists of several test cases. Each case is given on H+1H + 1 lines. The first line contains three non-negative integers WW, HH, and DD: the width and height of the blurred image and the blurring distance, respectively, where 1≤W,H≤101 \le W, H \le 10 and D≤min⁡(W/2,H/2)D \le \min(W/2, H/2). The following HH lines give the brightness of each pixel of the blurred image. Each line contains WW non-negative real numbers given to two decimal places, and every value is less than 100100.

Zero or more lines consisting entirely of whitespace may appear between cases. The last line of the input contains three zeros.

Output

For each test case, print the deblurred image as a matrix of real numbers of the same size (HH lines, each with WW values). Round each entry to two decimal places and right-justify it in a field of width 88. Separate the outputs of two consecutive test cases by a single empty line, and do not print an empty line after the last test case. It is guaranteed that every test case has exactly one unique solution.

Examples1

  1. Example 1

    Input
    2 2 1
    1 1
    1 1
    
    3 3 1
    19 14 20
    12 15 18
    13 14 16
    
    4 4 2
    14 15 14 15
    14 15 14 15
    14 15 14 15
    14 15 14 15
    
    0 0 0
    
    Expected output
        1.00    1.00
        1.00    1.00
    
        2.00   30.00   17.00
       25.00    7.00   13.00
       14.00    0.00   35.00
    
        1.00   27.00    2.00   28.00
       21.00   12.00   17.00    8.00
       21.00   12.00   17.00    8.00
        1.00   27.00    2.00   28.00