델타를 이용한 2차원 배열
import java.util.Scanner;
class Solution {
public static void main(String args[]) throws Exception {
Scanner sc = new Scanner(System.in);
int T = sc.nextInt();
for(int test_case = 1; test_case <= T; test_case++) {
int N = sc.nextInt();
int[][] arr = new int[N][N];
int[] dr = {0, 1, 0, -1};
int[] dc = {1, 0, -1, 0};
int row = 0;
int col = 0;
int count = 1;
int status = 0;
while(count <= N * N) {
arr[row][col] = count;
int nr = row + dr[status];
int nc = col + dc[status];
if(nr < 0 || nr >= N || nc < 0 || nc >= N || arr[nr][nc] != 0 ) {
status = (status + 1) % 4;
}
row = nr;
col = nc;
count++;
}
System.out.println("#" + test_case);
for (int i = 0; i < N; i++) {
for(int j = 0; j < N; j++) {
System.out.print(arr[i][j] + " ");
}
System.out.println();
}
}
}
}