Problem:
Problem C
Changing the Sitting Arrangement
You are the teacher of a class of
n
2
pupils at an elementary school.
Seats in the classroom are set up in a square shape of
n
rows (row 1 through row
n
) by
n
columns (column 1 through column
n
).
You are planning a change in the sitting arrangement.
In order for your pupils to interact with many different pupils,
you want to make those currently on adjacent seats have remote seats.
There is at least one sitting arrangement such that
every pair of pupils currently on adjacent seats
have seats with the Manhattan distance of no less than
⌊
n
/ 2⌋ between them.
Your task is to find such an arrangement.
Here, ⌊
x
⌋ represents the largest integer
less than or equal to
x.
The Manhattan distance of two seats are the sum of the absolute difference
of their row numbers and that of their column numbers.
Adjacent seats mean those with the Manhattan distance 1.
For example, in the first dataset of Sample Input (
n
= 4), pupils at the four adjacent seats of the pupil no. 10 are those numbered 6, 9, 11, and 14.
  In the Output for the Sample Input corresponding to this, their seats have Manhattan distances 3, 3, 2, and 3 from the seat of the pupil no. 10, all of which are ⌊ 4 / 2 ⌋ = 2 or greater.
  This condition also holds for all the other pupils.
Input
The input consists of multiple datasets, each in the format below.
The number of datasets does not exceed 50.
n
a
11
a
12
⋯
a
1
n
a
21
a
22
⋯
a
2
n
⋮
a
n
1
a
n
2
⋯
a
n
n
Here,
n
is the number of rows and also columns of the seats in the classroom (2 ≤
n
≤ 50).
  The following
n
lines denote the current sitting arrangement.
Each
a
ij
gives the ID number of the pupil
currently sitting in the
i
-th row from the back and the
j
-th column
from the left, as seen from the teacher's desk.
a
ij
's are integers 1 through
n
2
,
without duplicates.
The end of the input is indicated by a line consisting of a single zero.
Output
For each dataset, output a sitting arrangement satisfying the condition stated above in the same format as in the input.
If there are two or more such arrangements, any of them will do.
Sample Input
4
1 2 3 4
5 6 7 8
9 10 11 12
13 14 15 16
7
1 8 15 22 29 36 43
2 9 16 23 30 37 44
3 10 17 24 31 38 45
4 11 18 25 32 39 46
5 12 19 26 33 40 47
6 13 20 27 34 41 48
7 14 21 28 35 42 49
0
Output for the Sample Input
6 1 14 8
16 13 11 9
4 10 2 12
5 3 15 7
4 20 18 44 12 16 36
23 38 40 48 7 39 26
29 9 14 35 37 2 49
13 46 30 5 45 43 24
42 33 17 22 19 27 47
1 3 21 25 11 15 41
31 6 8 34 28 32 10

Submitted solutions:

code_snippet: #include <bits/stdc++.h>
using namespace std;
using ll = long long;
#define rep(i, s, t) for (ll i = s; i < (ll)(t); i++)
#define rrep(i, s, t) for(ll i = (ll)(t) - 1; i >= (ll)(s); i--)
#define all(x) begin(x), end(x)
#define rall(x) rbegin(x), rend(x)

#define TT template<typename T>
TT using vec = vector<T>;
template<class T1, class T2> bool chmin(T1 &x, T2 y) { return x > y ? (x = y, true) : false; }
template<class T1, class T2> bool chmax(T1 &x, T2 y) { return x < y ? (x = y, true) : false; }

struct io_setup {
    io_setup() {
        ios::sync_with_stdio(false);
        std::cin.tie(nullptr);
        cout << fixed << setprecision(15);
    }
} io_setup;

bool check(int N,vector<vector<int>>X,vector<vector<int>>Y){
    vector<pair<int,int>>at(N*N+1);
    rep(i,0,N)rep(j,0,N){
        at[Y[i][j]]={i,j};
    }
    rep(i,0,N)rep(j,0,N)rep(k,0,N)rep(l,0,N){
        if(abs(i-k)+abs(j-l)==1){
            int a=X[i][j],b=X[k][l];
            auto[x,y]=at[a];
            auto[z,w]=at[b];
            if(abs(x-z)+abs(y-w)<N/2)return false;
        }
    }
    return true;
}

int main() {
    while(1){
        int N;
        cin>>N;
        if(N==0)return 0;
        vector A(N,vector<int>(N));
        rep(i,0,N)rep(j,0,N)cin>>A[i][j];
        vector ans(N,vector<int>(N,-1));
        if(N%2==0){
            rep(i,0,N){
                rep(j,0,N){
                    if(i%2==0){
                        if(j%2==0){
                            ans[i/2][j]=A[i][j];
                        }else{
                            ans[i/2+N/2][j]=A[i][j];
                        }
                    }else{
                        if(j%2==0){
                            ans[(N%4==0?i/2+N/2:i/2)][(j+N/2)%N]=A[i][j];
                        }else{
                            ans[(N%4!=0?i/2+N/2:i/2)][(j+N/2)%N]=A[i][j];
                        }
                    }
                }
            }
        }else{
            rep(i,0,N){
                rep(j,0,N){
                    if(j%2==1)ans[i][j/2+(N+1)/2]=A[i][j];
                    else ans[i][j/2]=A[i][j];
                }
            }
            vector<vector<int>>X(N);
            for(int i=0;i<N;i+=2)X[i/2]=ans[i];
            for(int i=1;i<N;i+=2)X[(N+1)/2+i/2]=ans[i];
            swap(ans,X);
        }
        check(N,A,ans);
        rep(i,0,N){
            rep(j,0,N)cout<<ans[i][j]<<" ";
            cout<<endl;
        }
    }
}
accuracy: 1
time_ms: 40
memory_kb: 3840
final_rank: 4

code_snippet: #include <bits/extc++.h>
using namespace std;

#define rep(i, n) for(int i = 0; i < (int)(n); i++)
typedef vector<int> vi;

bool solve();

int main() {
	while (solve());
	return 0;
}

bool solve() {
	int n;
	cin >> n;
	if (n == 0) return false;
	vector<vi> a(n, vi(n));
	rep(i, n) rep(j, n) cin >> a[i][j];

	for (int i = 1; i < n; i += 2) {
    for (int j = 0; j < n; j += 2) cout << a[i][j] << " ";
    for (int j = 1; j < n; j += 2) cout << a[n-i-1][j] << " \n"[j + 2 >= n];
	}
  for (int i = 0; i < n; i += 2) {
    for (int j = 0; j < n; j += 2) cout << a[i][j] << " ";
    for (int j = 1; j < n; j += 2) cout << a[n-i-1][j] << " \n"[j + 2 >= n];
  }

	return true;
}
accuracy: 1
time_ms: 10
memory_kb: 3188
final_rank: 1

code_snippet: #include <bits/stdc++.h>
using namespace std;
using ll = long long;
#define rep(i,n) for(int i = 0;i < (int)n;i++)

vector<vector<ll>> clc(vector<vector<ll>> a){
  ll n = a.size();
  vector<vector<ll>> ret;
  rep(i,n){
    vector<ll> gu,ki;
    rep(j,n){
      if(j % 2 == 0){
        gu.push_back(a[i][j]);

      }else{
        ki.push_back(a[i][j]);
      }
    }
    reverse(gu.begin(),gu.end());
    reverse(ki.begin(),ki.end());
    vector<ll> now; 
    for(auto & p:gu){
      now.push_back(p);
    }
    for(auto &p:ki){
      now.push_back(p);
    }
    ret.push_back(now);

  }
  return ret;
}

vector<vector<ll>> tenti(vector<vector<ll>> &a){
  ll n = a.size();
  vector<vector<ll>> b(n,vector<ll>(n));
  rep(i,n){
    rep(j,n){
      b[j][i] = a[i][j];
    }
  }
  return b;
}

void solve(ll n,vector<vector<ll>> x){
  
  x = clc(x);
  x = tenti(x);
  x = clc(x);
  rep(i,n){
    rep(j,n){
      cout << x[i][j] << " " ;
    }
    cout << endl;
  }


}


int main(){
  while(true){
    ll n;cin >> n;
    vector<vector<ll>> x(n,vector<ll>(n));
    rep(i,n){
      rep(j,n){
        cin >> x[i][j];
      }
    }
    if(n == 0)break;
    solve(n,x);
  }
}
accuracy: 1
time_ms: 10
memory_kb: 3496
final_rank: 2

code_snippet: #include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (int i = 0; i < (n); i++)

void test(vector<vector<int>> &x) {
  map<int, pair<int, int>> mp;
  rep(i, x.size()) rep(j, x.size()) {
    mp[x[i][j]] = { i, j };
  }
  vector Grid(x.size(), vector<int>(x.size()));
  int idx = 0;
  rep(i, x.size()) rep(j, x.size()) {
    Grid[i][j] = ++idx;
  }
  rep(i, x.size() - 1) rep(j, x.size() - 1) {
    auto [i1, j1] = mp[Grid[i][j]];
    if (i > 0) {
      auto [i2, j2] = mp[Grid[i - 1][j]];
      if (abs(i1 - i2) + abs(j1 - j2) < x.size() / 2) {
        cerr << Grid[i][j] << " " << Grid[i - 1][j] << endl;
        exit(0);
      }
    }
    if (j > 0) {
      auto [i2, j2] = mp[Grid[i][j - 1]];
      if (abs(i1 - i2) + abs(j1 - j2) < x.size() / 2) {
        cerr << Grid[i][j] << " " << Grid[i][j - 1] << endl;
        exit(0);
      }
    }
    if (i + 1 < x.size()) {
      auto [i2, j2] = mp[Grid[i + 1][j]];
      if (abs(i1 - i2) + abs(j1 - j2) < x.size() / 2) {
        cerr << Grid[i][j] << " " << Grid[i + 1][j] << endl;
        exit(0);
      }
    }
    if (j + 1 < x.size()) {
      auto [i2, j2] = mp[Grid[i + 1][j]];
      if (abs(i1 - i2) + abs(j1 - j2) < x.size() / 2) {
        cerr << Grid[i][j] << " " << Grid[i][j + 1] << endl;
        exit(0);
      }
    }
  }
}

int main() {
  while (true) {
    int n;
    cin >> n;
    if (n == 0)
      break;
    vector Grid(n, vector<int>(n));
    rep(i, n) rep(j, n) cin >> Grid[i][j];
    if (n == 2 || n == 3) {
      rep(i, n) rep(j, n) {
        cout << Grid[i][j] << " \n"[j == n - 1];
      }
      continue;
    }

    map<int, int> mp;
    int idx = 0;
    rep(i, n) rep(j, n) {
      mp[idx] = Grid[i][j];
      Grid[i][j] = idx++;
    }
    bool isOdd = n % 2;
    if (isOdd)
      n--;
    int mid = n / 2;
    vector nGrid = Grid;
    rep(i, n) rep(j, n) {
      if ((i + j) % 2 == 0) {
        nGrid[i][j] = Grid[(i + mid) % n][(j + mid) % n];
      }
    }
    swap(nGrid, Grid);
    if (isOdd) {
      n++;
      vector<int> num(0);
      for (int i = 0; i < n; i += 2) {
        num.push_back(Grid[i][n - 1]);
      }
      for (int i = n - 3; i >= 0; i -= 2) {
        num.push_back(Grid[n - 1][i]);
      }
      int idx = 0;
      for (int i = 0; i < n; i += 2) {
        Grid[i][n - 1] = num[(idx + n / 2) % num.size()];
        idx++;
      }
      for (int i = n - 3; i >= 0; i -= 2) {
        Grid[n - 1][i] = num[(idx + n / 2) % num.size()];
        idx++;
      }
    }
    rep(i, n) rep(j, n) {
      cout << mp[Grid[i][j]] << " \n"[j == n - 1];
      // cout << Grid[i][j] << " \n"[j == n - 1];
    }
    // test(Grid);
  }
}
accuracy: 1
time_ms: 10
memory_kb: 3564
final_rank: 3