diff --git "a/\353\260\261\355\225\230\353\236\214/1954\353\213\254\355\214\275\354\235\264.java" "b/\353\260\261\355\225\230\353\236\214/1954\353\213\254\355\214\275\354\235\264.java" new file mode 100644 index 00000000..307f1334 --- /dev/null +++ "b/\353\260\261\355\225\230\353\236\214/1954\353\213\254\355\214\275\354\235\264.java" @@ -0,0 +1,64 @@ +import java.util.*; + +public class Solution { + static int[] dX = {0, 1, 0, -1}; + static int[] dY = {1, 0, -1, 0}; + + public static void main(String args[]) throws Exception { + Scanner sc = new Scanner(System.in); + + int T = sc.nextInt(); + + for(int t=1; t < T+1; t++) { + int N = sc.nextInt(); + + System.out.println("#" + t); + + int[][] snail = new int[N][N]; + boolean[][] visited = new boolean[N][N]; + + int num = 1; + int x = 0; + int y = 0; + int dir = 0; + + while(num < N*N+1) { + snail[x][y] = num++; + visited[x][y] = true; + + int dx = x + dX[dir]; + int dy = y + dY[dir]; + + if(isIn(N, dx, dy) && visited[dx][dy] == false) { + x = dx; + y = dy; + } else { + dir++; + dir %= 4; + x += dX[dir]; + y += dY[dir]; + } + } + + printSnail(N, snail); + } + sc.close(); + } + + private static void printSnail(int n, int[][] snail) { + for(int i = 0; i < n; i++) { + for(int j = 0; j < n; j++) { + System.out.print(snail[i][j]); + System.out.print(" "); + } + System.out.println(""); + } + } + + private static boolean isIn(int n, int x, int y) { + if(-1 < x && x < n && -1 < y && y < n) + return true; + else + return false; + } +} \ No newline at end of file