From 73a772b3c6e7cc465a2a3aff193c5fc02a5745f5 Mon Sep 17 00:00:00 2001 From: Jonghwan Lee <123362165+0224LJH@users.noreply.github.com> Date: Tue, 22 Jul 2025 19:06:41 +0900 Subject: [PATCH] =?UTF-8?q?[20250722]=20BOJ=20/=20G4=20/=20=EA=B1=B0?= =?UTF-8?q?=EC=9A=B8=20/=20=EC=9D=B4=EC=A2=85=ED=99=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../22 BOJ \352\261\260\354\232\270 .md" | 121 ++++++++++++++++++ 1 file changed, 121 insertions(+) create mode 100644 "0224LJH/202507/22 BOJ \352\261\260\354\232\270 .md" diff --git "a/0224LJH/202507/22 BOJ \352\261\260\354\232\270 .md" "b/0224LJH/202507/22 BOJ \352\261\260\354\232\270 .md" new file mode 100644 index 00000000..35cdc7ad --- /dev/null +++ "b/0224LJH/202507/22 BOJ \352\261\260\354\232\270 .md" @@ -0,0 +1,121 @@ +```java +import java.awt.Point; +import java.io.BufferedReader; +import java.io.IOException; +import java.io.InputStreamReader; +import java.util.*; + +public class Main { + + //거울 만날 시 진행 방향 + //오른쪽 <-> 위 + // 아래 <-> 왼쪽 + + static final int MIRROR = -1; + + static int height,width,direction; + static int [][] arr; + static int[] dy = {1,0,-1,0}; // 아래 오른쪽 위쪽 왼쪽 순 + static int[] dx = {0,1,0,-1}; + static int[] ans; + + static HashMap map = new HashMap<>(); + + public static void main(String[] args) throws IOException { + init(); + process(); + print(); + } + + private static void init() throws IOException { + BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); + StringTokenizer st = new StringTokenizer(br.readLine()); + height = Integer.parseInt(st.nextToken()); + width = Integer.parseInt(st.nextToken()); + arr = new int[height+2][width+2]; + ans = new int[height*2 + width*2+1]; + + for (int i = 1; i <= height; i++) { + st = new StringTokenizer(br.readLine()); + for (int j = 1; j <= width; j++) { + arr[i][j] = Integer.parseInt(st.nextToken()) * (-1); + } + } + + } + + private static void process() throws IOException { + markHoleNumber(); + for (int i = 1; i < ans.length; i++) { + if (ans[i] != 0) continue; + Point p = map.get(i); + + if (p.x == 0) direction = 1; //오른쪽으로 + else if (p.y == height+1) direction = 2; //위로 + else if (p.x == width+1) direction = 3; //왼쪽으로 + else direction = 0; // 아래로 + + processLightMove(i); + } + + + + } + + private static void processLightMove(int pointNum) { + Point p = map.get(pointNum); + int y = p.y; + int x = p.x; + + while (true) { + y += dy[direction]; + x += dx[direction]; + + if (arr[y][x] > 0) { + ans[pointNum] = arr[y][x]; + ans[ans[pointNum]] = pointNum; + break; + } + + if (arr[y][x] == MIRROR){ + switch (direction) { + case 0: direction = 3; break; + case 1: direction = 2; break; + case 2: direction = 1; break; + case 3: direction = 0; break; + } + } + + + } + } + + private static void markHoleNumber() { + // 구멍 번호 마킹 + direction = 0; + int num = 1; + int y = 1; + int x = 0; + while ( y != 0 || x != 0){ + if ( (y == height +1 || y == 0) && (x == 0 || x == width + 1) ) { + direction++; + } else{ + map.put(num,new Point(x,y)); + arr[y][x] = num++; + } + + y += dy[direction]; + x += dx[direction]; + } + + } + + + private static void print() { + for (int i = 1; i < ans.length; i++) { + System.out.print(ans[i] + " "); + } + } +} + +``` \ No newline at end of file