-
Notifications
You must be signed in to change notification settings - Fork 0
/
B16930.java
91 lines (72 loc) · 2.48 KB
/
B16930.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
import java.util.*;
import java.io.*;
class B16930 {
static int N, M, K;
static char[][] arr;
static int[][] visit;
static int sx, sy;
static int ex, ey;
static int[] dx = {-1, 0, 1, 0};
static int[] dy = {0, -1, 0, 1};
static int result;
public static void main (String[] args) throws IOException {
input();
solution();
output();
}
public static void solution() {
// BFS
Queue<int[]> queue = new ArrayDeque<>();
visit[sx][sy] = 1;
queue.add(new int[]{sx, sy, 1});
while (!queue.isEmpty()) {
int[] q = queue.poll();
int move = q[2];
for (int d = 0; d < dx.length; d++) {
for (int k = 1; k <= K; k++) {
int nx = q[0] + dx[d] * k;
int ny = q[1] + dy[d] * k;
if (nx < 0 || ny < 0 || nx >= N || ny >= M || arr[nx][ny] == '#') break;
if (nx == ex && ny == ey) {
result = move;
return;
}
if (visit[nx][ny] == 0) {
visit[nx][ny] = move + 1;
queue.add(new int[]{nx, ny, move + 1});
}
else if (visit[nx][ny] == move + 1) continue;
else break;
}
}
}
}
public static void output() throws IOException {
BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(System.out));
bw.write(result+"");
bw.flush();
bw.close();
}
public static void input() throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
StringTokenizer st = new StringTokenizer(br.readLine());
N = Integer.parseInt(st.nextToken());
M = Integer.parseInt(st.nextToken());
K = Integer.parseInt(st.nextToken());
arr = new char[N][M];
visit = new int[N][M];
result = -1;
for (int i = 0; i < N; i++) {
String line = br.readLine();
for (int j = 0; j < M; j++) {
arr[i][j] = line.charAt(j);
}
}
st = new StringTokenizer(br.readLine());
sx = Integer.parseInt(st.nextToken()) - 1;
sy = Integer.parseInt(st.nextToken()) - 1;
ex = Integer.parseInt(st.nextToken()) - 1;
ey = Integer.parseInt(st.nextToken()) - 1;
br.close();
}
}