Universal Cup Judging System

Universal Cup

Time Limit: 3.0 s Memory Limit: 1024 MB Total points: 100 Hackable ✓
Statistics

给定一个正偶数 $n$。考虑一个 $n\times n$ 的网格,其行和列均从 $1$ 到 $n$ 编号。第 $r$ 行、第 $c$ 列的格子记为 $(r,c)$。

你的任务是将网格中的全部 $n^2$ 个格子排列成一个序列 $p_1,p_2,\ldots,p_{n^2}$,使每个格子恰好出现一次,并且约定 $p_{n^2+1}=p_1$ 后,对于从 $1$ 到 $n^2$ 的每个 $i$,格子 $p_i$ 和 $p_{i+1}$ 位于同一行或同一列,且满足

$$ |r_i-r_{i+1}|+|c_i-c_{i+1}|=((i-1)\bmod 4)+1, $$

其中 $p_i=(r_i,c_i)$,$p_{i+1}=(r_{i+1},c_{i+1})$。

换言之,这个序列应当是一条长度为 $n^2$ 的闭合路径,恰好访问每个格子一次,每次移动均为水平或竖直移动,移动长度按照序列 $1,2,3,4,1,2,3,4,\ldots$ 循环重复,包括最后从 $p_{n^2}$ 返回 $p_1$ 的移动。

如果存在这样的序列,输出其中任意一个。

输入格式

第一行包含一个偶数 $n$($4\le n\le 200$)。

输出格式

如果不存在合法序列,输出一行 NO。

否则,第一行输出 YES,然后输出 $n$ 行,每行包含 $n$ 个整数。第 $i$ 行、第 $j$ 列应包含一个整数 $k$,表示在你的答案中,$p_k$ 是格子 $(i,j)$。

如果有多个合法答案,输出任意一个即可。

样例

输入格式 1

8

输出格式 1

YES
1 7 25 26 8 27 37 38
2 60 23 33 59 48 42 41
5 6 16 34 57 46 45 39
3 21 22 52 58 28 43 53
64 19 24 35 9 47 36 54
62 61 15 32 10 49 31 40
4 18 17 51 56 50 44 55
63 20 14 13 11 29 30 12

说明

problem_20738_9261fca9ca37e680062124022dec552c.png

第 1 次移动(浅色);第 64 次移动,返回起点(深色)。

Discussions

About Discussions

The discussion section is only for posting: General Discussions (problem-solving strategies, alternative approaches), and Off-topic conversations.

This is NOT for reporting issues! If you want to report bugs or errors, please use the Issues section below.

Open Discussions 0
No discussions in this category.

Issues

About Issues

If you find any issues with the problem (statement, scoring, time/memory limits, test cases, etc.), you may submit an issue here. A problem moderator will review your issue.

Guidelines:

  1. This is not a place to publish discussions, editorials, or requests to debug your code. Issues are only visible to you and problem moderators.
  2. Do not submit duplicated issues.
  3. Issues must be filed in English or Chinese only.
Active Issues 0
No issues in this category.
Closed/Resolved Issues 0
No issues in this category.