Problem Description
Problem Analysis
This is a relatively hard problem to think of, but it is also a very classic one.
The classic game “Light Up the Bulbs” is this kind.
That is, when you press a switch, this light and the lights above, below, left, and right change state.
Because pressing a switch twice is meaningless,
there are only two states for each switch:
pressed or not pressed. So this problem asks us to output
whether each switch is pressed (1) or not pressed (0).
The problem asks whether all 1s can be turned into 0s through operations.
If not, output IMPOSSIBLE.
If there are multiple operations, output the one that requires the fewest presses (1).
If the counts are equal, output the one with the lexicographically smallest result.
(If there are 00111 and 11100, output 00111.)
Idea:
If we enumerate on/off for every cell, there are $2^{n*m}$ states.
So within the range 1≤M≤15,1≤N≤15, it will definitely explode.
So think about whether it can be optimized.
In fact, once the first row is determined, the press pattern for the next row is necessarily determined,
because the only thing that affects the first row is the corresponding light in the second row.
So if a light in the first row is off, the corresponding switch in the second row should be pressed to turn it on.
And so on to obtain all states.
If the last row is also all on after pressing,
then this solution is valid.
So we can enumerate the first-row operations, which have $2^m$ states.
Code:
To conveniently represent the first-row light operations,
we can use binary to represent whether to operate.
| |
And so on; a loop can represent all operations.
There are $2^m$ iterations in total.
For each i, you can perform the AND operation i&1 to get the last binary digit.
To determine whether a light is on or off, we can use the XOR operation,
because we have the operation rules
| |
That is, XOR with 1 toggles it, and XOR with 0 leaves it unchanged.
This is the same as whether the light is toggled.
After obtaining the first-row operations for each state,
we can judge downward in order to update the answer.
| |