# |
Submission time |
Handle |
Problem |
Language |
Result |
Execution time |
Memory |
815903 |
2023-08-09T02:13:09 Z |
powervic08 |
Mecho (IOI09_mecho) |
Java 11 |
|
551 ms |
51344 KB |
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));
}
}
}
long[][] dists = new long[n][m];
for (int i = 0; i < n; i++) {
Arrays.fill(dists[i], Long.MAX_VALUE);
}
while (!q.isEmpty()) {
Node p = q.poll();
if (p.dist >= dists[p.i][p.j]) {
continue;
}
dists[p.i][p.j] = 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] != 2) {
q.add(new Node(newi, newj, p.dist + S));
}
}
}
}
dists[endi][endj] = Integer.MAX_VALUE;
int l = -1;
int r = 2 * n * n;
while (r - l > 1) {
int mid = (l + r) / 2;
if (works(mid, dists, grid, starti, startj, endi, endj)) {
l = mid;
} else {
r = mid;
}
}
return l;
}
public static boolean works(int mid, long[][] dists, int[][] grid, int starti, int startj, int endi, int endj) {
Queue<Node> q = new LinkedList<>();
if ((long) mid * S >= dists[starti][startj])
return false;
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 && !visited[newi][newj]) {
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;
}
}
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
50 ms |
8300 KB |
Output is correct |
2 |
Correct |
52 ms |
8296 KB |
Output is correct |
3 |
Correct |
47 ms |
8644 KB |
Output is correct |
4 |
Correct |
47 ms |
8564 KB |
Output is correct |
5 |
Correct |
50 ms |
8468 KB |
Output is correct |
6 |
Correct |
52 ms |
8384 KB |
Output is correct |
7 |
Correct |
522 ms |
38672 KB |
Output is correct |
8 |
Correct |
48 ms |
8520 KB |
Output is correct |
9 |
Correct |
51 ms |
8492 KB |
Output is correct |
10 |
Correct |
50 ms |
8320 KB |
Output is correct |
11 |
Correct |
51 ms |
8464 KB |
Output is correct |
12 |
Incorrect |
59 ms |
8944 KB |
Output isn't correct |
13 |
Correct |
64 ms |
9028 KB |
Output is correct |
14 |
Correct |
118 ms |
11732 KB |
Output is correct |
15 |
Correct |
51 ms |
8184 KB |
Output is correct |
16 |
Correct |
50 ms |
8412 KB |
Output is correct |
17 |
Correct |
50 ms |
8488 KB |
Output is correct |
18 |
Correct |
50 ms |
8428 KB |
Output is correct |
19 |
Correct |
53 ms |
8416 KB |
Output is correct |
20 |
Correct |
59 ms |
8488 KB |
Output is correct |
21 |
Correct |
60 ms |
8536 KB |
Output is correct |
22 |
Correct |
53 ms |
8584 KB |
Output is correct |
23 |
Correct |
55 ms |
8584 KB |
Output is correct |
24 |
Correct |
59 ms |
8676 KB |
Output is correct |
25 |
Correct |
59 ms |
9120 KB |
Output is correct |
26 |
Correct |
69 ms |
8784 KB |
Output is correct |
27 |
Correct |
64 ms |
8788 KB |
Output is correct |
28 |
Correct |
66 ms |
8980 KB |
Output is correct |
29 |
Correct |
59 ms |
9024 KB |
Output is correct |
30 |
Correct |
63 ms |
9132 KB |
Output is correct |
31 |
Correct |
71 ms |
10172 KB |
Output is correct |
32 |
Correct |
69 ms |
9204 KB |
Output is correct |
33 |
Correct |
170 ms |
15412 KB |
Output is correct |
34 |
Correct |
190 ms |
15804 KB |
Output is correct |
35 |
Correct |
240 ms |
16880 KB |
Output is correct |
36 |
Correct |
147 ms |
16044 KB |
Output is correct |
37 |
Correct |
223 ms |
16036 KB |
Output is correct |
38 |
Correct |
258 ms |
17644 KB |
Output is correct |
39 |
Correct |
166 ms |
16516 KB |
Output is correct |
40 |
Correct |
215 ms |
16588 KB |
Output is correct |
41 |
Correct |
282 ms |
18448 KB |
Output is correct |
42 |
Correct |
156 ms |
17220 KB |
Output is correct |
43 |
Correct |
214 ms |
17172 KB |
Output is correct |
44 |
Correct |
293 ms |
19784 KB |
Output is correct |
45 |
Correct |
173 ms |
17568 KB |
Output is correct |
46 |
Correct |
201 ms |
17856 KB |
Output is correct |
47 |
Correct |
319 ms |
20452 KB |
Output is correct |
48 |
Correct |
168 ms |
18136 KB |
Output is correct |
49 |
Correct |
223 ms |
18856 KB |
Output is correct |
50 |
Correct |
377 ms |
22784 KB |
Output is correct |
51 |
Correct |
182 ms |
20772 KB |
Output is correct |
52 |
Correct |
224 ms |
21108 KB |
Output is correct |
53 |
Correct |
391 ms |
25244 KB |
Output is correct |
54 |
Correct |
165 ms |
22128 KB |
Output is correct |
55 |
Correct |
233 ms |
21892 KB |
Output is correct |
56 |
Correct |
402 ms |
27252 KB |
Output is correct |
57 |
Correct |
178 ms |
23876 KB |
Output is correct |
58 |
Correct |
230 ms |
23592 KB |
Output is correct |
59 |
Correct |
437 ms |
29052 KB |
Output is correct |
60 |
Correct |
176 ms |
25256 KB |
Output is correct |
61 |
Correct |
227 ms |
25444 KB |
Output is correct |
62 |
Correct |
481 ms |
30772 KB |
Output is correct |
63 |
Correct |
363 ms |
30508 KB |
Output is correct |
64 |
Correct |
551 ms |
31184 KB |
Output is correct |
65 |
Correct |
472 ms |
30652 KB |
Output is correct |
66 |
Correct |
423 ms |
30488 KB |
Output is correct |
67 |
Correct |
400 ms |
30748 KB |
Output is correct |
68 |
Correct |
279 ms |
26640 KB |
Output is correct |
69 |
Correct |
297 ms |
26460 KB |
Output is correct |
70 |
Correct |
290 ms |
25676 KB |
Output is correct |
71 |
Correct |
280 ms |
25988 KB |
Output is correct |
72 |
Correct |
242 ms |
25368 KB |
Output is correct |
73 |
Correct |
384 ms |
50800 KB |
Output is correct |
74 |
Correct |
466 ms |
50824 KB |
Output is correct |
75 |
Correct |
493 ms |
50800 KB |
Output is correct |
76 |
Correct |
501 ms |
50904 KB |
Output is correct |
77 |
Correct |
477 ms |
50744 KB |
Output is correct |
78 |
Correct |
504 ms |
51180 KB |
Output is correct |
79 |
Correct |
445 ms |
51172 KB |
Output is correct |
80 |
Correct |
450 ms |
51344 KB |
Output is correct |
81 |
Correct |
475 ms |
51256 KB |
Output is correct |
82 |
Correct |
453 ms |
51280 KB |
Output is correct |
83 |
Correct |
520 ms |
47196 KB |
Output is correct |
84 |
Correct |
504 ms |
47228 KB |
Output is correct |
85 |
Correct |
512 ms |
47336 KB |
Output is correct |
86 |
Correct |
484 ms |
47176 KB |
Output is correct |
87 |
Correct |
495 ms |
47028 KB |
Output is correct |
88 |
Correct |
520 ms |
42904 KB |
Output is correct |
89 |
Correct |
512 ms |
42616 KB |
Output is correct |
90 |
Correct |
508 ms |
42852 KB |
Output is correct |
91 |
Correct |
529 ms |
42684 KB |
Output is correct |
92 |
Correct |
523 ms |
42784 KB |
Output is correct |