Problem Statement:
An n‑order Sudoku means that for the numbers 1‑n, each appears exactly once in every row and column. For any two adjacent numbers that are in the same row of the Sudoku, we construct the corresponding ordered pair (i, j). For example, if a row contains … i j …, then the ordered pair (i, j) can be formed. Our goal is to find a valid n‑order Sudoku such that for every ordered pair (i, j) with
1\leq i , j \leq n,\; i \neq j
it holds that across the entire n‑order Sudoku, (i, j) appears exactly once.
Input: n
Output: If no such Sudoku arrangement exists, output "NaN"; if a valid arrangement exists, output any one such Sudoku in matrix form.
I think I understand the case where n is even, but I’m still not quite sure about the case where n is odd.
For the case where n is even, first consider a permutation of 0 to n‑1.
First row: 0, n‑1, 1, n‑2, 2, n‑3 … n/2‑1, n/2
The first n/2 rows: each row is obtained from the previous row by adding 1 modulo n to each position.
The last n/2 rows: take the first n/2 rows, reverse their order, and place them sequentially as the last n/2 rows.
Example: n = 6:
051423
102534
213045
540312
435201
324150
The proof is not difficult and is left as an exercise… It feels that the main idea is to construct a permutation whose difference array modulo n has no repetitions… then a cyclic shift works… The odd case feels a bit mysterious; for n = 3 I tried and it seems to have no solution.
At first I constructed the “rotational central‑symmetric” case with n = 4, and then I guessed whether all 2^k are possible . But when I tried to follow the “rotational” construction method, I couldn’t get the case for n = 8.
It turned out that last night, shortly after I posted the question, a classmate who asked me the question also constructed n = 6, although his construction was rather messy and had no obvious pattern. At that time we were wondering whether odd numbers don’t work and even numbers do , but after some trial we still couldn’t find a general solution for the n = 2k case…
Anyway, many thanks to K‑brother for the answer . I’ve passed it on to that classmate .