// yukicodr // No.401 数字の渦巻き // //#include "stdafx.h" #include #include #include //list #include //tree #include //連想配列 #include //hash #include //hash #include #include using namespace std; #define RIGHT (1) #define LEFT (2) #define UP (3) #define DOWN (4) vector > mass; int N; void solve(int i, int x, int y, int dir) { if (i > N * N) return; mass[y][x] = i; switch (dir) { case RIGHT: if (x + 1 < N && mass[y][x+1] == 0) { x++; } else { y++; dir = DOWN; } break; case LEFT: if (x - 1 >= 0 && mass[y][x - 1] == 0) { x--; } else { y--; dir = UP; } break; case UP: if (y - 1 >= 0 && mass[y-1][x] == 0) { y--; } else { x++; dir = RIGHT; } break; case DOWN: if (y + 1 < N && mass[y+1][x] == 0) { y++; } else { x--; dir = LEFT; } break; } solve(i+1,x,y,dir); } int main() { cin >> N; mass.resize(N); for (int i = 0; i < N; i++) { mass[i].resize(N); } for (int y = 0; y < N; y++) { for (int x = 0; x < N; x++) { mass[y][x] = 0; } } solve(1, 0, 0, RIGHT); for (int y = 0; y < N; y++) { for (int x = 0; x < N; x++) { cout << setfill('0') << setw(3) << right << mass[y][x]<<" "; } cout << endl; } //getchar(); return 0; }