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();
}
}