'codeforces 771 div 2 big brush

Link to question : https://codeforces.com/contest/1638/problem/D In the Editorial of this question

""" Let's try to build the solution from the last operation to the first operation. The last operation can be any 2×2 square painted in a single color. If there is no such square, it is clearly impossible. Otherwise, this square being the last operation implies that we could previously color its cells in any color, multiple times, without any consequences. We will name these special cells.

What happens when we run out of 2×2 squares painted in a single color? Well, we can use the special cells described above. The next operation considered can be any 2×2 square such that all its non-special cells are painted in the same color. If there is no such square, it is clearly impossible.

We now have a correct solution. It consists of at most nm operation because at each step we turn at least one non-special cell into a special one and there are nm cells. We can implement this solution similar to BFS. First, insert all 2×2 squares painted in a single color into a queue. Then, at each step take the square in the front of the queue, add it to the solution and make all its non-special cells special. When making a cell special, check all 2×2 squares that contain it and if some of them meet the condition after the current step, insert them into the queue. Note that there are at most 9 such squares."""

how can there could be atmost 9 squares possible ??



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source