import java.util.*;
import java.io.*;
public class mecho {
public static void main(String[] args) throws IOException {
Reader in = new Reader();
PrintWriter out = new PrintWriter(System.out);
int N = in.nextInt(), K = in.nextInt();
char[][] mat = new char[N][N];
for (int i = 0; i < N; i++) {
mat[i] = in.next().toCharArray();
}
Queue<int[]> q = new LinkedList<>();
int[][] bee = new int[N][N];
int[] start = new int[2], end = new int[2];
for (int i = 0; i < N; i++) {
for (int j = 0; j < N; j++) {
if (mat[i][j] == 'H') {
bee[i][j] = 0;
q.offer(new int[]{i, j});
} else {
bee[i][j] = -1;
}
if (mat[i][j] == 'M') {
start = new int[]{i, j};
}
if (mat[i][j] == 'D') {
end = new int[]{i, j};
}
}
}
int[] x = new int[]{-1, 1, 0, 0}, y = new int[]{0, 0, -1, 1};
while (!q.isEmpty()) {
int[] current = q.poll();
int r = current[0], c = current[1];
for (int k = 0; k < 4; k++) {
int r2 = r + x[k], c2 = c + y[k];
if (r2 >= 0 && r2 < N && c2 >= 0 && c2 < N) {
if (mat[r2][c2] != 'D' && mat[r2][c2] != 'T' && bee[r2][c2] == -1) {
bee[r2][c2] = bee[r][c] + 1;
q.offer(new int[]{r2, c2});
}
}
}
}
out.println(search(mat, bee, start, end, K, 0, bee[start[0]][start[1]] - 1));
out.close();
}
public static int search(char[][] mat, int[][] bee, int[] start, int[] end, int K, int l, int r) {
if (l > r) {
return -1;
}
int mid = (l + r) / 2;
if (check(mat, bee, start, end, K, mid)) {
int m = search(mat, bee, start, end, K, mid + 1, r);
if (m == -1) {
return mid;
}
return m;
}
return search(mat, bee, start, end, K, l, mid - 1);
}
public static boolean check(char[][] mat, int[][] bee, int[] start, int[] end, int K, int eat) {
int N = mat.length;
int[][] dist = new int[N][N];
for (int i = 0; i < N; i++) {
Arrays.fill(dist[i], -1);
}
dist[start[0]][start[1]] = 0;
Queue<int[]> q = new LinkedList<>();
q.offer(start);
int[] x = new int[]{-1, 1, 0, 0}, y = new int[]{0, 0, -1, 1};
while (!q.isEmpty()) {
int[] current = q.poll();
int r = current[0], c = current[1];
for (int k = 0; k < 4; k++) {
int r2 = r + x[k], c2 = c + y[k];
if (r2 >= 0 && r2 < N && c2 >= 0 && c2 < N) {
if (mat[r2][c2] != 'T' && mat[r2][c2] != 'H' && dist[r2][c2] == -1) {
if (bee[r2][c2] == -1 || (dist[r][c] + 1) / K + eat < bee[r2][c2]) {
dist[r2][c2] = dist[r][c] + 1;
q.offer(new int[]{r2, c2});
}
}
}
}
}
return dist[end[0]][end[1]] >= 0;
}
static class Reader {
BufferedInputStream in;
public Reader() {
in = new BufferedInputStream(System.in);
}
public String nextLine() throws IOException {
int c;
StringBuilder sb = new StringBuilder("");
while ((c = in.read()) != '\n')
sb.append((char)(c));
return sb.toString();
}
public String next() throws IOException {
int c;
StringBuilder sb = new StringBuilder("");
while ((c = in.read()) != ' ' && c != '\n')
sb.append((char)(c));
return sb.toString();
}
public int nextInt() throws IOException {
return (int)nextLong();
}
public long nextLong() throws IOException {
int c;
long res = 0;
boolean start = false, negative = false;
while ((c = in.read()) != ' ' && c != '\n' || !start)
if (c >= '0' && c <= '9' || c == '-') {
start = true;
if (c == '-')
negative = true;
else
res = res * 10 + c - '0';
}
return res * (negative ? -1 : 1);
}
}
public static void sort(int[] arr) {
List<Integer> list = new ArrayList<>();
for (int i : arr) {
list.add(i);
}
Collections.sort(list);
for (int i = 0; i < arr.length; i++) {
arr[i] = list.get(i);
}
}
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
68 ms |
8448 KB |
Output is correct |
2 |
Correct |
62 ms |
8456 KB |
Output is correct |
3 |
Correct |
60 ms |
8168 KB |
Output is correct |
4 |
Correct |
65 ms |
8504 KB |
Output is correct |
5 |
Correct |
63 ms |
8208 KB |
Output is correct |
6 |
Correct |
63 ms |
8252 KB |
Output is correct |
7 |
Correct |
566 ms |
29088 KB |
Output is correct |
8 |
Correct |
65 ms |
8220 KB |
Output is correct |
9 |
Correct |
74 ms |
8200 KB |
Output is correct |
10 |
Correct |
62 ms |
8324 KB |
Output is correct |
11 |
Correct |
67 ms |
8300 KB |
Output is correct |
12 |
Correct |
78 ms |
8396 KB |
Output is correct |
13 |
Correct |
82 ms |
8272 KB |
Output is correct |
14 |
Correct |
140 ms |
11276 KB |
Output is correct |
15 |
Correct |
67 ms |
8292 KB |
Output is correct |
16 |
Correct |
68 ms |
8344 KB |
Output is correct |
17 |
Correct |
67 ms |
8444 KB |
Output is correct |
18 |
Correct |
69 ms |
8360 KB |
Output is correct |
19 |
Correct |
62 ms |
8232 KB |
Output is correct |
20 |
Correct |
77 ms |
8204 KB |
Output is correct |
21 |
Correct |
80 ms |
8384 KB |
Output is correct |
22 |
Correct |
77 ms |
8536 KB |
Output is correct |
23 |
Correct |
76 ms |
8700 KB |
Output is correct |
24 |
Correct |
97 ms |
11200 KB |
Output is correct |
25 |
Correct |
100 ms |
11312 KB |
Output is correct |
26 |
Correct |
104 ms |
11384 KB |
Output is correct |
27 |
Correct |
109 ms |
11244 KB |
Output is correct |
28 |
Correct |
103 ms |
11416 KB |
Output is correct |
29 |
Correct |
108 ms |
11308 KB |
Output is correct |
30 |
Correct |
107 ms |
11176 KB |
Output is correct |
31 |
Correct |
127 ms |
11528 KB |
Output is correct |
32 |
Correct |
116 ms |
11552 KB |
Output is correct |
33 |
Correct |
253 ms |
15492 KB |
Output is correct |
34 |
Correct |
260 ms |
15340 KB |
Output is correct |
35 |
Correct |
313 ms |
18180 KB |
Output is correct |
36 |
Correct |
262 ms |
16316 KB |
Output is correct |
37 |
Correct |
282 ms |
15628 KB |
Output is correct |
38 |
Correct |
361 ms |
19184 KB |
Output is correct |
39 |
Correct |
305 ms |
17676 KB |
Output is correct |
40 |
Correct |
239 ms |
18352 KB |
Output is correct |
41 |
Correct |
395 ms |
19264 KB |
Output is correct |
42 |
Correct |
320 ms |
19012 KB |
Output is correct |
43 |
Correct |
250 ms |
18336 KB |
Output is correct |
44 |
Correct |
465 ms |
19532 KB |
Output is correct |
45 |
Correct |
298 ms |
19480 KB |
Output is correct |
46 |
Correct |
321 ms |
20948 KB |
Output is correct |
47 |
Correct |
404 ms |
21092 KB |
Output is correct |
48 |
Correct |
354 ms |
19324 KB |
Output is correct |
49 |
Correct |
268 ms |
21440 KB |
Output is correct |
50 |
Correct |
504 ms |
21252 KB |
Output is correct |
51 |
Correct |
287 ms |
22984 KB |
Output is correct |
52 |
Correct |
319 ms |
23120 KB |
Output is correct |
53 |
Correct |
459 ms |
24256 KB |
Output is correct |
54 |
Correct |
331 ms |
25720 KB |
Output is correct |
55 |
Correct |
297 ms |
24740 KB |
Output is correct |
56 |
Correct |
525 ms |
24576 KB |
Output is correct |
57 |
Correct |
367 ms |
26828 KB |
Output is correct |
58 |
Correct |
337 ms |
26516 KB |
Output is correct |
59 |
Correct |
550 ms |
26416 KB |
Output is correct |
60 |
Correct |
350 ms |
28180 KB |
Output is correct |
61 |
Correct |
350 ms |
28144 KB |
Output is correct |
62 |
Correct |
650 ms |
28556 KB |
Output is correct |
63 |
Correct |
651 ms |
27804 KB |
Output is correct |
64 |
Correct |
740 ms |
28584 KB |
Output is correct |
65 |
Correct |
732 ms |
28408 KB |
Output is correct |
66 |
Correct |
663 ms |
27836 KB |
Output is correct |
67 |
Correct |
607 ms |
28452 KB |
Output is correct |
68 |
Correct |
452 ms |
29284 KB |
Output is correct |
69 |
Correct |
438 ms |
30140 KB |
Output is correct |
70 |
Correct |
446 ms |
28884 KB |
Output is correct |
71 |
Correct |
450 ms |
24184 KB |
Output is correct |
72 |
Correct |
367 ms |
26588 KB |
Output is correct |
73 |
Correct |
425 ms |
25616 KB |
Output is correct |
74 |
Correct |
496 ms |
28984 KB |
Output is correct |
75 |
Correct |
503 ms |
28428 KB |
Output is correct |
76 |
Correct |
527 ms |
28580 KB |
Output is correct |
77 |
Correct |
550 ms |
28536 KB |
Output is correct |
78 |
Correct |
495 ms |
28584 KB |
Output is correct |
79 |
Correct |
510 ms |
29696 KB |
Output is correct |
80 |
Correct |
498 ms |
28404 KB |
Output is correct |
81 |
Correct |
499 ms |
28420 KB |
Output is correct |
82 |
Correct |
577 ms |
29676 KB |
Output is correct |
83 |
Correct |
563 ms |
28796 KB |
Output is correct |
84 |
Correct |
496 ms |
28568 KB |
Output is correct |
85 |
Correct |
484 ms |
28796 KB |
Output is correct |
86 |
Correct |
500 ms |
28236 KB |
Output is correct |
87 |
Correct |
485 ms |
28688 KB |
Output is correct |
88 |
Correct |
566 ms |
30988 KB |
Output is correct |
89 |
Correct |
546 ms |
30900 KB |
Output is correct |
90 |
Correct |
557 ms |
31552 KB |
Output is correct |
91 |
Correct |
552 ms |
30948 KB |
Output is correct |
92 |
Correct |
561 ms |
31480 KB |
Output is correct |