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] != 'G' && 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, N * N));
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 (mat[r2][c2] == 'D' || (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 |
Incorrect |
58 ms |
8452 KB |
Output isn't correct |
2 |
Incorrect |
58 ms |
8248 KB |
Output isn't correct |
3 |
Incorrect |
59 ms |
8348 KB |
Output isn't correct |
4 |
Incorrect |
61 ms |
8312 KB |
Output isn't correct |
5 |
Incorrect |
59 ms |
8576 KB |
Output isn't correct |
6 |
Incorrect |
62 ms |
8152 KB |
Output isn't correct |
7 |
Incorrect |
215 ms |
27740 KB |
Output isn't correct |
8 |
Correct |
61 ms |
8328 KB |
Output is correct |
9 |
Incorrect |
59 ms |
8268 KB |
Output isn't correct |
10 |
Incorrect |
60 ms |
8360 KB |
Output isn't correct |
11 |
Incorrect |
59 ms |
8560 KB |
Output isn't correct |
12 |
Incorrect |
69 ms |
8392 KB |
Output isn't correct |
13 |
Incorrect |
72 ms |
8624 KB |
Output isn't correct |
14 |
Correct |
67 ms |
8360 KB |
Output is correct |
15 |
Incorrect |
61 ms |
8304 KB |
Output isn't correct |
16 |
Incorrect |
60 ms |
8348 KB |
Output isn't correct |
17 |
Incorrect |
59 ms |
8348 KB |
Output isn't correct |
18 |
Incorrect |
58 ms |
8460 KB |
Output isn't correct |
19 |
Incorrect |
63 ms |
8544 KB |
Output isn't correct |
20 |
Incorrect |
63 ms |
8416 KB |
Output isn't correct |
21 |
Incorrect |
65 ms |
8520 KB |
Output isn't correct |
22 |
Incorrect |
63 ms |
8460 KB |
Output isn't correct |
23 |
Incorrect |
66 ms |
8480 KB |
Output isn't correct |
24 |
Incorrect |
64 ms |
8628 KB |
Output isn't correct |
25 |
Incorrect |
65 ms |
8508 KB |
Output isn't correct |
26 |
Incorrect |
64 ms |
8476 KB |
Output isn't correct |
27 |
Incorrect |
69 ms |
8732 KB |
Output isn't correct |
28 |
Incorrect |
63 ms |
8532 KB |
Output isn't correct |
29 |
Incorrect |
70 ms |
8556 KB |
Output isn't correct |
30 |
Incorrect |
69 ms |
8704 KB |
Output isn't correct |
31 |
Incorrect |
68 ms |
8676 KB |
Output isn't correct |
32 |
Incorrect |
66 ms |
8600 KB |
Output isn't correct |
33 |
Incorrect |
134 ms |
14244 KB |
Output isn't correct |
34 |
Incorrect |
170 ms |
14460 KB |
Output isn't correct |
35 |
Incorrect |
145 ms |
14416 KB |
Output isn't correct |
36 |
Incorrect |
125 ms |
14952 KB |
Output isn't correct |
37 |
Incorrect |
147 ms |
15224 KB |
Output isn't correct |
38 |
Incorrect |
128 ms |
15112 KB |
Output isn't correct |
39 |
Incorrect |
128 ms |
15540 KB |
Output isn't correct |
40 |
Incorrect |
142 ms |
15532 KB |
Output isn't correct |
41 |
Incorrect |
140 ms |
15396 KB |
Output isn't correct |
42 |
Incorrect |
135 ms |
17516 KB |
Output isn't correct |
43 |
Incorrect |
169 ms |
17784 KB |
Output isn't correct |
44 |
Incorrect |
142 ms |
17624 KB |
Output isn't correct |
45 |
Incorrect |
141 ms |
19328 KB |
Output isn't correct |
46 |
Incorrect |
154 ms |
19172 KB |
Output isn't correct |
47 |
Incorrect |
141 ms |
19480 KB |
Output isn't correct |
48 |
Incorrect |
168 ms |
20376 KB |
Output isn't correct |
49 |
Incorrect |
178 ms |
20316 KB |
Output isn't correct |
50 |
Incorrect |
165 ms |
20304 KB |
Output isn't correct |
51 |
Incorrect |
170 ms |
19564 KB |
Output isn't correct |
52 |
Incorrect |
179 ms |
19560 KB |
Output isn't correct |
53 |
Incorrect |
179 ms |
20760 KB |
Output isn't correct |
54 |
Incorrect |
170 ms |
23440 KB |
Output isn't correct |
55 |
Incorrect |
190 ms |
23592 KB |
Output isn't correct |
56 |
Incorrect |
176 ms |
24356 KB |
Output isn't correct |
57 |
Incorrect |
179 ms |
24300 KB |
Output isn't correct |
58 |
Incorrect |
200 ms |
22720 KB |
Output isn't correct |
59 |
Incorrect |
188 ms |
25572 KB |
Output isn't correct |
60 |
Incorrect |
193 ms |
27332 KB |
Output isn't correct |
61 |
Incorrect |
188 ms |
27656 KB |
Output isn't correct |
62 |
Incorrect |
179 ms |
27132 KB |
Output isn't correct |
63 |
Incorrect |
176 ms |
27316 KB |
Output isn't correct |
64 |
Incorrect |
174 ms |
27296 KB |
Output isn't correct |
65 |
Incorrect |
173 ms |
27388 KB |
Output isn't correct |
66 |
Incorrect |
177 ms |
27128 KB |
Output isn't correct |
67 |
Correct |
176 ms |
27156 KB |
Output is correct |
68 |
Incorrect |
206 ms |
27336 KB |
Output isn't correct |
69 |
Incorrect |
219 ms |
27940 KB |
Output isn't correct |
70 |
Incorrect |
208 ms |
27832 KB |
Output isn't correct |
71 |
Incorrect |
206 ms |
28036 KB |
Output isn't correct |
72 |
Incorrect |
216 ms |
27144 KB |
Output isn't correct |
73 |
Incorrect |
204 ms |
27392 KB |
Output isn't correct |
74 |
Incorrect |
224 ms |
27456 KB |
Output isn't correct |
75 |
Incorrect |
209 ms |
27672 KB |
Output isn't correct |
76 |
Incorrect |
209 ms |
27664 KB |
Output isn't correct |
77 |
Incorrect |
210 ms |
27716 KB |
Output isn't correct |
78 |
Correct |
215 ms |
27924 KB |
Output is correct |
79 |
Incorrect |
230 ms |
27876 KB |
Output isn't correct |
80 |
Incorrect |
223 ms |
27868 KB |
Output isn't correct |
81 |
Incorrect |
218 ms |
28008 KB |
Output isn't correct |
82 |
Incorrect |
217 ms |
27796 KB |
Output isn't correct |
83 |
Incorrect |
215 ms |
27976 KB |
Output isn't correct |
84 |
Incorrect |
219 ms |
28036 KB |
Output isn't correct |
85 |
Incorrect |
223 ms |
28060 KB |
Output isn't correct |
86 |
Incorrect |
198 ms |
28100 KB |
Output isn't correct |
87 |
Incorrect |
219 ms |
27840 KB |
Output isn't correct |
88 |
Incorrect |
224 ms |
27368 KB |
Output isn't correct |
89 |
Incorrect |
222 ms |
28160 KB |
Output isn't correct |
90 |
Incorrect |
218 ms |
28072 KB |
Output isn't correct |
91 |
Incorrect |
215 ms |
28104 KB |
Output isn't correct |
92 |
Incorrect |
215 ms |
28060 KB |
Output isn't correct |