Skip to content

Commit 86b0b83

Browse files
authored
Merge pull request #853 from AlgorithmWithGod/0224LJH
[20250909] BOJ / G3 / 레이저 통신 / 이종환
2 parents f9156b6 + 9c118a9 commit 86b0b83

File tree

1 file changed

+111
-0
lines changed

1 file changed

+111
-0
lines changed
Lines changed: 111 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,111 @@
1+
```java
2+
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+
static char[][] arr;
10+
static int[][] dp;
11+
static int width,height,goalY,goalX , curY,curX, curCnt,curDir,ans;
12+
static boolean success = false;
13+
14+
static Queue<Point> q = new LinkedList<>();
15+
static int[] dy = {-1,0,1,0};
16+
static int[] dx = {0,1,0,-1};
17+
18+
static final char WALL = '*';
19+
static final char GOAL = 'C';
20+
21+
22+
static class Point{
23+
int y,x,dir;
24+
25+
public Point(int y,int x,int dir) {
26+
this.y = y;
27+
this.x = x;
28+
this.dir = dir;
29+
}
30+
}
31+
32+
public static void main(String[] args) throws NumberFormatException, IOException {
33+
init();
34+
process();
35+
print();
36+
}
37+
38+
public static void init() throws NumberFormatException, IOException {
39+
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
40+
StringTokenizer st = new StringTokenizer(br.readLine());
41+
width = Integer.parseInt(st.nextToken());
42+
height = Integer.parseInt(st.nextToken());
43+
arr = new char[height][width];
44+
dp = new int[height][width];
45+
boolean first = true;
46+
47+
for (int i = 0 ; i < height; i++) {
48+
String input = br.readLine();
49+
arr[i] = input.toCharArray();
50+
Arrays.fill(dp[i], Integer.MAX_VALUE/2);
51+
for (int j = 0; j < width; j++) {
52+
if (arr[i][j] == GOAL && first) {
53+
first = false;
54+
dp[i][j] = -1;
55+
for (int k = 0; k < 4; k++) q.add(new Point(i,j,k));
56+
} else if (arr[i][j] == GOAL) {
57+
goalY = i;
58+
goalX = j;
59+
} else if (arr[i][j] == WALL) {
60+
dp[i][j] = -1;
61+
}
62+
}
63+
}
64+
65+
}
66+
67+
public static void process() {
68+
while(!q.isEmpty()) {
69+
int qLen = q.size();
70+
for (int i = 0; i < qLen; i++) {
71+
unitProcess();
72+
}
73+
}
74+
}
75+
76+
77+
public static void unitProcess() {
78+
Point p = q.poll();
79+
for (int j = 0; j < 4; j++) {
80+
curY = p.y;
81+
curX = p.x;
82+
curDir = j;
83+
curCnt = dp[curY][curX] +1 ;
84+
if (curDir == p.dir) continue;
85+
86+
while(true) {
87+
curY += dy[curDir];
88+
curX += dx[curDir];
89+
if (curY == goalY && curX == goalX) {
90+
ans = curCnt;
91+
}
92+
93+
if (curY >= height || curY < 0 || curX >= width || curX < 0 ) break;
94+
if (arr[curY][curX] == WALL) break;
95+
if (dp[curY][curX] <= curCnt) continue;
96+
97+
dp[curY][curX] = curCnt;
98+
q.add(new Point(curY,curX,j));
99+
100+
101+
102+
}
103+
104+
}
105+
}
106+
107+
public static void print() {
108+
System.out.println(dp[goalY][goalX]);
109+
}
110+
}
111+
```

0 commit comments

Comments
 (0)