Source: RMO-2009-P7
Category: Extremal Problems

Problem: "On a chessboard, the eight squares on a diagonal are called a fence. A rook starts moving from a square outside the fence. In each move, it goes from one square to another, satisfying: (1) It cannot move onto the fence; (2) Each square is moved to at most once. Find the maximum number of moves that cross the fence.
"

Answer: 47

Reference solution:
The answer is $47$.

Let the square in row $i$ and column $j$ be denoted by $(i,j)$. The fence consists of the eight squares $(i,i)$ for $1\le i\le 8$. We divide the remaining squares into four classes:
\[
\begin{aligned}
A&=\{(i,j):2\le j+1\le i\le 4\}\cup\{(i,j):5\le j\le i-1\le 7\},\\
B&=\{(i,j):2\le i+1\le j\le 4\}\cup\{(i,j):5\le i\le j-1\le 7\},\\
C&=\{(i,j):5\le i\le 8,\ 1\le j\le 4\},\\
D&=\{(i,j):1\le i\le 4,\ 5\le j\le 8\}.
\end{aligned}
\]
Thus $A$ and $C$ are below the fence, while $B$ and $D$ are above the fence. Also $|A|=|B|=12$, so $|A\cup B|=24$.

A rook move crosses the fence exactly when its two endpoints lie on opposite sides of the main diagonal. With the above coloring, every crossing is of one of the following two types:
\[
A\leftrightarrow D \qquad\text{or}\qquad B\leftrightarrow C.
\]
Indeed, a horizontal or vertical segment crossing the diagonal in the top-left or bottom-right part of the board has its near-diagonal endpoint in $A$ or $B$, and the other endpoint in $D$ or $C$ respectively.

Therefore every crossing has one endpoint in $A\cup B$. Since the rook never visits a square more than once, any fixed square in $A\cup B$ can be an endpoint of at most two crossing moves: one when the rook enters the square and one when it leaves. Hence the total number of crossings is at most
\[
2|A\cup B|=48.
\]

We now rule out equality. Suppose that $48$ crossings were possible. Then every square in $A\cup B$ would have to be used as an endpoint of exactly two crossing moves. In particular, no square in $A\cup B$ could be incident to a non-crossing move in the rook's path.

Consequently, once the path uses crossings of type $A\leftrightarrow D$, it cannot switch to crossings of type $B\leftrightarrow C$ through a non-crossing move involving $A$ or $B$. The only possible switch would have to pass between a square in $C$ and a square in $D$ without using $A$ or $B$. But this is impossible: squares of $C$ have rows $5,6,7,8$ and columns $1,2,3,4$, while squares of $D$ have rows $1,2,3,4$ and columns $5,6,7,8$, so no rook move can go directly from $C$ to $D$.

Thus, under the equality assumption, all crossings would have to be of a single type. If all crossings are of type $A\leftrightarrow D$, then only the $12$ squares of $A$ can serve as the required near-diagonal endpoints, giving at most $2|A|=24$ crossings. Similarly, if all crossings are of type $B\leftrightarrow C$, there are at most $2|B|=24$ crossings. This contradicts the assumption of $48$ crossings. Hence at most $47$ crossings are possible.

It remains to show that $47$ crossings can be achieved. The following sequence of visited squares does so:
\[
\begin{aligned}
&(8,2)\to(1,2)\to(7,2)\to(7,8)\to(7,3)\to(1,3)\to(8,3)\to(2,3)\\
&\to(6,3)\to(6,8)\to(6,2)\to(6,7)\to(6,4)\to(1,4)\to(8,4)\to(2,4)\\
&\to(7,4)\to(3,4)\to(5,4)\to(5,8)\to(5,3)\to(5,7)\to(5,2)\to(5,6)\\
&\to(8,6)\to(1,6)\to(7,6)\to(2,6)\to(2,1)\to(2,7)\to(8,7)\to(3,7)\\
&\to(3,1)\to(3,6)\to(3,2)\to(3,5)\to(8,5)\to(2,5)\to(7,5)\to(1,5)\\
&\to(6,5)\to(4,5)\to(4,1)\to(4,6)\to(4,2)\to(4,7)\to(4,3)\to(4,8).
\end{aligned}
\]
Each listed square is off the fence, no square is repeated, and each consecutive pair lies in a common row or column. Moreover, for every consecutive pair, the two endpoints lie on opposite sides of the main diagonal, so every one of the $47$ moves crosses the fence. Therefore the maximum number of crossings is exactly $47$.

Grading guidelines:
(Partial) 1. Observed/proved that a rook move crosses the fence iff its endpoints lie on opposite sides of the main diagonal (equivalently, one has i<j and the other i>j), since the rook moves horizontally/vertically and cannot land on the diagonal. 2. Established a global upper bound of the form “#crossings ≤ 48” by identifying a set of 24 off-diagonal squares near the fence (e.g. A∪B in the official partition) such that every crossing has an endpoint in this set, and arguing each such square can be an endpoint of at most two crossing moves (enter/leave) because squares are not revisited. 3. Gave an explicit long non-repeating rook path avoiding the fence that realizes at least 40 crossing moves (or, more generally, a concrete construction with a stated number of crossings and a verifiable check that every move crosses). (Almost) 1. Proved the upper bound 48 using the “each crossing uses A∪B, hence ≤2|A∪B|” endpoint-counting argument, and attempted to rule out equality by a switching/connectedness argument, but left a small gap in justifying why a 48-crossing path cannot mix the two crossing types (A↔D and B↔C). 2. Correctly proved that 48 is impossible (hence the maximum is ≤47), but did not supply a complete explicit path achieving 47, or gave a construction that is off by one move / has one repeated square / has one move that does not cross. 3. Presented a construction intended to achieve 47 crossings and verified legality (no fence, no repeats, rook moves), but did not fully check that every consecutive pair lies on opposite sides of the diagonal (or missed a boundary case where a move would cross through a fence square without landing on it).

Construction task:
Provide an explicit construction for the sequence of squares visited by the rook to achieve the maximum possible number of moves that cross the fence.

Construction format requirements:
- Output a single Python list of lists, where each inner list represents a square as `[r, c]`.
- Use 0-based indexing for the 8x8 chessboard, so `0 <= r <= 7` and `0 <= c <= 7`.
- The fence is defined as the main diagonal where `r == c`.
- The sequence must start with the initial square and contain all subsequently visited squares in order.
- The rook cannot move onto the fence (i.e., no square in the sequence can have `r == c`).
- Each square can be visited at most once.
- Every move must be a valid rook move (either `r` or `c` remains constant, and the other strictly changes).
- To maximize efficiency in your output, every move in your sequence must strictly cross the fence. A move from `[r1, c1]` to `[r2, c2]` crosses the fence if `(r1 - c1) * (r2 - c2) < 0`.
- The sequence must achieve the maximum possible number of fence-crossing moves for this board.
- Put ONLY the Python literal list of lists inside the `<construct>` block.

Reference construction:
[[7, 1], [0, 1], [6, 1], [6, 7], [6, 2], [0, 2], [7, 2], [1, 2], [5, 2], [5, 7], [5, 1], [5, 6], [5, 3], [0, 3], [7, 3], [1, 3], [6, 3], [2, 3], [4, 3], [4, 7], [4, 1], [4, 6], [4, 2], [4, 5], [7, 5], [0, 5], [6, 5], [1, 5], [1, 0], [1, 6], [7, 6], [2, 6], [2, 0], [2, 5], [2, 1], [2, 4], [7, 4], [1, 4], [6, 4], [0, 4], [5, 4], [3, 4], [3, 0], [3, 5], [3, 1], [3, 6], [3, 2], [3, 7]]

Verifier code:
import sys
import ast

def verify():
    payload = sys.stdin.read().strip()
    if not payload:
        print("Empty input.")
        return
    
    try:
        points = ast.literal_eval(payload)
    except Exception as e:
        print(f"Failed to parse input as Python literal: {e}")
        return
    
    if not isinstance(points, list):
        print("The construction must be a list.")
        return
        
    if len(points) != 48:
        print(f"Expected exactly 48 points, got {len(points)}.")
        return
        
    seen = set()
    for i, pt in enumerate(points):
        if not isinstance(pt, (list, tuple)):
            print(f"Point at index {i} is not a list or tuple.")
            return
        if len(pt) != 2:
            print(f"Point at index {i} does not have exactly 2 elements.")
            return
            
        r, c = pt
        if type(r) is not int or type(c) is not int:
            print(f"Coordinates of point at index {i} are not strictly integers.")
            return
            
        if not (0 <= r <= 7 and 0 <= c <= 7):
            print(f"Coordinates of point {pt} at index {i} are out of bounds (0-7).")
            return
            
        if r == c:
            print(f"Point {pt} at index {i} lies on the fence (r == c).")
            return
            
        tup = (r, c)
        if tup in seen:
            print(f"Point {pt} at index {i} is visited more than once.")
            return
        seen.add(tup)
        
    for i in range(len(points) - 1):
        r1, c1 = points[i]
        r2, c2 = points[i+1]
        
        # Check valid rook move
        if not ((r1 == r2 and c1 != c2) or (c1 == c2 and r1 != r2)):
            print(f"Move from {points[i]} to {points[i+1]} is not a valid rook move.")
            return
            
        # Check fence crossing
        if (r1 - c1) * (r2 - c2) >= 0:
            print(f"Move from {points[i]} to {points[i+1]} does not strictly cross the fence.")
            return
            
    print("True")

if __name__ == "__main__":
    verify()