Educational Codeforces Round 141 (Rated for Div. 2) B-Matrix of Differences

题目链接:https://codeforces.com/contest/1783/problem/B

题干:

For a square matrix of integers of size n×nn×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 (1432)(1342) 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×n, where each integer from 11 to n2n2 occurs exactly once, such that its beauty is the maximum possible among all such matrices.

Input

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

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

Output

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

要求:

time limit per test2 seconds

memory limit per test256 megabytes

inputstandard input

outputstandard output

思路:

让尽量相差大的数相临,从而得到尽量大的差,才能保证找到最多的数字。一开始考虑一排从前填一排从后填,相临着填,发现无法完成最优,考虑斜着填。如图所示:

考虑用bfs,对行列相加为奇数则从后往前,对偶数则从前天,用队列不断写入,再传入该点的右边和下边的坐标,注意判断边界和打标记,防止重复执行。

代码:

ps:最优解可能不止一种哦。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

爱吃芒果的蘑菇

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值