Skip to content

Instantly share code, notes, and snippets.

@qjatn0120
Created March 23, 2023 14:16
Show Gist options
  • Save qjatn0120/7590773b72dd4b4de86cc975eeb2ed89 to your computer and use it in GitHub Desktop.
Save qjatn0120/7590773b72dd4b4de86cc975eeb2ed89 to your computer and use it in GitHub Desktop.
#ifdef DEBUG
#include "debug.h"
#endif // DEBUG
#ifndef DEBUG
template <typename T>
void debug(T &x){}
template <typename T>
void debug(T &x, int i, int j){}
#endif // DEBUG
#include <bits/stdc++.h>
using namespace std;
int main(){
cin.tie(nullptr), ios::sync_with_stdio(false);
int T;
cin >> T;
while(T--){
int N;
cin >> N;
if(N == 4){
cout << "12 10 6 14\n3 8 4 1\n5 15 11 9\n2 7 13 16\n";
}else{
vector <int> v;
for(int i = 1; i <= N; i+= 2) v.push_back(i);
for(int i = 2; i <= N; i+= 2) v.push_back(i);
for(int r = 0; r < N; r++){
for(int i : v) cout << (i + r * N) << " ";
cout << "\n";
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment