Skip to content

Commit 73a772b

Browse files
authored
[20250722] BOJ / G4 / 거울 / 이종환
1 parent 6b5b877 commit 73a772b

File tree

1 file changed

+121
-0
lines changed

1 file changed

+121
-0
lines changed

0224LJH/202507/22 BOJ 거울 .md

Lines changed: 121 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,121 @@
1+
```java
2+
import java.awt.Point;
3+
import java.io.BufferedReader;
4+
import java.io.IOException;
5+
import java.io.InputStreamReader;
6+
import java.util.*;
7+
8+
public class Main {
9+
10+
//거울 만날 시 진행 방향
11+
//오른쪽 <-> 위
12+
// 아래 <-> 왼쪽
13+
14+
static final int MIRROR = -1;
15+
16+
static int height,width,direction;
17+
static int [][] arr;
18+
static int[] dy = {1,0,-1,0}; // 아래 오른쪽 위쪽 왼쪽 순
19+
static int[] dx = {0,1,0,-1};
20+
static int[] ans;
21+
22+
static HashMap<Integer, Point> map = new HashMap<>();
23+
24+
public static void main(String[] args) throws IOException {
25+
init();
26+
process();
27+
print();
28+
}
29+
30+
private static void init() throws IOException {
31+
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
32+
StringTokenizer st = new StringTokenizer(br.readLine());
33+
height = Integer.parseInt(st.nextToken());
34+
width = Integer.parseInt(st.nextToken());
35+
arr = new int[height+2][width+2];
36+
ans = new int[height*2 + width*2+1];
37+
38+
for (int i = 1; i <= height; i++) {
39+
st = new StringTokenizer(br.readLine());
40+
for (int j = 1; j <= width; j++) {
41+
arr[i][j] = Integer.parseInt(st.nextToken()) * (-1);
42+
}
43+
}
44+
45+
}
46+
47+
private static void process() throws IOException {
48+
markHoleNumber();
49+
for (int i = 1; i < ans.length; i++) {
50+
if (ans[i] != 0) continue;
51+
Point p = map.get(i);
52+
53+
if (p.x == 0) direction = 1; //오른쪽으로
54+
else if (p.y == height+1) direction = 2; //위로
55+
else if (p.x == width+1) direction = 3; //왼쪽으로
56+
else direction = 0; // 아래로
57+
58+
processLightMove(i);
59+
}
60+
61+
62+
63+
}
64+
65+
private static void processLightMove(int pointNum) {
66+
Point p = map.get(pointNum);
67+
int y = p.y;
68+
int x = p.x;
69+
70+
while (true) {
71+
y += dy[direction];
72+
x += dx[direction];
73+
74+
if (arr[y][x] > 0) {
75+
ans[pointNum] = arr[y][x];
76+
ans[ans[pointNum]] = pointNum;
77+
break;
78+
}
79+
80+
if (arr[y][x] == MIRROR){
81+
switch (direction) {
82+
case 0: direction = 3; break;
83+
case 1: direction = 2; break;
84+
case 2: direction = 1; break;
85+
case 3: direction = 0; break;
86+
}
87+
}
88+
89+
90+
}
91+
}
92+
93+
private static void markHoleNumber() {
94+
// 구멍 번호 마킹
95+
direction = 0;
96+
int num = 1;
97+
int y = 1;
98+
int x = 0;
99+
while ( y != 0 || x != 0){
100+
if ( (y == height +1 || y == 0) && (x == 0 || x == width + 1) ) {
101+
direction++;
102+
} else{
103+
map.put(num,new Point(x,y));
104+
arr[y][x] = num++;
105+
}
106+
107+
y += dy[direction];
108+
x += dx[direction];
109+
}
110+
111+
}
112+
113+
114+
private static void print() {
115+
for (int i = 1; i < ans.length; i++) {
116+
System.out.print(ans[i] + " ");
117+
}
118+
}
119+
}
120+
121+
```

0 commit comments

Comments
 (0)