Source: RMO-2018-P4
Category: Existence and Construction

Problem: "In an $n\\times n$ grid, some squares are painted black such that the bottom-left square $L$ and the top-right square $R$ are not painted black, and every possible path of a knight from $L$ to $R$ passes through at least one black square. Determine all integers $n\\geq4$ for which there must exist three diagonally adjacent squares, at least two of which are painted black.
"

Answer: All integers that are congruent to $1$ modulo $3$ and greater than $1$

Reference solution:
The answer is all integers $n \equiv 1 \pmod 3$ with $n > 1$.

Number the rows from bottom to top and the columns from left to right consecutively as $1,2,\dots,n$. Let the coordinates of the bottom-left square $L$ be $(1,1)$ and the top-right square $R$ be $(n,n)$.

**Case 1: $n = 3k$**
Color black all squares in rows whose numbers are congruent to $2 \pmod 3$. 
In this coloring, there is no set of three diagonally adjacent squares with at least two black squares, because out of any three consecutive rows, exactly one is congruent to $2 \pmod 3$. 
If a knight starts from $L(1,1)$ and does not pass through any black square, it can only move between the uncolored rows $y \equiv 1 \pmod 3$ and $y \equiv 0 \pmod 3$. A knight move from $y \equiv 1 \pmod 3$ must land in $y' \equiv 0 \pmod 3$, and vice versa. By checking the possible changes in coordinates, one can verify the invariant that for any reachable square $(x,y)$, $x \equiv y \pmod 2$ if $y \equiv 1 \pmod 3$, and $x \not\equiv y \pmod 2$ if $y \equiv 0 \pmod 3$. Since $R=(3k, 3k)$ has $y \equiv 0 \pmod 3$ but $x \equiv y \pmod 2$, it violates the invariant and cannot be reached. Thus, this coloring satisfies all requirements.

**Case 2: $n = 3k+2$**
Color black all squares in rows whose numbers are congruent to $0 \pmod 3$. 
Analogous to the previous case, no three diagonally adjacent squares contain more than one black square. The uncolored rows are $y \equiv 1 \pmod 3$ and $y \equiv 2 \pmod 3$. The invariant for reachable squares becomes $x \equiv y \pmod 2$ for $y \equiv 1 \pmod 3$, and $x \not\equiv y \pmod 2$ for $y \equiv 2 \pmod 3$. The square $R=(3k+2, 3k+2)$ has $y \equiv 2 \pmod 3$ but $x \equiv y \pmod 2$, so it is unreachable.

**Case 3: $n = 3k+1$**
We prove by induction that the statement holds (i.e., no such coloring exists) when $n=3k+1$. It suffices to show that if the statement were false, there would exist a path for the knight from $L$ to $R$ containing no black squares, contradicting the hypothesis.

*Base case $k=1$ ($n=4$):* By the contradiction assumption, the diagonally adjacent squares $(1,4), (2,3), (3,2)$ cannot contain two black squares. Thus $(2,3)$ and $(3,2)$ are not both black. Assume without loss of generality that $(2,3)$ is unpainted; then the path $L(1,1) \rightarrow (2,3) \rightarrow R(4,4)$ contains no black squares, a contradiction.

*Inductive step:* Assume $k>1$ and that the statement holds for all smaller values of $k$; we consider the case $k$. For a square $(a,b)$, denote $(a',b')=(a+3k-3,\;b+3k-3)$.
By the contradiction assumption, the squares $(2,3)$ and $(3,2)$ are not both black; assume without loss of generality that $(2,3)$ is unpainted. 
If the square $(2',3')$ is also unpainted, then by the induction hypothesis (applied to the $(3k-2) \times (3k-2)$ subgrid from $(2,3)$ to $(2',3')$) there exists a path from $(2,3)$ to $(2',3')$ containing no black squares. Consequently, the path $L \rightarrow (2,3) \rightarrow \dots \rightarrow (2',3') \rightarrow R$ contains no black squares.

If the square $(2',3')$ is painted black, then by the contradiction assumption the square $(3',2')$ is unpainted. To block the symmetric unpainted path $L \rightarrow (3,2) \rightarrow \dots \rightarrow (3',2') \rightarrow R$, we may assume that $(3,2)$ is painted black. 
Furthermore, if $(4,4)$ were unpainted, the induction hypothesis applied to the subgrid from $(4,4)$ to $(4',4')$ would yield an unpainted path from $(4,4)$ to $R=(4',4')$, creating the unpainted path $L \rightarrow (2,3) \rightarrow (4,4) \rightarrow \dots \rightarrow R$. Thus, we may assume the square $(4,4)$ is painted black. Similarly, to block the path $L \rightarrow \dots \rightarrow (1',1') \rightarrow (3',2') \rightarrow R$, we may assume the square $(1',1')$ is painted black.

By the contradiction assumption, any three consecutive squares on a diagonal can contain at most one black square. Thus, the black squares $(3,2)$ and $(4,4)$ force their diagonally adjacent squares to be unpainted. This ensures that the knight can go from $L$ to the square $(1,4)$ entirely via forced unpainted squares (e.g., $L \rightarrow (2,3) \rightarrow (3,5) \rightarrow (1,4)$). Similarly, the black squares $(1',1')$ and $(2',3')$ force a sequence of their diagonal neighbors to be unpainted, providing an unpainted path from $(1',4')$ to $R$. 

Finally, by the induction hypothesis applied to the $(3k-2) \times (3k-2)$ subgrid from $(1,4)$ to $(1',4')$, the knight can travel from $(1,4)$ to $(1',4')$ without landing on any black squares. Concatenating these segments yields a valid knight path from $L$ to $R$ with no black squares, which completes the induction and establishes the contradiction.

Grading guidelines:
(Partial) 1. For n≡0 or 2 (mod 3), gave an explicit coloring (e.g. all squares in one residue class of rows mod 3) and correctly argued that it blocks all knight paths from L to R. 2. Proved the “no forced triple” property for the construction: in any three diagonally adjacent squares, at most one can be black (e.g. because among three consecutive rows exactly one is a black row). 3. Established a correct invariant for knight moves restricted to the unpainted rows (typically combining y mod 3 with x≡y (mod 2) / x≢y (mod 2)) and used it to show R is unreachable under the proposed construction. (Almost) 1. Correctly proved the positive direction for n≡1 (mod 3) by induction/reduction to a smaller subgrid, but left a localized gap in one of the forced-unpainted path concatenations near the boundary (e.g. reaching (1,4) or (1',4') without hitting black squares). 2. Solved the base case n=4 and set up the inductive step for n=3k+1, including the necessary WLOG choices (e.g. assuming (2,3) unpainted, then considering (2',3') black/unpainted), but did not fully justify one of the “to block the symmetric path, we may assume … is black” implications. 3. Completed all three cases and obtained the correct answer set, but made a minor non-negligible mistake in one congruence/invariant check (e.g. the parity condition at R in Case 1/2) while the overall method is clearly correct.

Construction task:
For the specific case $n=81$, provide an explicit construction of the grid coloring that satisfies all the problem's conditions (i.e., $L$ and $R$ are white, every knight path from $L$ to $R$ contains at least one black square, and no three diagonally adjacent squares have two or more black squares).

Construction requirements:
- Output a single Python expression that evaluates to a list of lists of integers, representing the $81 \times 81$ grid.
- Use `0` for white squares and `1` for black squares.
- The outer list should contain 81 inner lists (rows), ordered from top to bottom. Each inner list should contain 81 integers representing the columns from left to right.
- The expression must be self-contained and evaluate directly to the target data structure when passed to `eval()`.
- You may use list comprehensions, conditional expressions, and integer constants. Do not use assignment statements, imports, or any non-expression syntax.
- Put ONLY the raw Python expression inside the construction block. Do not wrap it in markdown code blocks or add any other text.

Reference construction:
[[1 if i % 3 == 1 else 0 for j in range(81)] for i in range(81)]

Verifier code:
import sys
from collections import deque

def verify():
    raw_input = sys.stdin.read().strip()
    if not raw_input:
        print("Empty input.")
        return
        
    try:
        # The instruction allows list comprehensions, so we use eval instead of ast.literal_eval
        grid = eval(raw_input)
    except Exception as e:
        print(f"Failed to parse input: {e}")
        return
        
    # 2. Verify grid dimensions and elements
    if not isinstance(grid, list):
        print("Grid must be a list.")
        return
    if len(grid) != 81:
        print(f"Grid must have exactly 81 rows, got {len(grid)}.")
        return
        
    for r_idx, row in enumerate(grid):
        if not isinstance(row, list):
            print(f"Row {r_idx} is not a list.")
            return
        if len(row) != 81:
            print(f"Row {r_idx} must have exactly 81 elements, got {len(row)}.")
            return
        for c_idx, val in enumerate(row):
            if type(val) is not int or val not in (0, 1):
                print(f"Element at ({r_idx}, {c_idx}) is not strictly the integer 0 or 1.")
                return
                
    # 3. Verify L and R are 0
    if grid[80][0] != 0:
        print("Bottom-left square (80, 0) must be 0 (white).")
        return
    if grid[0][80] != 0:
        print("Top-right square (0, 80) must be 0 (white).")
        return
        
    # 4. Iterate through all valid starting positions for a diagonal of length 3
    # Main diagonals
    for r in range(79):
        for c in range(79):
            count = grid[r][c] + grid[r+1][c+1] + grid[r+2][c+2]
            if count >= 2:
                print(f"Main diagonal starting at ({r}, {c}) has {count} black squares (must be strictly less than 2).")
                return
                
    # Anti-diagonals
    for r in range(79):
        for c in range(2, 81):
            count = grid[r][c] + grid[r+1][c-1] + grid[r+2][c-2]
            if count >= 2:
                print(f"Anti-diagonal starting at ({r}, {c}) has {count} black squares (must be strictly less than 2).")
                return
                
    # 5. Initialize a BFS queue to find all reachable squares from (80, 0) using valid knight moves
    queue = deque([(80, 0)])
    visited = {(80, 0)}
    
    knight_moves = [
        (-2, -1), (-2, 1), (-1, -2), (-1, 2),
        (1, -2), (1, 2), (2, -1), (2, 1)
    ]
    
    while queue:
        r, c = queue.popleft()
        
        # 6. Assert that the top-right square (0, 80) is not reachable
        if (r, c) == (0, 80):
            print("Top-right square (0, 80) is reachable from bottom-left square without hitting a black square.")
            return
            
        for dr, dc in knight_moves:
            nr, nc = r + dr, c + dc
            if 0 <= nr < 81 and 0 <= nc < 81:
                if (nr, nc) not in visited:
                    # Restricted only to squares where grid[r][c] == 0
                    if grid[nr][nc] == 0:
                        visited.add((nr, nc))
                        queue.append((nr, nc))
                        
    print("True")

if __name__ == "__main__":
    verify()