C. Not Adjacent Matrix

Problem - C - Codeforces

We will consider the numbers aa and bb as adjacent if they differ by exactly one, that is, |a−b|=1|a−b|=1.

We will consider cells of a square matrix n×nn×n as adjacent if they have a common side, that is, for cell (r,c)(r,c) cells (r,c−1)(r,c−1), (r,c+1)(r,c+1), (r−1,c)(r−1,c) and (r+1,c)(r+1,c) are adjacent to it.

For a given number nn, construct a square matrix n×nn×n such that:

  • Each integer from 11 to n2n2 occurs in this matrix exactly once;
  • If (r1,c1)(r1,c1) and (r2,c2)(r2,c2) are adjacent cells, then the numbers written in them must not be adjacent.

Input

The first line contains one integer tt (1≤t≤1001≤t≤100). Then tt test cases follow.

Each test case is characterized by one integer nn (1≤n≤1001≤n≤100).

Output

For each test case, output:

  • -1, if the required matrix does not exist;
  • the required matrix, otherwise (any such matrix if many of them exist).

The matrix should be outputted as nn lines, where each line contains nn integers.

Example

input

Copy

3
1
2
3

output

Copy

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

思路:那么就先输出所有奇数或者所有偶数叭

#include<iostream>
using namespace std;
int t,n;
void solve()
{
    cin>>n;
    if(n==1)cout<<1<<endl;
    else if(n==2)cout<<-1<<endl;
   else
    {
         int count=0;
        for(int i=1;i<=n*n;i++)
        {
            if(i%2==0)
            {
                  ++count;
                cout<<i<<" ";
                if(count==n){cout<<endl;count=0;}
            }
        }
        for(int i=1;i<=n*n;i++)
        {
            if(i%2!=0)
            {
                count++;
                cout<<i<<" ";
                if(count==n){cout<<endl;count=0;}
            }
        }
    }
}
int main()
{
    cin>>t;
    while(t--)
    {
        solve();
    }
}

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值