forked from tanishqchamola/Hacktober2020
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathEVENM.cpp
54 lines (52 loc) · 1008 Bytes
/
EVENM.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
/*
Problem : EVENM
Contest : JUNE20B
Author : sid_155
*/
#include<bits/stdc++.h>
using namespace std;
void giveme(stack<int> &odd,stack<int> &even,int sq){
for(int i=1;i<=sq;i++){
if(i%2==0){
even.push(i);
}
else
odd.push(i);
}
}
int main(){
int t;
cin>>t;
while(t--){
int n;
cin>>n;
int square = n*n;
stack<int> odd,even;
giveme(odd,even,square);
int sqsq[n+1][n+1] = {0};
for(int i=1;i<=n;i++){
for(int j=1;j<=n;j++){
if((i+j)%2 == 0){
sqsq[i][j] = odd.top();
odd.pop();
}
else{
if(!even.empty()){
sqsq[i][j] = even.top();
even.pop();
}
else{
sqsq[i][j] = odd.top();
odd.pop();
}
}
}
}
for(int i=1;i<=n;i++){
for(int j=1;j<=n;j++){
cout<<sqsq[i][j]<<" ";
}
cout<<endl;
}
}
}