# Thread: help me with the acm question

1. ## help me with the acm question

Today,I have a problem with this acm quetion,Who can help me?
Maybe you wonder what an annoying painting tool is? First of all, the painting tool we speak of supports only black and white. Therefore, a picture consists of a rectangular area of pixels, which are either black or white. Second, there is only one operation how to change the colour of pixels:

Select a rectangular area of r rows and c columns of pixels, which is completely inside the picture. As a result of the operation, each pixel inside the selected rectangle changes its colour (from black to white, or from white to black).

Initially, all pixels are white. To create a picture, the operation described above can be applied several times.
Can you paint a certain picture which you have in mind?

Input

TThe input contains several test cases. Each test case starts with one line containing four integers n, m, r and c. (1 <= r <= n <= 100, 1 <= c <= m <= 100), The following n lines each describe one row of pixels of the painting you want to create. The ith line consists of m characters describing the desired pixel values of the ith row in the finished painting ('0' indicates white, '1' indicates black).

The last test case is followed by a line containing four zeros.

Output

For each test case, print the minimum number of operations needed to create the painting, or -1 if it is impossible.

Sample Input

3 3 1 1
010
101
010
4 3 2 1
011
110
011
110
3 4 2 2
0110
0111
0000
0 0 0 0

Sample Output

4
6
-1

2. Looking at the second example: at each move choose area where all pixels are 1, or choose mixed area which after the transformation yields a new area with all pixels 1. Of course, I may be completely off with this greedy solution attempt but it would work for the sample cases.

Edit: it won't work just like that. For example the following position with 2x2 rectangles is solveable even though there isn't any transformation that leads to an all black area:

0110
1001
1001
0110

However, all 2x2 sections contain at most 2 black pixels and there are transformations that lead to rectangles with 3 black pixels. May-be that would help?

3. First of all, notice how you will never have to use exactly the same rectangle on the same position more than once: applying it X times is the same as applying it (X%2) times (!!a = a).

Now we can use the fact that the rectangle must be inside the image. If the upper-left bit is not the initial color, there's only one rectangle that can be used to fix that. Now since we know we never have to use the same rectangle, we can move one pixel to the right. Again, there's only one rectangle that can invert it if we must, since the only other option is one pixel to the left, but we already know whether we had to select that.

That results in quite an easy algorithm, but I believe it should be efficient enough. It's only O((n-r+1)*(m-c+1)*r*c). So the maximum is about 6.502.500 operations. During the contests, you could usually fit 1 to 10 million operations per test case. Since these operations are fairly simple, I imagine it'll be fast enough.

4. Sounds brilliant, except practically solves the challenge for the OP

5. I don't get it. Can't you just use rectangles of size 1x1 to control individual pixels? All images are possible and can be drawn in at most O(n) time.

EDIT: Ah, you don't get to choose r and c. I see now.

6. Originally Posted by anon
Sounds brilliant, except practically solves the challenge for the OP
I know... Puzzles like these always get me excited and whatever I'm doing, I put down my work until I fix the problem...

@brewbuck: Yeah, I had to read the problem statement twice as well before I understood that point .