Blur
InterviewTime limit2sMemory limit256 MB
Blur a binary image up to nine times with a wrapping 3 by 3 average and count how many distinct gray values remain.
- Level
Easy3 of 10
- Topics
- Simulation, Matrix, Math
- Solved
- No attempts yet
Problem
You have a black and white image that is pixels wide and pixels high. Each pixel is written as a single number: black is and white is . A friend asks you to blur the image, which produces various shades of gray.
The blur works like this. Build a new image of the same size, and set each pixel of the new image to the average of the pixels in the square centered at the corresponding pixel of the old image. When you take that average, the edges wrap around. The left neighbor of a pixel in the leftmost column sits in the rightmost column of the same row, and the top neighbor of a pixel in the top row sits in the bottom row of the same column. The square therefore always gives you exactly pixels. If you want the image blurrier, apply the same process again to the blurred image.
You are given the original image and the number of times to blur it. If all the arithmetic is performed exactly, how many distinct shades of gray does the final image have?
Warning: floating point numbers are finicky. If you represent fractions in floating point, may not equal . Find a way to solve this without floating point arithmetic.
Input
The first line contains three space separated integers , , and (, ), the width of the image, its height, and the number of times to blur it. Each of the following lines contains space separated integers describing the original image. Each integer is or according to the color of the pixel.
Output
Print, on a single line, the number of distinct shades of gray in the final image.