Source: EGMO-2019-P2
Category: Extremal Problems

Problem: "Given a positive integer $n$. Place several dominoes on a $2n\\times 2n$ grid such that every cell of the grid is adjacent to exactly one cell covered by a domino. Find the maximum number of dominoes that can be placed. Note: A domino is a $1\\times2$ or $2\\times1$ grid. Each domino must be placed on two cells of the grid, and different dominoes cannot overlap. Two distinct cells are called adjacent if they share a common edge.
"

Answer: $(n+1)n/2$

Reference solution:
We will prove that the maximum number of dominoes that can be placed is $\frac{n^2+n}{2}$. 

Let the cells of the $2n \times 2n$ grid be denoted by coordinates $(x,y)$ for $1 \le x, y \le 2n$. We define the "layer" or "depth" of a cell as its distance from the boundary of the grid:
$$ d(x,y) = \min(x, 2n+1-x, y, 2n+1-y) $$
Note that $1 \le d(x,y) \le n$. We color the grid with two colors, black and white, by defining the set of black cells $B$ as those belonging to odd layers:
$$ B = \{ (x,y) \mid d(x,y) \text{ is odd} \} $$

**Claim 1:** Every cell in the grid is adjacent to exactly two black cells.
*Proof.* Let $(x,y)$ be a cell in layer $m$, so $d(x,y) = m$. Its neighbors can only belong to layers $m-1$, $m$, or $m+1$. 
- If $(x,y)$ is not a corner of its layer, it has exactly two neighbors in layer $m$, one neighbor in layer $m-1$, and one neighbor in layer $m+1$. Since $m-1$ and $m+1$ have the same parity, which is opposite to the parity of $m$, the cell $(x,y)$ has either 2 neighbors in an odd layer (if $m$ is odd) and 2 in an even layer, or 2 neighbors in an even layer (if $m$ is even) and 2 in an odd layer. In either case, it has exactly 2 neighbors in $B$.
- If $(x,y)$ is a corner of layer $m$, it has exactly two neighbors in layer $m$, two neighbors in layer $m-1$, and zero neighbors in layer $m+1$. By the same parity argument, exactly two of its neighbors belong to $B$. (For $m=1$, the layer $m-1$ is empty, meaning the corner cell has only 2 neighbors in total, both of which are in layer $1 \subseteq B$).
Thus, every cell in the grid has exactly two black neighbors.

**Claim 2:** The total number of black cells is $2n^2 + 2n$.
*Proof.* The layer $L_m = \{(x,y) \mid d(x,y) = m\}$ forms a square border of size $(2n-2m+2) \times (2n-2m+2)$. The number of cells in $L_m$ is $4(2n-2m+1) = 8n - 8m + 4$. Summing this over all odd $m \in \{1, 3, \dots\}$ up to $n$ yields exactly $2n^2 + 2n$ black cells.

**Upper Bound:**
Let $D$ be the set of cells covered by the dominoes. Suppose there are $k$ dominoes; since each domino covers exactly 2 cells, $|D| = 2k$. 
The problem requires that every cell in the grid is adjacent to exactly one cell in $D$. Therefore, the total number of adjacencies (edges) between the set of all grid cells and $D$ must equal the total number of grid cells.
By restricting our focus to the black cells, the number of edges between $B$ and $D$ is exactly $|B| = 2n^2 + 2n$, because each black cell has exactly one neighbor in $D$. 
On the other hand, by Claim 1, every cell in $D$ is adjacent to exactly two black cells. Thus, the number of edges between $B$ and $D$ can also be counted as $2|D| = 4k$. 
Equating the two counts gives $4k = 2n^2 + 2n$, which implies $k = \frac{n^2+n}{2}$. This proves that any valid placement must consist of exactly $\frac{n^2+n}{2}$ dominoes.

**Construction of exactly $\frac{n^2+n}{2}$ dominoes:**
To show that such a placement is always possible, we construct it layer by layer. For each odd layer $m \in \{1, 3, \dots\}$ with $m \le n$, the subgrid bounded by $L_m$ has side length $S = 2n - 2m + 2$. We place exactly $S - 1$ dominoes on $L_m$ using local coordinates $(i,j)$ for $1 \le i, j \le S$ (which correspond to global coordinates $(i+m-1, j+m-1)$).

- **If $S \equiv 2 \pmod 4$:**
  - **Top edge:** Horizontal dominoes covering $(1, j)$ and $(1, j+1)$ for $j \in \{1, 5, 9, \dots, S-1\}$.
  - **Bottom edge:** Horizontal dominoes covering $(S, j)$ and $(S, j+1)$ for $j \in \{3, 7, 11, \dots, S-3\}$.
  - **Left edge:** Vertical dominoes covering $(i, 1)$ and $(i+1, 1)$ for $i \in \{4, 8, 12, \dots, S-2\}$.
  - **Right edge:** Vertical dominoes covering $(i, S)$ and $(i+1, S)$ for $i \in \{4, 8, 12, \dots, S-2\}$.

- **If $S \equiv 0 \pmod 4$:**
  - **Top edge:** Horizontal dominoes covering $(1, j)$ and $(1, j+1)$ for $j \in \{2, 6, 10, \dots, S-2\}$.
  - **Bottom edge:** Horizontal dominoes covering $(S, j)$ and $(S, j+1)$ for $j \in \{4, 8, 12, \dots, S-4\}$.
  - **Left edge:** Vertical dominoes covering $(i, 1)$ and $(i+1, 1)$ for $i \in \{3, 7, 11, \dots, S-1\}$.
  - **Right edge:** Vertical dominoes covering $(i, S)$ and $(i+1, S)$ for $i \in \{3, 7, 11, \dots, S-1\}$.

This staggered boundary placement ensures that every cell in the grid is adjacent to exactly one covered cell. Summing the dominoes placed across all odd layers gives $\sum_{m \text{ odd}} (S - 1) = \frac{n^2+n}{2}$.

Thus, the maximum number of dominoes that can be placed is $\frac{n^2+n}{2}$.

Grading guidelines:
(Partial) 1. Introduced the layer depth d(x,y)=min(x,2n+1−x,y,2n+1−y) and the colouring by parity of d, and correctly proved the key local fact that every cell has exactly two neighbours in odd layers (equivalently: exactly two black neighbours). 2. Counted the number of black cells by summing the sizes of the odd layers (or an equivalent correct counting), obtaining |B|=2n^2+2n (or an equivalent explicit formula). 3. Set up a correct double-counting of edges between black cells and covered cells D to derive a linear relation 4k=|B| (or equivalent), thus giving the upper bound k≤(n^2+n)/2, even if the details are incomplete. (Almost) 1. Gave the correct double-counting argument and obtained k=(n^2+n)/2 as a necessary value, but did not provide a complete existence construction (or only sketched it without verifying the adjacency condition everywhere). 2. Provided a near-complete layer-by-layer construction achieving (n^2+n)/2 dominoes, but left a small gap (e.g. one boundary/parity case S≡0 or 2 (mod 4) not checked, or a corner/last-layer adjacency not fully verified). 3. Solution is almost complete, but made minor mistakes which are not negligible.

Construction task:
For the specific case $n=80$, provide an explicit construction for the maximum number of dominoes.

Construction requirements:
- Output a single Python expression (using list comprehensions) that evaluates to a flat list of tuples.
- Each tuple must be a 1-indexed coordinate `(r, c)` representing a cell on the $160 \times 160$ grid.
- Adjacent tuples at indices $2i$ and $2i+1$ represent a single domino covering those two cells.
- The configuration must maximize the number of dominoes placed, while satisfying the condition that every cell in the $160 \times 160$ grid is adjacent to exactly one cell covered by a domino.

Output formatting requirements:
- The output must be a single, valid Python expression.
- Do NOT wrap the output in markdown code blocks (e.g., no ```python ... ```).
- Do NOT include any explanations, comments, print statements, or assignments.
- Put ONLY the Python expression inside the construction block.

Reference construction:
[cell for k in range(1, 41) for j in range(1, 42 - k) for cell in ((2*k-1, 2*k + 4*j - 4), (2*k-1, 2*k + 4*j - 3))] + [cell for k in range(40, 1, -1) for j in range(1, 42 - k) for cell in ((164 - 2*k, 2*k + 4*j - 4), (164 - 2*k, 2*k + 4*j - 3))] + [cell for k in range(1, 41) for col in (2*k - 1, 162 - 2*k) for j in range(1, 42 - k) for cell in ((2*k + 4*j - 3, col), (2*k + 4*j - 2, col))]

Verifier code:
import sys

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

    try:
        # Evaluate the raw construction payload to support list comprehensions
        dominoes_cells = eval(payload)
    except Exception as e:
        print(f"Failed to parse input: {e}")
        return

    if not isinstance(dominoes_cells, list):
        print("Output must be a list.")
        return
        
    if len(dominoes_cells) != 6480:
        print(f"Expected exactly 6480 cells (3240 dominoes), got {len(dominoes_cells)}.")
        return

    for cell in dominoes_cells:
        if not (isinstance(cell, tuple) and len(cell) == 2):
            print(f"Invalid cell format: {cell}. Expected a 2-tuple.")
            return
        r, c = cell
        if type(r) is not int or type(c) is not int:
            print(f"Coordinates must be integers: {cell}.")
            return
        if not (1 <= r <= 160 and 1 <= c <= 160):
            print(f"Coordinates out of bounds: {cell}. Expected range [1, 160].")
            return

    for i in range(0, 6480, 2):
        c1 = dominoes_cells[i]
        c2 = dominoes_cells[i+1]
        dist = abs(c1[0] - c2[0]) + abs(c1[1] - c2[1])
        if dist != 1:
            print(f"Cells at indices {i} and {i+1} do not form a valid domino: {c1}, {c2}. Manhattan distance is {dist}, expected 1.")
            return

    covered_cells = set(dominoes_cells)
    if len(covered_cells) != 6480:
        print("Overlapping dominoes detected. Covered cells are not completely distinct.")
        return

    # Check neighbor count for every cell in the 160x160 grid
    for r in range(1, 161):
        for c in range(1, 161):
            neighbors = [
                (r-1, c),
                (r+1, c),
                (r, c-1),
                (r, c+1)
            ]
            count = sum(1 for nr, nc in neighbors if (nr, nc) in covered_cells)
            if count != 1:
                print(f"Cell ({r}, {c}) is adjacent to {count} covered cells, expected exactly 1.")
                return

    print("True")

if __name__ == "__main__":
    verify()