https://swexpertacademy.com/main/code/problem/problemSolver.do?contestProbId=AV5PobmqAPoDFAUq
#include <iostream>
#include <vector>
using namespace std;
void solution(int N) {
int num = 1;
int level = 0;
vector<vector<int>> list;
for (int i = 0; i < N; i++) {
vector<int> tmp(N, 0);
list.push_back(tmp);
}
if (N == 1) {
cout << num << endl;
return;
}
while (num <= N * N) {
int minus = 2 * level;
for (int i = 0; i < N -1 - minus && num <=N*N; i++) {
list[level][level + i] = num++;
}
for (int i = 0; i < N - 1 - minus && num <= N * N; i++) {
list[level+i][N-1-level] = num++;
}
for (int i = N-1; i > minus && num <= N * N; i--) {
list[N-1-level][i-level] = num++;
}
for (int i = N-1; i >minus && num <= N * N; i--) {
list[i-level][level] = num++;
}
level++;
if (N % 2 == 1 && level == N / 2) {
list[N / 2][N / 2] = num++;
}
}
for (int i = 0; i < N; i++) {
for (int n : list[i]) {
cout << n << " ";
}
cout << endl;
}
}
int main() {
int T; cin >> T;
for (int i = 1; i <= T; i++) {
int n; cin >> n;
cout << "#" << i << endl;
solution(n);
}
return 0;
}
'전공공부 > 코딩테스트' 카테고리의 다른 글
(c++) SWEA "1928) Base64 Decoder" (0) | 2022.05.28 |
---|---|
(c++) SWEA "1204) 최빈수" (0) | 2022.05.25 |
(c++) SWEA "1206) 1일차 - View" (0) | 2022.05.25 |
(c++) SWEA "2072) 홀수만 더하기" (0) | 2022.05.25 |
(c++) SWEA "1859) 백만 장자 프로젝트" (0) | 2022.05.25 |