// #include // Temp fix for gcc13 global pragma // #pragma GCC target("avx2,bmi2,popcnt,lzcnt") // #pragma GCC optimize("O3,unroll-loops") #include // #include using namespace std; #if __cplusplus >= 202002L using namespace numbers; #endif #ifdef LOCAL #include "Debug.h" #else #define debug_endl() 42 #define debug(...) 42 #define debug2(...) 42 #define debug_bin(...) 42 #endif namespace direction_vectors{ vector> dr2{{1, 0}, {0, 1}}; vector> dr4{{1, 0}, {0, 1}, {-1, 0}, {0, -1}}; vector> dr4diag{{1, 1}, {-1, 1}, {-1, -1}, {1, -1}}; vector> dr8{{1, 0}, {1, 1}, {0, 1}, {-1, 1}, {-1, 0}, {-1, -1}, {0, -1}, {1, -1}}; vector> drk{{2, 1}, {1, 2}, {-1, 2}, {-2, 1}, {-2, -1}, {-1, -2}, {1, -2}, {2, -1}}; vector> generate(int low, int high){ assert(0 <= low && low <= high); int th = sqrt(high) + 1; vector> dr; for(auto x = -th; x <= th; ++ x) for(auto y = -th; y <= th; ++ y) if(auto d = x * x + y * y; low <= d && d <= high) dr.push_back({x, y}); return dr; } } int main(){ cin.tie(0)->sync_with_stdio(0); cin.exceptions(ios::badbit | ios::failbit); auto dr = direction_vectors::dr4; int n; cin >> n; vector coef(n, vector(n)); for(auto &x: coef | ranges::views::join){ cin >> x; } const int m = min(n, 4); vector a(m * m); for(auto i = 0; i < m; ++ i){ for(auto j = 0; j < m; ++ j){ a[m * i + j] = n * i + j; } } auto get_cost = [&](const vector &a)->long long{ long long cost = 0; for(auto i = 0; i < m; ++ i){ for(auto j = 0; j < m; ++ j){ cost += 1LL * coef[i][j] * a[m * i + j]; } } return cost; }; map> mp; set> found{a}; mp[get_cost(a)] = a; deque> dq{a}; while(!dq.empty()){ auto a = dq.front(); dq.pop_front(); int zi = -1, zj = -1; for(auto i = 0; i < m; ++ i){ for(auto j = 0; j < m; ++ j){ if(a[m * i + j] == 0){ zi = i, zj = j; goto DONE; } } } DONE:; for(auto [di, dj]: dr){ int in = zi + di, jn = zj + dj; if(0 <= min(in, jn) && max(in, jn) < n){ swap(a[m * zi + zj], a[m * in + jn]); if(!found.contains(a)){ auto ca = get_cost(a); if(mp.contains(ca)){ auto b = mp[ca]; cout << "Yes\n"; for(auto rep = 2; rep; -- rep){ for(auto i = 0; i < n; ++ i){ for(auto j = 0; j < n; ++ j){ if(i < m && j < m){ cout << a[m * i + j] << " "; } else{ cout << n * i + j << " "; } } cout << "\n"; } swap(a, b); } return 0; } mp[ca] = a; found.insert(a); dq.push_back(a); } swap(a[m * zi + zj], a[m * in + jn]); } } } cout << "No\n"; return 0; } /* */