#include #include using namespace std; int main() { int N, H = 0, W = 0, ans[30][30] = {}, tmp = 0; cin >> N; for (int i = 0; i < N * N; ++i) { ans[H][W] = i + 1; if (tmp % 4 == 0) { if (W >= N - 1 || ans[H][W + 1] != 0) { ++H; ++tmp; } else ++W; } else if (tmp % 4 == 1) { if (H >= N - 1 || ans[H + 1][W] != 0) { --W; ++tmp; } else ++H; } else if (tmp % 4 == 2) { if (W <= 0 || ans[H][W - 1] != 0) { --H; ++tmp; } else --W; } else { if (H <= 0 || ans[H - 1][W] != 0) { ++W; ++tmp; } else --H; } } for (int i = 0; i < N; ++i) { for (int j = 0; j < N; ++j) cout << setw(3) << setfill('0') << ans[i][j] << ' '; cout << endl; } }