#include <iostream>
#include <memory.h>
#define MAX_N 10
using namespace std;
int board[MAX_N][MAX_N];
void setSnailNumber(int startX, int startY, int n, int val) {
if (n == 1) {
board[startX][startY] = val;
return;
}
if (n <= 0) {
return;
}
// 맨 위 가로
int increment = 0;
for (int c = startY; c < startY + n; c++) {
board[startX][c] = val + increment++;
}
// 맨 우측 세로
for (int r = startX + 1; r < startX + n; r++) {
board[r][startY + n - 1] = val + increment++;
}
// 맨 아래 가로
for (int c = startY + n - 2; c >= startY; c--) {
board[startX + n - 1][c] = val + increment++;
}
// 맨 좌측 세로
for (int r = startX + n - 2; r > startX; r--) {
board[r][startY] = val + increment++;
}
setSnailNumber(startX + 1, startY + 1, n - 2, val + increment);
}
int main(int argc, char** argv)
{
int test_case;
int T;
// freopen("input.txt", "r", stdin);
cin >> T;
for (test_case = 1; test_case <= T; ++test_case)
{
int n;
cin >> n;
memset(board, 0, sizeof(board));
setSnailNumber(0, 0, n, 1);
cout << "#" << test_case << "\n";
for (int r = 0; r < n; r++) {
for (int c = 0; c < n; c++) {
cout << board[r][c] << " ";
}
cout << "\n";
}
}
return 0;//정상종료시 반드시 0을 리턴해야합니다.
}
Leave a comment