Submission #815887

# Submission time Handle Problem Language Result Execution time Memory
815887 2023-08-09T02:02:21 Z powervic08 Mecho (IOI09_mecho) Java 11
95 / 100
539 ms 47944 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));
                }
            }
        }
        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 = 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, int[][] dists, int[][] grid, int starti, int startj, int endi, int endj) {
        Queue<Node> q = new LinkedList<>();
        if (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 51 ms 8408 KB Output is correct
2 Correct 48 ms 8352 KB Output is correct
3 Correct 49 ms 8264 KB Output is correct
4 Correct 52 ms 8496 KB Output is correct
5 Correct 46 ms 8484 KB Output is correct
6 Correct 47 ms 8396 KB Output is correct
7 Correct 505 ms 33484 KB Output is correct
8 Correct 55 ms 8344 KB Output is correct
9 Correct 49 ms 8548 KB Output is correct
10 Correct 53 ms 8416 KB Output is correct
11 Correct 50 ms 8264 KB Output is correct
12 Incorrect 58 ms 8696 KB Output isn't correct
13 Correct 61 ms 8900 KB Output is correct
14 Correct 113 ms 12040 KB Output is correct
15 Correct 48 ms 8292 KB Output is correct
16 Correct 48 ms 8464 KB Output is correct
17 Correct 50 ms 8692 KB Output is correct
18 Correct 49 ms 8316 KB Output is correct
19 Correct 50 ms 8324 KB Output is correct
20 Correct 52 ms 8332 KB Output is correct
21 Correct 53 ms 8616 KB Output is correct
22 Correct 53 ms 8664 KB Output is correct
23 Correct 55 ms 8480 KB Output is correct
24 Correct 61 ms 8608 KB Output is correct
25 Correct 59 ms 8732 KB Output is correct
26 Correct 59 ms 8884 KB Output is correct
27 Correct 59 ms 8712 KB Output is correct
28 Correct 63 ms 9000 KB Output is correct
29 Correct 61 ms 8700 KB Output is correct
30 Correct 63 ms 8936 KB Output is correct
31 Correct 73 ms 10256 KB Output is correct
32 Correct 63 ms 9024 KB Output is correct
33 Correct 138 ms 14896 KB Output is correct
34 Correct 205 ms 14792 KB Output is correct
35 Correct 249 ms 16340 KB Output is correct
36 Correct 145 ms 15056 KB Output is correct
37 Correct 205 ms 15528 KB Output is correct
38 Correct 275 ms 16964 KB Output is correct
39 Correct 148 ms 15668 KB Output is correct
40 Correct 212 ms 15772 KB Output is correct
41 Correct 279 ms 17876 KB Output is correct
42 Correct 153 ms 16048 KB Output is correct
43 Correct 215 ms 16332 KB Output is correct
44 Correct 299 ms 18672 KB Output is correct
45 Correct 157 ms 16340 KB Output is correct
46 Correct 203 ms 16828 KB Output is correct
47 Correct 319 ms 19300 KB Output is correct
48 Correct 155 ms 16996 KB Output is correct
49 Correct 222 ms 17132 KB Output is correct
50 Correct 352 ms 19936 KB Output is correct
51 Correct 160 ms 17532 KB Output is correct
52 Correct 208 ms 17456 KB Output is correct
53 Correct 380 ms 21116 KB Output is correct
54 Correct 161 ms 18264 KB Output is correct
55 Correct 223 ms 18172 KB Output is correct
56 Correct 389 ms 23548 KB Output is correct
57 Correct 168 ms 18912 KB Output is correct
58 Correct 224 ms 21036 KB Output is correct
59 Correct 440 ms 25452 KB Output is correct
60 Correct 170 ms 20572 KB Output is correct
61 Correct 231 ms 20976 KB Output is correct
62 Correct 491 ms 25900 KB Output is correct
63 Correct 364 ms 25516 KB Output is correct
64 Correct 530 ms 26500 KB Output is correct
65 Correct 458 ms 26076 KB Output is correct
66 Correct 412 ms 25980 KB Output is correct
67 Correct 378 ms 25888 KB Output is correct
68 Correct 269 ms 23884 KB Output is correct
69 Correct 299 ms 24080 KB Output is correct
70 Correct 308 ms 22148 KB Output is correct
71 Correct 318 ms 22664 KB Output is correct
72 Correct 254 ms 21624 KB Output is correct
73 Correct 363 ms 47944 KB Output is correct
74 Correct 464 ms 47796 KB Output is correct
75 Correct 514 ms 47776 KB Output is correct
76 Correct 500 ms 47712 KB Output is correct
77 Correct 469 ms 47592 KB Output is correct
78 Correct 502 ms 44640 KB Output is correct
79 Correct 476 ms 44500 KB Output is correct
80 Correct 440 ms 44656 KB Output is correct
81 Correct 497 ms 44640 KB Output is correct
82 Correct 469 ms 44552 KB Output is correct
83 Correct 521 ms 40556 KB Output is correct
84 Correct 478 ms 40616 KB Output is correct
85 Correct 535 ms 40572 KB Output is correct
86 Correct 513 ms 40440 KB Output is correct
87 Correct 485 ms 40492 KB Output is correct
88 Correct 512 ms 36812 KB Output is correct
89 Correct 522 ms 36456 KB Output is correct
90 Correct 510 ms 36608 KB Output is correct
91 Correct 518 ms 36672 KB Output is correct
92 Correct 539 ms 36768 KB Output is correct