Skip to content

Commit 776241e

Browse files
authored
[20251124] BOJ / P3 / 달리기
1 parent accb335 commit 776241e

File tree

1 file changed

+95
-0
lines changed

1 file changed

+95
-0
lines changed
Lines changed: 95 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,95 @@
1+
```
2+
import java.io.*;
3+
import java.util.ArrayDeque;
4+
import java.util.Queue;
5+
import java.util.StringTokenizer;
6+
7+
public class Main {
8+
private static final BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
9+
private static final BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(System.out));
10+
private static final int[] dx = {1, 0, -1, 0};
11+
private static final int[] dy = {0, 1, 0, -1};
12+
private static char[][] map;
13+
private static int[][] dist, count;
14+
private static int[] start, end;
15+
private static int N, M, K;
16+
17+
public static void main(String[] args) throws IOException {
18+
init();
19+
int answer = BFS();
20+
21+
bw.write(answer + "\n");
22+
bw.flush();
23+
bw.close();
24+
br.close();
25+
}
26+
27+
private static void init() throws IOException {
28+
StringTokenizer st = new StringTokenizer(br.readLine());
29+
N = Integer.parseInt(st.nextToken());
30+
M = Integer.parseInt(st.nextToken());
31+
K = Integer.parseInt(st.nextToken());
32+
33+
map = new char[N + 1][M + 1];
34+
dist = new int[N + 1][M + 1];
35+
count = new int[N + 1][M + 1];
36+
37+
start = new int[2];
38+
end = new int[2];
39+
40+
for (int i = 1; i <= N; i++) {
41+
char[] input = br.readLine().toCharArray();
42+
for (int j = 1; j <= M; j++) {
43+
map[i][j] = input[j - 1];
44+
dist[i][j] = Integer.MAX_VALUE;
45+
}
46+
}
47+
48+
st = new StringTokenizer(br.readLine());
49+
start[0] = Integer.parseInt(st.nextToken());
50+
start[1] = Integer.parseInt(st.nextToken());
51+
end[0] = Integer.parseInt(st.nextToken());
52+
end[1] = Integer.parseInt(st.nextToken());
53+
}
54+
55+
private static int BFS() {
56+
Queue<int[]> q = new ArrayDeque<>();
57+
dist[start[0]][start[1]] = 0;
58+
q.add(new int[]{start[0], start[1], 0});
59+
60+
while (!q.isEmpty()) {
61+
int[] current = q.poll();
62+
63+
for (int i = 0; i < 4; i++) {
64+
int nx = current[0];
65+
int ny = current[1];
66+
if (i % 2 == 0) {
67+
int k = 1;
68+
while (k <= K && !OOB(nx + dx[i] * k, ny) && map[nx + dx[i] * k][ny] == '.') {
69+
if (dist[nx + dx[i] * k][ny] < current[2] + 1) break;
70+
dist[nx + dx[i] * k][ny] = Math.min(dist[nx + dx[i] * k][ny], current[2] + 1);
71+
count[nx + dx[i] * k][ny]++;
72+
if (count[nx + dx[i] * k][ny] <= 2) q.add(new int[]{nx + dx[i] * k, ny, current[2] + 1});
73+
k++;
74+
}
75+
} else {
76+
int k = 1;
77+
while (k <= K && !OOB(nx, ny + dy[i] * k) && map[nx][ny + dy[i] * k] == '.') {
78+
if (dist[nx][ny + dy[i] * k] < current[2] + 1) break;
79+
dist[nx][ny + dy[i] * k] = Math.min(dist[nx][ny + dy[i] * k], current[2] + 1);
80+
count[nx][ny + dy[i] * k]++;
81+
if (count[nx][ny + dy[i] * k] <= 2) q.add(new int[]{nx, ny + dy[i] * k, current[2] + 1});
82+
k++;
83+
}
84+
}
85+
}
86+
}
87+
88+
return dist[end[0]][end[1]] != Integer.MAX_VALUE ? dist[end[0]][end[1]] : -1;
89+
}
90+
91+
private static boolean OOB(int nx, int ny) {
92+
return nx < 1 || nx > N || ny < 1 || ny > M;
93+
}
94+
}
95+
```

0 commit comments

Comments
 (0)