From d1f56b93d9d29f90514a320d2d66676e1bb442d1 Mon Sep 17 00:00:00 2001 From: ramrami-B <21801034@handong.ac.kr> Date: Wed, 17 Jan 2024 00:24:15 +0900 Subject: [PATCH] =?UTF-8?q?Create=201954=EB=8B=AC=ED=8C=BD=EC=9D=B4.java?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...\353\213\254\355\214\275\354\235\264.java" | 64 +++++++++++++++++++ 1 file changed, 64 insertions(+) create mode 100644 "\353\260\261\355\225\230\353\236\214/1954\353\213\254\355\214\275\354\235\264.java" 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