QOJ.ac

QOJ

Time Limit: 1 s Memory Limit: 64 MB Total points: 100

#804. 杰作

Statistics

超级计算机“深思”(Deep Thought)很久以前就已经回答了关于生命、宇宙以及一切的终极问题,现在它无事可做。最近,另一个问题突然摆在了它面前。一位著名的艺术家一直在寻找一种创作“理想”杰作的方法,并为此花费了 42 年时间寻找解决方案。他决定直接向“深思”请教。

这个问题对超级计算机来说太简单了,在艺术家提出问题后的 42 秒内,它就给出了解决方案。该算法设计如下:

  1. 取任何形状为正方形的世界名画。例如,你可以选择列奥纳多·达·芬奇的《蒙娜丽莎》。
  2. 制作这幅画的副本。将其切割成 $n^2$ 个大小相同的正方形碎片。如果你能在真迹上进行此操作,效果会更好。
  3. 将 $1$ 到 $n^2$ 的数字分配给得到的正方形碎片。
  4. 重新排列这些正方形,以确保每一行和每一列的 $n$ 个数字之和相等。
  5. 为了完成杰作,非常重要的一点是确保每两个相邻(边相邻)元素的差的绝对值大于 $1$。

“深思”拒绝提供针对给定维度 $n$ 的杰作生成程序。因此,你必须自己完成它。

输入格式

输入仅包含一行,为一个正整数 $n$,表示切割后的画作的行数和列数。

$$1 \le n \le 42$$

输出格式

第一行输出数字 $n$。接下来的 $n$ 行,每行包含 $n$ 个整数,表示打印出的矩阵。打印出的矩阵应符合上述给定的约束条件以构成杰作。如果存在多个解,你可以输出其中任意一个。如果不存在解,则应输出 -1。

样例

样例输入 1

5

样例输出 1

5
12 14 11 13 15
22 24 21 23 25
7 9 6 8 10
17 19 16 18 20
2 4 1 3 5

样例输入 2

3

样例输出 2

-1

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.