#include #define ALL(x) (x).begin(), (x).end() #define LB(v, x) (int)(lower_bound(ALL(v), x) - (v).begin()) #define UQ(v) sort(ALL(v)), (v).erase(unique(ALL(v)), (v).end()) #define IO ios::sync_with_stdio(false), cin.tie(nullptr); #define chmax(a, b) (a) = (a) < (b) ? (b) : (a) #define chmin(a, b) (a) = (a) < (b) ? (a) : (b) using namespace std; using ll = long long; const int INF = 1e9 + 10; const ll INFL = 4e18; const vector dx = {0, 1, 0, -1, 1, 1, -1, -1}; const vector dy = {1, 0, -1, 0, 1, -1, 1, -1}; // 0下 1右 2上 3左 4右下 5右上 6左下 7左上 int main() { ll N; cin >> N; vector> ans(N, vector(N)); int x = 0, y = 0; int type = 0; if (N % 2 == 0) type = 1; int idx = 1; for (int t = N; t >= 1; t--) { if (t % 2 == 1) { vector jun; int gx, gy; // 0下 1右 2上 3左 4右下 5右上 6左下 7左上 // 右下 上 左上 右 if (type == 0) jun = {4, 2, 7, 1}, gx = x + t - 1, gy = y; // 左下 上 右上 左 else jun = {6, 2, 5, 3}, gx = x - t + 1, gy = y; int d = 0, d2 = t - 1; while (true) { ans[y][x] = idx++; if (x == gx && y == gy) break; if (d % 2 == 0) x += dx[jun[d]] * d2, y += dy[jun[d]] * d2, d2--; else x += dx[jun[d]], y += dy[jun[d]]; d++, d %= 4; } if (type == 0) x--, y++; else x++, y++; } else { vector jun; int px = x, py = y; // 0下 1右 2上 3左 4右下 5右上 6左下 7左上 // 左上 右 右下 上 int shote; if (type == 0) jun = {7, 1, 4, 2}, shote = 0; // 左下 上 右上 左 else jun = {6, 2, 5, 3}, shote = 1; ans[y][x] = idx++; x += dx[shote] * (t - 1); y += dy[shote] * (t - 1); int d = 0, d2 = t - 1; while (true) { if (x < 0 || x >= N || y < 0 || y >= N || ans[y][x] != 0) break; ans[y][x] = idx++; if (d % 2 == 0) x += dx[jun[d]] * d2, y += dy[jun[d]] * d2, d2--; else x += dx[jun[d]], y += dy[jun[d]]; d++, d %= 4; } if (type == 0) x = px - 1, y = py + 1; else x = px + 1, y = py + 1; type = 1 - type; } } for (int i = 0; i < N; i++) { for (int j = 0; j < N; j++) { printf("%5d ", ans[i][j]); } cout << endl; } }