Time limit
2s
Memory limit
128 MB
There is a security panel with R rows and C columns. The buttons are numbered from 1 to R x C in row-major order, starting at the upper-left corner.
Initially, every button is off. When one button is pressed, the given 3 x 3 pattern is placed with its center on that button. Every in-panel button corresponding to a * in the pattern changes state. Positions outside the panel are ignored. Changing state means that an on button becomes off and an off button becomes on.
To unlock the panel, every button must be on. Pressing the same button twice is never useful, so each button only needs to be considered as either pressed or not pressed.
The input contains multiple test cases.
The first line of each test case contains the number of rows R and columns C of the panel. (1 <= R, C <= 5)
The next three lines each contain a string of length 3 describing the pattern applied when a button is pressed. The center cell represents the pressed button. A * means the button at that relative position changes state, and a . means it does not change.
The input ends with a line containing 0 0.
For each test case, first print Case #i on its own line, where i is the 1-based test case number.
If it is possible to turn on every button, print the button numbers that must be pressed in increasing order on the next line. The number of pressed buttons must be minimum. If several minimum solutions exist, choose the one whose reversed output sequence is lexicographically smallest.
If it is impossible, print Impossible..