#include #include using namespace std; vector > odd(int n) { vector > a(n, vector(n)); int y = 0; int x = n / 2; for (int i = 1; i <= n * n; ++i) { a[y][x] = i; int nextY = (y + n - 1) % n; int nextX = (x + 1) % n; if (a[nextY][nextX] == 0) { y = nextY; x = nextX; } else { y = (y + 1) % n; } } return a; } int main() { int n; cin >> n; vector > a(n, vector(n)); if (n % 2 == 1) { a = odd(n); } else if (n % 4 == 0) { int i = 1; for (int y = 0; y < n; ++y) { for (int x = 0; x < n; ++x) { if ((y + 1) % 4 < 2) { if ((x + 1) % 4 < 2) { a[y][x] = i; } } else { if ((x + 1) % 4 >= 2) { a[y][x] = i; } } ++i; } } i = 1; for (int y = n - 1; y >= 0; --y) { for (int x = n - 1; x >= 0; --x) { if ((y + 1) % 4 < 2) { if ((x + 1) % 4 >= 2) { a[y][x] = i; } } else { if ((x + 1) % 4 < 2) { a[y][x] = i; } } ++i; } } } else if (n % 4 == 2) { int smaller = (n / 4) * 2 + 1; vector > base = odd(smaller); for (int y = 0; y < smaller; ++y) { for (int x = 0; x < smaller; ++x) { base[y][x] = (base[y][x] - 1) * 4; } } enum Type { U, L, X }; vector > types(smaller, vector(smaller, L)); for (int x = 0; x < smaller; ++x) { types[smaller / 2 + 1][x] = U; } for (int y = smaller / 2 + 2; y < smaller; ++y) { for (int x = 0; x < smaller; ++x) { types[y][x] = X; } } types[smaller / 2][smaller / 2] = U; types[smaller / 2 + 1][smaller / 2] = L; int offsetL[2][2] = { 4, 1, 2, 3 }; int offsetU[2][2] = { 1, 4, 2, 3 }; int offsetX[2][2] = { 1, 4, 3, 2 }; for (int y = 0; y < n; ++y) { for (int x = 0; x < n; ++x) { Type type = types[y / 2][x / 2]; a[y][x] = base[y / 2][x / 2] + (type == L ? offsetL[y % 2][x % 2] : type == U ? offsetU[y % 2][x % 2] : offsetX[y % 2][x % 2]); } } } for (int y = 0; y < n; ++y) { for (int x = 0; x < n; ++x) { cout << a[y][x] << " "; } cout << endl; } return 0; }