import java.util.*;
import java.io.*;
public class mecho {
static int[] dx = {-1, 1, 0, 0};
static int[] dy = {0, 0, -1, 1};
static int S;
public static void main(String[] args) throws IOException {
BufferedReader f = new BufferedReader(new InputStreamReader(System.in));
PrintWriter out = new PrintWriter(System.out);
StringTokenizer st = new StringTokenizer(f.readLine());
int n = Integer.parseInt(st.nextToken());
S = Integer.parseInt(st.nextToken());
int[][] grid = new int[n][n];
int starti = 0, startj = 0, endi = 0, endj = 0;
for (int i = 0; i < n; i++) {
String s = f.readLine();
for (int j = 0; j < n; j++) {
if (s.charAt(j) == 'H') grid[i][j] = 1;
else if (s.charAt(j) == 'T') grid[i][j] = 2;
else if (s.charAt(j) == 'M') {
starti = i;
startj = j;
} else if (s.charAt(j) == 'D') {
endi = i;
endj = j;
}
}
}
out.println(maximumMinutes(grid, starti, startj, endi, endj));
out.close();
f.close();
}
public static int maximumMinutes(int[][] grid, int starti, int startj, int endi, int endj) {
int n = grid.length;
int m = grid[0].length;
Queue<Node> q = new LinkedList<>();
for (int i = 0; i < n; i++) {
for (int j = 0; j < m; j++) {
if (grid[i][j] == 1) {
q.add(new Node(i, j, 0));
}
}
}
int[][] dists = new int[n][m];
for (int i = 0; i < n; i++) {
Arrays.fill(dists[i], Integer.MAX_VALUE);
}
while (!q.isEmpty()) {
Node p = q.poll();
if (p.dist >= dists[p.i][p.j]) {
continue;
}
dists[p.i][p.j] = (int) p.dist;
for (int i = 0; i < 4; i++) {
int newi = dx[i] + p.i;
int newj = dy[i] + p.j;
if (inRange(newi, newj, grid)) {
if (grid[newi][newj] == 0) {
q.add(new Node(newi, newj, p.dist + S));
}
}
}
}
dists[endi][endj] = Integer.MAX_VALUE;
int l = -1;
int r = n * n;
while (l < r) {
int mid = (l + r + 1) / 2;
if (works(mid, dists, grid, starti, startj, endi, endj)) {
l = mid;
} else {
r = mid - 1;
}
}
return l;
}
public static boolean works(int mid, int[][] dists, int[][] grid, int starti, int startj, int endi, int endj) {
Queue<Node> q = new LinkedList<>();
q.add(new Node(starti, startj, (long) mid * S));
boolean[][] visited = new boolean[grid.length][grid[0].length];
while (!q.isEmpty()) {
Node p = q.poll();
if (p.i == endi && p.j == endj) {
return true;
}
if (visited[p.i][p.j]) {
continue;
}
visited[p.i][p.j] = true;
for (int i = 0; i < 4; i++) {
int newi = dx[i] + p.i;
int newj = dy[i] + p.j;
if (inRange(newi, newj, grid)) {
if (grid[newi][newj] == 0 && dists[newi][newj] > p.dist + 1 || newi == endi && newj == endj && dists[newi][newj] == p.dist + 1) {
q.add(new Node(newi, newj, p.dist + 1));
}
}
}
}
return false;
}
public static boolean inRange(int i, int j, int[][] grid) {
return i >= 0 && j >= 0 && i < grid.length && j < grid[0].length;
}
static class Node {
int i, j;
long dist;
public Node(int a, int b, long c) {
i = a;
j = b;
dist = c;
}
}
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
48 ms |
8616 KB |
Output is correct |
2 |
Correct |
48 ms |
8316 KB |
Output is correct |
3 |
Correct |
48 ms |
8476 KB |
Output is correct |
4 |
Correct |
50 ms |
8448 KB |
Output is correct |
5 |
Correct |
48 ms |
8608 KB |
Output is correct |
6 |
Correct |
50 ms |
8376 KB |
Output is correct |
7 |
Correct |
603 ms |
33512 KB |
Output is correct |
8 |
Correct |
54 ms |
8412 KB |
Output is correct |
9 |
Correct |
49 ms |
8692 KB |
Output is correct |
10 |
Correct |
51 ms |
8456 KB |
Output is correct |
11 |
Correct |
52 ms |
8560 KB |
Output is correct |
12 |
Incorrect |
56 ms |
9004 KB |
Output isn't correct |
13 |
Incorrect |
62 ms |
9040 KB |
Output isn't correct |
14 |
Correct |
98 ms |
12556 KB |
Output is correct |
15 |
Correct |
49 ms |
8504 KB |
Output is correct |
16 |
Correct |
52 ms |
8320 KB |
Output is correct |
17 |
Correct |
49 ms |
8544 KB |
Output is correct |
18 |
Correct |
51 ms |
8452 KB |
Output is correct |
19 |
Correct |
51 ms |
8432 KB |
Output is correct |
20 |
Correct |
53 ms |
8496 KB |
Output is correct |
21 |
Correct |
54 ms |
8604 KB |
Output is correct |
22 |
Correct |
59 ms |
8628 KB |
Output is correct |
23 |
Correct |
54 ms |
8496 KB |
Output is correct |
24 |
Correct |
58 ms |
8628 KB |
Output is correct |
25 |
Correct |
62 ms |
8628 KB |
Output is correct |
26 |
Correct |
61 ms |
8928 KB |
Output is correct |
27 |
Correct |
59 ms |
8800 KB |
Output is correct |
28 |
Correct |
63 ms |
9064 KB |
Output is correct |
29 |
Correct |
62 ms |
8952 KB |
Output is correct |
30 |
Correct |
66 ms |
9008 KB |
Output is correct |
31 |
Correct |
72 ms |
10328 KB |
Output is correct |
32 |
Correct |
76 ms |
10968 KB |
Output is correct |
33 |
Correct |
141 ms |
14772 KB |
Output is correct |
34 |
Correct |
199 ms |
14776 KB |
Output is correct |
35 |
Correct |
251 ms |
16096 KB |
Output is correct |
36 |
Correct |
144 ms |
15372 KB |
Output is correct |
37 |
Correct |
205 ms |
15236 KB |
Output is correct |
38 |
Correct |
278 ms |
16600 KB |
Output is correct |
39 |
Correct |
148 ms |
15612 KB |
Output is correct |
40 |
Correct |
205 ms |
15852 KB |
Output is correct |
41 |
Correct |
308 ms |
17600 KB |
Output is correct |
42 |
Correct |
155 ms |
15916 KB |
Output is correct |
43 |
Correct |
211 ms |
16132 KB |
Output is correct |
44 |
Correct |
329 ms |
18628 KB |
Output is correct |
45 |
Correct |
151 ms |
16480 KB |
Output is correct |
46 |
Correct |
199 ms |
16668 KB |
Output is correct |
47 |
Correct |
356 ms |
18828 KB |
Output is correct |
48 |
Correct |
166 ms |
16680 KB |
Output is correct |
49 |
Correct |
214 ms |
17220 KB |
Output is correct |
50 |
Correct |
389 ms |
19516 KB |
Output is correct |
51 |
Correct |
162 ms |
17292 KB |
Output is correct |
52 |
Correct |
215 ms |
17688 KB |
Output is correct |
53 |
Correct |
445 ms |
21164 KB |
Output is correct |
54 |
Correct |
163 ms |
18156 KB |
Output is correct |
55 |
Correct |
229 ms |
18644 KB |
Output is correct |
56 |
Correct |
469 ms |
23272 KB |
Output is correct |
57 |
Correct |
170 ms |
19100 KB |
Output is correct |
58 |
Correct |
223 ms |
21064 KB |
Output is correct |
59 |
Correct |
509 ms |
25456 KB |
Output is correct |
60 |
Correct |
168 ms |
20460 KB |
Output is correct |
61 |
Correct |
225 ms |
21328 KB |
Output is correct |
62 |
Correct |
555 ms |
25612 KB |
Output is correct |
63 |
Correct |
369 ms |
25600 KB |
Output is correct |
64 |
Correct |
577 ms |
26820 KB |
Output is correct |
65 |
Correct |
478 ms |
26380 KB |
Output is correct |
66 |
Correct |
444 ms |
25792 KB |
Output is correct |
67 |
Correct |
424 ms |
25756 KB |
Output is correct |
68 |
Correct |
280 ms |
24980 KB |
Output is correct |
69 |
Correct |
288 ms |
24452 KB |
Output is correct |
70 |
Correct |
276 ms |
23464 KB |
Output is correct |
71 |
Correct |
278 ms |
24500 KB |
Output is correct |
72 |
Incorrect |
259 ms |
21868 KB |
Output isn't correct |
73 |
Incorrect |
412 ms |
47760 KB |
Output isn't correct |
74 |
Correct |
500 ms |
47820 KB |
Output is correct |
75 |
Correct |
551 ms |
48004 KB |
Output is correct |
76 |
Correct |
531 ms |
47900 KB |
Output is correct |
77 |
Correct |
505 ms |
47700 KB |
Output is correct |
78 |
Correct |
566 ms |
44472 KB |
Output is correct |
79 |
Correct |
469 ms |
44712 KB |
Output is correct |
80 |
Correct |
487 ms |
44748 KB |
Output is correct |
81 |
Correct |
560 ms |
44612 KB |
Output is correct |
82 |
Correct |
510 ms |
44684 KB |
Output is correct |
83 |
Correct |
603 ms |
40548 KB |
Output is correct |
84 |
Correct |
561 ms |
40396 KB |
Output is correct |
85 |
Correct |
572 ms |
40468 KB |
Output is correct |
86 |
Correct |
533 ms |
40428 KB |
Output is correct |
87 |
Correct |
580 ms |
40500 KB |
Output is correct |
88 |
Correct |
582 ms |
36768 KB |
Output is correct |
89 |
Correct |
566 ms |
36636 KB |
Output is correct |
90 |
Correct |
600 ms |
36580 KB |
Output is correct |
91 |
Correct |
589 ms |
36748 KB |
Output is correct |
92 |
Correct |
601 ms |
36748 KB |
Output is correct |