Temp
unknown
c_cpp
a year ago
2.6 kB
9
Indexable
#pragma GCC optimize("Ofast")
#pragma GCC optimize("no-stack-protector")
#pragma GCC optimize("unroll-loops")
#pragma GCC target("sse,sse2,sse3,ssse3,popcnt,abm,mmx,tune=native")
#pragma GCC optimize("fast-math")
#pragma GCC optimize("Ofast")
#pragma GCC optimize("unroll-loops")
#pragma GCC target("avx,avx2,fma")
#include <bits/stdc++.h>
using namespace std;
#define pi (3.141592653589)
#define mod 1000000007
#define pb push_back
#define is insert
#define mp make_pair
#define ff first
#define ss second
#define all(x) x.begin(), x.end()
#define min3(a, b, c) min(c, min(a, b))
#define min4(a, b, c, d) min(d, min(c, min(a, b)))
#define rfr(n) for (int i = n - 1; i >= 0; i--)
#define rep1(i, a, b) for (long long i = a; i <= b; i++)
#define fr(n) for (long long i = 0; i < n; i++)
#define nesfr(x, y) \
for (long long i = 0; i < x; i++) \
for (long long j = 0; j < y; j++)
#define rep(i, a, b) for (long long i = a; i < b; i++)
#define fast ios_base::sync_with_stdio(false), cin.tie(nullptr), cout.tie(nullptr);
typedef long long int ll;
typedef long double ld;
typedef vector<ll> vi;
#define nl cout << "\n"
typedef uint32_t u32;
const unsigned int M = 1000000007;
const int N = 2e5 + 5;
void traverse(ll row, ll col, vector<vector<ll>> &a, vector<vector<ll>> &vis)
{
queue<pair<ll, ll>> q;
q.push({row, col});
vis[row][col] = 1;
a[row][col] = 3;
ll tracker = 0;
while (!q.empty())
{
int x = q.front().ff;
int y = q.front().ss;
q.pop();
vi delrow = {-1, 0, 1, 0};
vi delcol = {0, 1, 0, -1};
rep(i, 0, 4)
{
int newx = x + delrow[i];
int newy = y + delcol[i];
if (newx >= 0 && newx < a.size() && newy >= 0 && newy < a[0].size() && vis[newx][newy] == 0)
{
tracker++;
q.push({newx, newy});
a[newx][newy] = tracker;
vis[newx][newy] = 1;
}
}
}
}
signed main()
{
fast;
ll t;
cin >> t;
while (t--)
{
ll n;
cin >> n;
if (n == 2)
{
cout << -1 << endl;
}
else
{
vector<vector<ll>> vis(n, vector<ll>(n, 0));
vector<vector<ll>> a(n, vector<ll>(n, 0));
traverse(n - 1, 1, a, vis);
a[n - 1][1] = n * n;
for (auto it : a)
{
for (auto it1 : it)
{
cout << it1 << " ";
}
nl;
}
}
}
}
Editor is loading...
Leave a Comment