CBOJ Winter Contest Problem 4 - Paint.NET


Submit solution

Points: 12 (partial)
Time limit: 2.0s
Memory limit: 512M

Author:
Problem type

After failing his chemistry test, Bob has decided to give up on his dreams and became a graphic artist instead!

Bob is given a canvas with dimensions \(N\) pixels tall and \(M\) pixels wide, where each pixel of which has some colour with a value ranging from \(0\) to \(255\). To paint the canvas, Bob will apply a fill operation to the canvas.

A fill operation is defined with the four integers \(X\), \(Y\), \(C\), and \(T\) where \((X, Y)\) represents the coordinate of the pixel on the canvas to which the operation is applied, \(C\) represents the colour which the area should be coloured, and \(T\) represents the fill operation's threshold (specified below).

The area that should be painted along with the initial pixel at \((X, Y)\) is defined as follows:

  • Initially, the pixel at \((X, Y)\) should be painted.
  • Consider the pixels that are adjacent to the initial one (share a common side with the current pixel). If the absolute difference between the initial pixel's colour (before the fill operation began) and the adjacent pixel's colour is less than \(T\), then the pixel should be painted as well.
  • Repeat the previous two steps for the newly painted pixels.

Help Bob paint the canvas! Output the canvas after the fill operation is applied.

For this problem, Python users should submit their submissions in PyPy over CPython.

Input Specification

The first line of the input will contain two integers \(N\), \(M\) \((1 \le N, M \le 1000)\), indicating the dimensions of the canvas.

The next \(N\) lines of the input will contain \(M\) integers, describing the initial canvas and each pixel's value.

The final line of the input contains four integers \(X\), \(Y\), \(C\), and \(T\) \((1 \le X \le N, 1 \le Y \le M, 0 \le C, T \le 255)\), representing the coordinates of the initial pixel, the filled colour, and the threshold.

Output Specification

Output the canvas after the fill operation is applied. To do this, output \(N\) lines each with \(M\) integers separated with whitespace.

Subtasks

Note that you will not be required to pass the sample input to receive points for the first subtask.

Subtask 1 [10%]

\(1 \le N, M \le 3\)

Subtask 2 [90%]

No additional constraints.

Sample Input

2 5
1 2 3 0 5
1 0 3 3 0
1 2 4 2

Sample Output

4 4 4 0 5
4 4 4 4 0

Sample Explanation

The fill operation begins on the first row's second pixel (denoted by the value 2).

After considering the initial neighbours the canvas becomes:

4 4 4 0 5
1 0 3 3 0

After another round of considering the adjacent pixels the canvas becomes:

4 4 4 0 5
4 0 4 3 0

Finally, the grid becomes:

4 4 4 0 5
4 4 4 4 0

Note that the pixel with value 0 became coloured with 4 because it has a difference of \(|1 - 0| = 1\) (compared with the pixel on the left).


Comments

There are no comments at the moment.