From f1ae0d525b9ceab968da292b739f6a0db035698b Mon Sep 17 00:00:00 2001 From: SehyeonLee <909am@neostack.co.kr> Date: Thu, 6 Feb 2025 20:15:59 +0900 Subject: [PATCH] =?UTF-8?q?1913=EB=AC=B8=EC=A0=9C(=EB=8B=AC=ED=8C=BD?= =?UTF-8?q?=EC=9D=B4)=20=EB=8B=A4=EB=A5=B8=20=ED=92=80=EC=9D=B4?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- 2025-02-01/terry/b_1913.java | 14 ++----- 2025-02-01/terry/b_1913_2.java | 70 ++++++++++++++++++++++++++++++++++ 2 files changed, 74 insertions(+), 10 deletions(-) create mode 100644 2025-02-01/terry/b_1913_2.java diff --git a/2025-02-01/terry/b_1913.java b/2025-02-01/terry/b_1913.java index 013ed43..c7e3fad 100644 --- a/2025-02-01/terry/b_1913.java +++ b/2025-02-01/terry/b_1913.java @@ -23,22 +23,16 @@ public static void main(String[] args) throws IOException { arr[x][y] = count; - /** - * 0: 상 - * 1: 우 - * 2: 하 - * 3: 좌 - */ int v = 0; // 방향 int step = 1; // 증감 값 int repeat = 0; // 두 번씩 반복 + int[] dx = {-1, 0, 1, 0}; + int[] dy = {0, 1, 0, -1}; while (x >= 0 && y >= 0) { for (int i = 0; i < step; i++) { count++; - if (v == 0) x -= 1; // 상 - if (v == 1) y += 1; // 우 - if (v == 2) x += 1; // 하 - if (v == 3) y -= 1; // 좌 + x += dx[v]; + y += dy[v]; if (x < 0 || y < 0) break; arr[x][y] = count; } diff --git a/2025-02-01/terry/b_1913_2.java b/2025-02-01/terry/b_1913_2.java new file mode 100644 index 0000000..fe9ccf1 --- /dev/null +++ b/2025-02-01/terry/b_1913_2.java @@ -0,0 +1,70 @@ +package study.d_250201; + +import java.util.*; +import java.io.*; + +public class b_1913_2 { + public static void main(String[] args) throws IOException { + BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); + BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(System.out)); + + //given + int n = Integer.parseInt(br.readLine()); + int m = Integer.parseInt(br.readLine()); + int[][] arr = new int[n][n]; + + + // when + int count = 1; + + int half = n / 2; + int step = 2; + + int x = half; + int y = half; + arr[half][half] = count; + + int[] dx = {0, 1, 0, -1}; + int[] dy = {1, 0, -1, 0}; + + int[] vx = {1, 0, -1, 0}; + int[] vy = {0, -1, 0, 0}; + int v = 0; // 0, 1, 2, 3 + + for (int i = 0; i < half; i++) { + x = half - i - 1; + y = half - i; + for (int j = 0; j < 4; j++) { // 상, 우, 하, 좌 (4번 반복) + for (int k = 0; k < step; k++) { + arr[x][y] = ++count; + if (k == (step - 1)) { // step의 마지막은 방향 전환 + x += vx[v]; + y += vy[v]; + } else { // 직선 이동 + x += dx[v]; + y += dy[v]; + } + } + v = (v + 1) % 4; // step 끝나면 방향 전환 + } + step += 2; // 네 바퀴 다돌면 step 증가 + } + + + //then + for (int i = 0; i < n; i++) { + for (int j = 0; j < n; j++) { + bw.write(arr[i][j] + " "); + if (arr[i][j] == m) { + x = i + 1; + y = j + 1; + } + } + bw.newLine(); + } + + bw.write(x + " " + y); + br.close(); + bw.close(); + } +}