This is intended to be simple, but I still have not understood how to solve it.
You have a building. Its windows are disposed like a grid with N rows and M columns.
Each window has a light (on / off). Here's an example of such a building:
1 0 1 1 0
0 1 0 0 1
1 0 1 1 0
0 1 0 0 1
0 1 0 0 1
with N = M = 5.
You have N+M special buttons: N buttons to toggle the state of ALL the lights of the i-th row, and M buttons to toggle the state of ALL the lights of the j-th column.
Toggle(x):
if (x = 1) then x = 0
else x = 1
You want to turn off all the lights using these N+M buttons, however this isn't always possibile. Determine if it's possible to turn off all the lights and, in that case, wich buttons has to be pressed (see examples).
SAMPLE INPUT:
5 5
1 0 1 1 0
0 1 0 0 1
1 0 1 1 0
0 1 0 0 1
0 1 0 0 1
OUTPUT:
Rows: 1 0 1 0 0
Columns: 0 1 0 0 1
- - -
SAMPLE INPUT:
5 5
1 0 1 1 0
0 1 1 0 1
1 0 1 1 0
0 1 0 0 1
0 0 1 0 1
OUTPUT:
Impossible
- - -
UPDATE: I forgot this...
Constraints:
- 1 < N < 500
- 1 < M < 500
- The building has at least one lighted window.
(code deleted)
This is a problem from TC, no?
It can be solved like this:
1) Make all the lights in the 1st row on or off (check both) by using column switches
2) If all the lights in i-th row are off, then toggle i-th row. If all the lights are on, do nothing. Else there is no solution for current 1) case.
each row*each column of the first row* you have 2M, am I wrong?