-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy path1520C - Not Adjacent Matrix.cpp
71 lines (54 loc) · 1.44 KB
/
1520C - Not Adjacent Matrix.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
/*
We will consider the numbers a and b as adjacent if they differ by exactly one, that is, |a-b|=1.
We will consider cells of a square matrix n×n as adjacent if they have a common side, that is, for cell (r,c) cells (r,c-1), (r,c+1), (r-1,c) and (r+1,c) are adjacent to it.
For a given number n, construct a square matrix n×n such that:
Each integer from 1 to n2 occurs in this matrix exactly once;
If (r1,c1) and (r2,c2) are adjacent cells, then the numbers written in them must not be adjacent.
Input
The first line contains one integer t (1=t=100). Then t test cases follow.
Each test case is characterized by one integer n (1=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 n lines, where each line contains n integers.
Example
inputCopy
3
1
2
3
outputCopy
1
-1
2 9 7
4 6 3
1 8 5
*/
#include<bits/stdc++.h>
using namespace std;
int main(){
ios_base::sync_with_stdio(false);
cin.tie(NULL);
int t;
cin >> t;
while (t--) {
int n;
cin >> n;
if (n == 1) cout << 1;
else if (n == 2) cout << -1;
else {
int num = 1;
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
if (num > n * n) num = 2;
cout << num << " ";
num += 2;
}
cout << endl;
}
}
cout << endl;
}
return 0;
}