#include #include #include int main() { using namespace std; int n; cin >> n; auto to_3dig = [&](int n) -> string { string res = to_string(n); while (res.size() < 3) { res = "0" + res; } return res; }; constexpr int dx[] = {0, 1, 0, -1}; constexpr int dy[] = {1, 0, -1, 0}; vector ans(n, vector(n, "")); auto ok = [&](int x, int y, int d) -> bool { int nx = x + dx[d], ny = y + dy[d]; if (nx < 0 || nx >= n || ny < 0 || ny >= n) { return false; } if (ans[nx][ny] != "") { return false; } return true; }; int x = 0, y = 0, d = 0; for (int num = 1; num <= n * n; num++) { ans[x][y] = to_3dig(num); if (!ok(x, y, d)) { d = (d + 1) % 4; } x += dx[d]; y += dy[d]; } for (int i = 0; i < n; i++) { for (int j = 0; j < n; j++) { cout << ans[i][j] << " \n"[j == n - 1]; } } }