Source: APMO-2007-P2
Category: Extremal Problems

Problem: In a faulty $5\\times5$ grid of $25$ lights, pressing the switch of one light changes the state (from off to on, or from on to off) of that light and all adjacent lights in its row and column. Initially, all lights are off. After pressing the switches several times, exactly one light is on. Determine all possible positions of this light that is on.

Answer: the cells at positions $(2,2), (2,4), (3,3), (4,2), (4,4)$

Reference solution:
Let the lights be represented by a $5 \times 5$ grid of cells $(i,j)$ for $1 \le i, j \le 5$. Pressing a switch at $(i,j)$ toggles the state of the light at $(i,j)$ and all its orthogonal neighbors. We want to find all cells $(r,c)$ such that there exists a sequence of presses leaving exactly the light at $(r,c)$ on, with all other lights off.

First, we define two specific sets of cells:
$$S_1 = \{ (i,j) \mid i \in \{1,2,4,5\} \text{ and } j \in \{1,3,5\} \}$$
$$S_2 = \{ (i,j) \mid i \in \{1,3,5\} \text{ and } j \in \{1,2,4,5\} \}$$
Notice that $S_2$ is the reflection of $S_1$ across the main diagonal. We claim that for any switch pressed, the number of toggled lights belonging to $S_1$ is always even. Let $N(r,c)$ be the set of cells toggled by pressing $(r,c)$ (which includes $(r,c)$ and its orthogonal neighbors). We can exhaustively check the size of $N(r,c) \cap S_1$:
- If $r \in \{1,5\}$ and $c \in \{1,5\}$, $|N(r,c) \cap S_1| = 2$.
- If $r \in \{1,5\}$ and $c = 3$, $|N(r,c) \cap S_1| = 2$.
- If $r \in \{2,4\}$ and $c \in \{1,5\}$, $|N(r,c) \cap S_1| = 2$.
- If $r \in \{2,4\}$ and $c = 3$, $|N(r,c) \cap S_1| = 2$.
- If $r = 3$ and $c \in \{1,5\}$, $|N(r,c) \cap S_1| = 2$.
- If $r = 3$ and $c = 3$, $|N(r,c) \cap S_1| = 2$.
- If $c \in \{2,4\}$ and $r \in \{1,5\}$, $|N(r,c) \cap S_1| = 2$.
- If $c \in \{2,4\}$ and $r \in \{2,4\}$, $|N(r,c) \cap S_1| = 2$.
- If $c \in \{2,4\}$ and $r = 3$, $|N(r,c) \cap S_1| = 0$.

In all cases, pressing any switch changes the state of an even number of lights in $S_1$. Since initially all lights are off (so $0$ lights in $S_1$ are on), the parity of the number of lights turned on in $S_1$ is invariant and must remain even. Therefore, if exactly one light is finally on, its position cannot be in $S_1$. 

By an identical symmetric argument, any switch press toggles an even number of lights in $S_2$. Thus, the single light that is finally on cannot be in $S_2$ either. 

The position of the single light must therefore lie in the complement of $S_1$ and the complement of $S_2$. 
The complement of $S_1$ is $Z_1 = \{ (i,j) \mid i = 3 \text{ or } j \in \{2,4\} \}$.
The complement of $S_2$ is $Z_2 = \{ (i,j) \mid j = 3 \text{ or } i \in \{2,4\} \}$.
Taking the intersection $Z_1 \cap Z_2$:
- If $i = 3$, then for the cell to be in $Z_2$, we must have $j = 3$ (since $i \notin \{2,4\}$). This gives the position $(3,3)$.
- If $j \in \{2,4\}$, then for the cell to be in $Z_2$, we must have $i \in \{2,4\}$ (since $j \neq 3$). This gives the positions $(2,2), (2,4), (4,2), (4,4)$.

Hence, the only possible positions for the final single light are the five cells $(2,2), (2,4), (3,3), (4,2),$ and $(4,4)$.

Below we provide explicit constructions for the middle position $(3,3)$ and the upper-right position $(2,4)$. We represent the presses as a $5 \times 5$ matrix where a $1$ indicates the switch is pressed once, and a $0$ indicates it is not pressed.

For the middle position $(3,3)$, the construction is:
$$ \begin{pmatrix}
0 & 0 & 0 & 1 & 1 \\
0 & 0 & 1 & 0 & 0 \\
0 & 1 & 1 & 0 & 1 \\
1 & 0 & 0 & 0 & 1 \\
1 & 0 & 1 & 1 & 0
\end{pmatrix} $$
One can verify that this sequence of presses toggles the light at $(3,3)$ an odd number of times (three times) and every other light an even number of times.

For the upper-right position $(2,4)$, the construction is:
$$ \begin{pmatrix}
0 & 0 & 1 & 0 & 0 \\
0 & 1 & 1 & 1 & 0 \\
1 & 0 & 0 & 1 & 1 \\
1 & 0 & 0 & 1 & 0 \\
0 & 1 & 1 & 0 & 0
\end{pmatrix} $$
Similarly, this toggles the light at $(2,4)$ an odd number of times and all other lights an even number of times.

For the other three positions $(2,2), (4,4),$ and $(4,2)$, one can obtain the required switch presses by rotating the construction matrix for $(2,4)$ by $90^\circ, 180^\circ,$ and $270^\circ$, respectively. 

Thus, the possible positions of the single light that is on are exactly $(2,2), (2,4), (3,3), (4,2),$ and $(4,4)$.

Grading guidelines:
(Partial) 1. Used a parity/invariant argument to obtain a nontrivial restriction on the final position (e.g. defined a subset of cells and proved that every press toggles an even number of lights in it, hence the final single on-light cannot lie in that subset). 2. Correctly identified at least one of the two key forbidden sets (equivalently, proved that the final on-light cannot be in all cells with i∈{1,2,4,5}, j∈{1,3,5}, or the symmetric set with i∈{1,3,5}, j∈{1,2,4,5}), even if the full classification is not finished. 3. Gave a correct explicit press pattern (or a verifiable constructive procedure) that results in exactly one light on for at least one of the five correct target cells. (Almost) 1. Correctly proved (typically via two parity invariants) that the final on-light must belong to the intersection of the two complements, yielding the candidate set {(2,2),(2,4),(3,3),(4,2),(4,4)}, but did not provide constructions showing these candidates are all attainable. 2. Gave constructions for (3,3) and one corner-adjacent candidate (e.g. (2,4)) and indicated obtaining the other three by rotations/symmetry, but left the symmetry reduction or the verification of the constructions insufficiently justified. 3. Solution is almost complete, but made minor mistakes which are not negligible.

Construction task:
Find all possible positions of the single light that is on. For each possible position, determine a sequence of switch presses that achieves this state starting from all lights off.

Construction format requirements:
- Output a single Python dictionary.
- The keys must be all valid 0-based coordinates `(r, c)` (tuples of integers) of a light that can be the only one turned on.
- The value for each key must be a list of 0-based coordinates `(r, c)` representing a sequence of switch presses that results in exactly that light being on.
- Put ONLY the Python dictionary literal inside the `<construct>` block. Do not include any other text or markdown formatting.

Reference construction:
{
    (1, 1): [(0, 1), (0, 3), (1, 0), (1, 1), (1, 3), (1, 4), (2, 3), (3, 0), (3, 1), (3, 2), (4, 1)],
    (1, 3): [(0, 1), (0, 3), (1, 0), (1, 1), (1, 3), (1, 4), (2, 1), (3, 2), (3, 3), (3, 4), (4, 3)],
    (2, 2): [(0, 3), (0, 4), (1, 2), (2, 1), (2, 2), (2, 4), (3, 0), (3, 4), (4, 0), (4, 2), (4, 3)],
    (3, 1): [(0, 1), (1, 0), (1, 1), (1, 2), (2, 3), (3, 0), (3, 1), (3, 3), (3, 4), (4, 1), (4, 3)],
    (3, 3): [(0, 3), (1, 2), (1, 3), (1, 4), (2, 1), (3, 0), (3, 1), (3, 3), (3, 4), (4, 1), (4, 3)]
}

Verifier code:
import ast
import sys

def verify():
    # Read full stdin
    payload = sys.stdin.read().strip()
    if not payload:
        print("Empty input.")
        return

    # 1. Parse using ast.literal_eval
    try:
        data = ast.literal_eval(payload)
    except Exception as e:
        print(f"Failed to parse input as Python literal: {e}")
        return

    # Verify it's a dictionary
    if not isinstance(data, dict):
        print("Input must be a Python dictionary.")
        return

    # 2. Verify keys
    expected_keys = {(1, 1), (1, 3), (2, 2), (3, 1), (3, 3)}
    if set(data.keys()) != expected_keys:
        print(f"Dictionary keys must be exactly {expected_keys}.")
        return

    # 3-7. Verify moves for each key
    for target, moves in data.items():
        if not isinstance(moves, list):
            print(f"Value for key {target} must be a list of moves.")
            return

        # 3. Initialize grid
        grid = [[False] * 5 for _ in range(5)]

        for move in moves:
            if not (isinstance(move, tuple) and len(move) == 2):
                print(f"Move {move} for key {target} is not a 2-tuple.")
                return
            r, c = move
            if type(r) is not int or type(c) is not int:
                print(f"Move {move} for key {target} contains non-integers.")
                return
            
            # 4. Verify bounds
            if not (0 <= r < 5 and 0 <= c < 5):
                print(f"Move {move} for key {target} is out of bounds.")
                return

            # 5. Toggle state of the cell and its valid orthogonal neighbors
            for dr, dc in [(0, 0), (-1, 0), (1, 0), (0, -1), (0, 1)]:
                nr, nc = r + dr, c + dc
                if 0 <= nr < 5 and 0 <= nc < 5:
                    grid[nr][nc] = not grid[nr][nc]

        # 6. Count True cells
        true_cells = []
        for r in range(5):
            for c in range(5):
                if grid[r][c]:
                    true_cells.append((r, c))

        # 7. Verify exactly one cell is True and matches target
        if len(true_cells) != 1:
            print(f"For key {target}, expected exactly 1 light on, but found {len(true_cells)} lights on.")
            return
        if true_cells[0] != target:
            print(f"For key {target}, the light on is at {true_cells[0]}, which does not match the target.")
            return

    print("True")

if __name__ == "__main__":
    verify()