CF1783B.Matrix of Differences

普及-

通过率:0%

时间限制:2.00s

内存限制:256MB

AC君温馨提醒

该题目为【codeforces】题库的题目,您提交的代码将被提交至codeforces进行远程评测,并由ACGO抓取测评结果后进行展示。由于远程测评的测评机由其他平台提供,我们无法保证该服务的稳定性,若提交后无反应,请等待一段时间后再进行重试。

题目描述

For a square matrix of integers of size n×nn \times n, let's define its beauty as follows: for each pair of side-adjacent elements xx and yy, write out the number ∣x−y∣|x-y|, and then find the number of different numbers among them.

For example, for the matrix (1342)\begin{pmatrix} 1 & 3\\ 4 & 2 \end{pmatrix} the numbers we consider are ∣1−3∣=2|1-3|=2, ∣1−4∣=3|1-4|=3, ∣3−2∣=1|3-2|=1 and ∣4−2∣=2|4-2|=2; there are 33 different numbers among them (22, 33 and 11), which means that its beauty is equal to 33.

You are given an integer nn. You have to find a matrix of size n×nn \times n, where each integer from 11 to n2n^2 occurs exactly once, such that its beauty is the maximum possible among all such matrices.

对于一个 n×nn \times n 的整数方阵,我们定义其“优美度”如下:对每一对相邻(共享一条边)的元素 xx 和 yy,计算 ∣x−y∣|x-y|,然后统计这些差值中互不相同的数的个数。

例如,对于矩阵 (1342)\begin{pmatrix} 1 & 3\\ 4 & 2 \end{pmatrix},我们考虑的差值为 ∣1−3∣=2|1-3|=2、∣1−4∣=3|1-4|=3、∣3−2∣=1|3-2|=1 和 ∣4−2∣=2|4-2|=2;其中共有 33 个不同的数(即 22、33 和 11),因此该矩阵的优美度为 33。

给定一个整数 nn。你需要构造一个 n×nn \times n 的矩阵,使得 11 到 n2n^2 中的每个整数恰好出现一次,并且该矩阵的优美度在所有满足条件的矩阵中达到最大可能值。

输入格式

The first line contains a single integer tt (1≤t≤491 \le t \le 49) – the number of test cases.

The first (and only) line of each test case contains a single integer nn (2≤n≤502 \le n \le 50).

第一行包含一个整数 tt(1≤t≤491 \le t \le 49)——测试用例的数量。

每个测试用例的第一行(也是唯一一行)包含一个整数 nn(2≤n≤502 \le n \le 50)。

输出格式

For each test case, print nn rows of nn integers — a matrix of integers of size n×nn \times n, where each number from 11 to n2n^2 occurs exactly once, such that its beauty is the maximum possible among all such matrices. If there are multiple answers, print any of them.

对于每个测试用例,输出 nn 行,每行 nn 个整数——即一个大小为 n×nn \times n 的整数矩阵,其中 11 到 n2n^2 中的每个数字恰好出现一次,且该矩阵的“美观度”在所有此类矩阵中达到最大可能值。若存在多个满足条件的答案,输出任意一个即可。

输入输出样例

  • 输入#1

    2
    2
    3

    输出#1

    1 3
    4 2
    1 3 4
    9 2 7
    5 8 6

输入解题思路,AI测评打分。不知道怎么写?

首页