(Hungary 2004). A palace has the shape of a square divided into $2003 \times 2003$ rooms, like the squares of a large chessboard. There is a door between two rooms if and only if they share a wall. The main door allows, coming from outside the palace, to enter the palace through the room located at the northwest corner. A person enters the palace, visits some of the rooms, then leaves the palace, through the entrance door, when they return to the northwest corner room for the first time. It turns out that they visited each of the other rooms exactly 100 times, except for the room located at the southeast corner. How many times did the visitor go into the southeast corner room?
. The room has been visited 99 times.

More generally, consider a palace of $n \times n$ rooms, each of which has been visited exactly $p \geqslant 1$ times, except for the one located at the northwest corner, which has been visited exactly twice, and
the one located at the southeast corner, which has been visited exactly $x$ times. We will prove that if $n$ is odd then $x=p-1$, and if $n$ is even then $x=2 p-1$.

We start by coloring each room alternately in white or black, like a chessboard, so that the two rooms located at the northwest and southeast corners are black. Note that when moving through the palace, one always moves from a white room to a black room and vice versa.

If $n$ is odd:

In total, there are $\frac{n^{2}+1}{2}$ black rooms and $\frac{n^{2}-1}{2}$ white rooms. Since each white room has been visited exactly $p$ times and the person always leaves a white room to enter a black room, the total number of transitions from a white room to a black room during the walk is $p \frac{n^{2}-1}{2}$.

However, if we eliminate the first entry into the northwest corner room (coming from outside), this corresponds to the total number of visits to black rooms during the walk. The northwest corner room has then been visited only once. This number of visits is therefore equal to $1+x+p\left(\frac{n^{2}+1}{2}-2\right)$.

It follows that $1+x+p\left(\frac{n^{2}+1}{2}-2\right)=p \frac{n^{2}-1}{2}$, which simplifies to $x=p-1$.

If $n$ is even:

The reasoning is the same but with $\frac{n^{2}}{2}$ black rooms and $\frac{n^{2}}{2}$ white rooms. This time we arrive at $1+x+p\left(\frac{n^{2}}{2}-2\right)=p \frac{n^{2}}{2}$, which simplifies to $x=2 p-1$.